esx_vi_types.c 69.5 KB
Newer Older
1 2 3
/*
 * esx_vi_types.c: client for the VMware VI API 2.5 to manage ESX hosts
 *
4
 * Copyright (C) 2010, 2014 Red Hat, Inc.
5
 * Copyright (C) 2009-2011 Matthias Bolte <matthias.bolte@googlemail.com>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */

#include <config.h>

25
#include <stdint.h>
26 27 28
#include <libxml/parser.h>
#include <libxml/xpathInternals.h>

29
#include "virbuffer.h"
30
#include "datatypes.h"
31
#include "viralloc.h"
32
#include "virlog.h"
33 34
#include "esx_vi.h"
#include "esx_vi_types.h"
35
#include "virstring.h"
36 37 38

#define VIR_FROM_THIS VIR_FROM_ESX

39
VIR_LOG_INIT("esx.esx_vi_types");
40

41
#define ESX_VI__TEMPLATE__ALLOC(__type)                                       \
42
    int                                                                       \
43
    esxVI_##__type##_Alloc(esxVI_##__type **ptrptr)                           \
44
    {                                                                         \
45
        if (!ptrptr || *ptrptr) {                                             \
46 47 48 49 50 51
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); \
            return -1;                                                  \
        }                                                               \
                                                                        \
        if (VIR_ALLOC(*ptrptr) < 0)                                     \
            return -1;                                                  \
52 53 54 55
                                                                              \
        (*ptrptr)->_type = esxVI_Type_##__type;                               \
                                                                              \
        return 0;                                                             \
56 57 58 59 60 61 62 63
    }



#define ESX_VI__TEMPLATE__FREE(_type, _body)                                  \
    void                                                                      \
    esxVI_##_type##_Free(esxVI_##_type **ptrptr)                              \
    {                                                                         \
64
        esxVI_##_type *item ATTRIBUTE_UNUSED;                                 \
65
                                                                              \
66
        if (!ptrptr || !(*ptrptr)) {                                          \
67 68 69 70 71 72 73 74 75 76 77 78
            return;                                                           \
        }                                                                     \
                                                                              \
        item = *ptrptr;                                                       \
                                                                              \
        _body                                                                 \
                                                                              \
        VIR_FREE(*ptrptr);                                                    \
    }



79
#define ESX_VI__TEMPLATE__VALIDATE(__type, _require)                          \
80
    int                                                                       \
81
    esxVI_##__type##_Validate(esxVI_##__type *item)                           \
82
    {                                                                         \
83
        const char *typeName = esxVI_Type_ToString(esxVI_Type_##__type);      \
84 85 86
                                                                              \
        if (item->_type <= esxVI_Type_Undefined ||                            \
            item->_type >= esxVI_Type_Other) {                                \
87 88
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
                           _("%s object has invalid dynamic type"), typeName);\
89 90
            return -1;                                                        \
        }                                                                     \
91 92 93 94 95 96 97 98
                                                                              \
        _require                                                              \
                                                                              \
        return 0;                                                             \
    }



99
#define ESX_VI__TEMPLATE__DEEP_COPY(_type, _deep_copy)                        \
100
    int                                                                       \
101
    esxVI_##_type##_DeepCopy(esxVI_##_type **dest, esxVI_##_type *src)        \
102
    {                                                                         \
103
        if (!dest || *dest) {                                                 \
104 105
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
106 107 108
            return -1;                                                        \
        }                                                                     \
                                                                              \
109
        if (!src) {                                                           \
110 111 112 113 114 115 116 117 118
            return 0;                                                         \
        }                                                                     \
                                                                              \
        if (esxVI_##_type##_Alloc(dest) < 0) {                                \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        _deep_copy                                                            \
                                                                              \
119
        return 0;                                                             \
120 121 122 123 124
                                                                              \
      failure:                                                                \
        esxVI_##_type##_Free(dest);                                           \
                                                                              \
        return -1;                                                            \
125 126 127 128
    }



129 130
#define ESX_VI__TEMPLATE__LIST__APPEND(_type)                                 \
    int                                                                       \
131
    esxVI_##_type##_AppendToList(esxVI_##_type **list,  esxVI_##_type *item)  \
132
    {                                                                         \
133
        return esxVI_List_Append((esxVI_List **)list, (esxVI_List *)item);    \
134 135 136 137 138 139
    }



#define ESX_VI__TEMPLATE__LIST__DEEP_COPY(_type)                              \
    int                                                                       \
140
    esxVI_##_type##_DeepCopyList(esxVI_##_type **destList,                    \
141 142 143
                                 esxVI_##_type *srcList)                      \
    {                                                                         \
        return esxVI_List_DeepCopy                                            \
144
                 ((esxVI_List **)destList, (esxVI_List *)srcList,             \
145 146 147 148 149 150
                  (esxVI_List_DeepCopyFunc)esxVI_##_type##_DeepCopy,          \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



151 152
#define ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(_type)                     \
    int                                                                       \
153
    esxVI_##_type##_CastListFromAnyType(esxVI_AnyType *anyType,               \
154 155 156
                                        esxVI_##_type **list)                 \
    {                                                                         \
        return esxVI_List_CastFromAnyType                                     \
157
                 (anyType, (esxVI_List **)list,                               \
158 159 160 161 162 163 164
                  (esxVI_List_CastFromAnyTypeFunc)                            \
                    esxVI_##_type##_CastFromAnyType,                          \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



165 166
#define ESX_VI__TEMPLATE__LIST__SERIALIZE(_type)                              \
    int                                                                       \
167
    esxVI_##_type##_SerializeList(esxVI_##_type *list, const char *element,   \
168
                                  virBufferPtr output)                        \
169
    {                                                                         \
170
        return esxVI_List_Serialize((esxVI_List *)list, element, output,      \
171 172 173 174 175 176 177 178
                                    (esxVI_List_SerializeFunc)                \
                                      esxVI_##_type##_Serialize);             \
    }



#define ESX_VI__TEMPLATE__LIST__DESERIALIZE(_type)                            \
    int                                                                       \
179
    esxVI_##_type##_DeserializeList(xmlNodePtr node, esxVI_##_type **list)    \
180 181
    {                                                                         \
        return esxVI_List_Deserialize                                         \
182
                 (node, (esxVI_List **)list,                                  \
183 184 185 186 187 188
                  (esxVI_List_DeserializeFunc)esxVI_##_type##_Deserialize,    \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



189 190
#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, _dest_type, _extra, \
                                                   _dest_extra)               \
191
    int                                                                       \
192 193
    esxVI_##_type##_Cast##_dest_extra##FromAnyType(esxVI_AnyType *anyType,    \
                                                   _dest_type **ptrptr)       \
194
    {                                                                         \
195
        _dest_type *item ATTRIBUTE_UNUSED;                                    \
196
                                                                              \
197
        if (!anyType || !ptrptr || *ptrptr) {                                 \
198 199
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
200 201 202
            return -1;                                                        \
        }                                                                     \
                                                                              \
203
        item = *ptrptr;                                                       \
204
                                                                              \
205
        _extra                                                                \
206
                                                                              \
207 208 209 210 211 212 213 214 215 216
        return esxVI_##_type##_Deserialize##_dest_extra(anyType->node,        \
                                                        ptrptr);              \
    }



#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(_type)                           \
    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, esxVI_##_type,          \
    {                                                                         \
        if (anyType->type != esxVI_Type_##_type) {                            \
217
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
218 219 220 221
                           _("Call to %s for unexpected type '%s', "          \
                             "expected '%s'"),                                \
                           __FUNCTION__, anyType->other,                      \
                           esxVI_Type_ToString(esxVI_Type_##_type));          \
222 223
            return -1;                                                        \
        }                                                                     \
224 225 226 227 228 229 230 231
    }, /* nothing */)



#define ESX_VI__TEMPLATE__CAST_VALUE_FROM_ANY_TYPE(_type, _value_type)        \
    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(_type, _value_type,            \
    {                                                                         \
        if (anyType->type != esxVI_Type_##_type) {                            \
232
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
233 234 235 236
                           _("Call to %s for unexpected type '%s', "          \
                             "expected '%s'"),                                \
                           __FUNCTION__, anyType->other,                      \
                           esxVI_Type_ToString(esxVI_Type_##_type));          \
237 238 239
            return -1;                                                        \
        }                                                                     \
    }, Value)
240 241 242



243
#define ESX_VI__TEMPLATE__SERIALIZE_EXTRA(_type, _extra, _serialize)          \
244
    int                                                                       \
245
    esxVI_##_type##_Serialize(esxVI_##_type *item,                            \
246
                              const char *element, virBufferPtr output)       \
247
    {                                                                         \
248
        if (!element || !output) {                                            \
249 250
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
251 252 253
            return -1;                                                        \
        }                                                                     \
                                                                              \
254
        if (!item) {                                                          \
255 256 257
            return 0;                                                         \
        }                                                                     \
                                                                              \
258 259
        _extra                                                                \
                                                                              \
260 261
        if (esxVI_##_type##_Validate(item) < 0) {                             \
            return -1;                                                        \
262 263
        }                                                                     \
                                                                              \
264 265
        ESV_VI__XML_TAG__OPEN(output, element,                                \
                              esxVI_Type_ToString(esxVI_Type_##_type));       \
266 267 268 269 270 271 272 273 274 275 276
                                                                              \
        _serialize                                                            \
                                                                              \
        ESV_VI__XML_TAG__CLOSE(output, element);                              \
                                                                              \
        return 0;                                                             \
    }



#define ESX_VI__TEMPLATE__SERIALIZE(_type, _serialize)                        \
277
    ESX_VI__TEMPLATE__SERIALIZE_EXTRA(_type, /* nothing */, _serialize)
278 279 280



281 282
#define ESX_VI__TEMPLATE__DESERIALIZE_EXTRA(_type, _extra1, _extra2,          \
                                            _deserialize)                     \
283
    int                                                                       \
284
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type **ptrptr)      \
285 286 287
    {                                                                         \
        xmlNodePtr childNode = NULL;                                          \
                                                                              \
288
        _extra1                                                               \
289
                                                                              \
290
        if (!ptrptr || *ptrptr) {                                             \
291 292
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
293 294 295
            return -1;                                                        \
        }                                                                     \
                                                                              \
296
        if (esxVI_##_type##_Alloc(ptrptr) < 0) {                              \
297 298 299
            return -1;                                                        \
        }                                                                     \
                                                                              \
300 301
        _extra2                                                               \
                                                                              \
302
        for (childNode = node->children; childNode;                           \
303 304
             childNode = childNode->next) {                                   \
            if (childNode->type != XML_ELEMENT_NODE) {                        \
305
                virReportError(VIR_ERR_INTERNAL_ERROR,                        \
306 307
                               _("Wrong XML element type %d"),                \
                               childNode->type);                              \
308 309 310 311 312 313 314 315
                goto failure;                                                 \
            }                                                                 \
                                                                              \
            _deserialize                                                      \
                                                                              \
            VIR_WARN("Unexpected '%s' property", childNode->name);            \
        }                                                                     \
                                                                              \
316 317 318
        if (esxVI_##_type##_Validate(*ptrptr) < 0) {                          \
            goto failure;                                                     \
        }                                                                     \
319 320 321 322 323 324 325 326 327 328 329
                                                                              \
        return 0;                                                             \
                                                                              \
      failure:                                                                \
        esxVI_##_type##_Free(ptrptr);                                         \
                                                                              \
        return -1;                                                            \
    }



330
#define ESX_VI__TEMPLATE__DESERIALIZE(_type, _deserialize)                    \
331 332
    ESX_VI__TEMPLATE__DESERIALIZE_EXTRA(_type, /* nothing */, /* nothing */,  \
                                        _deserialize)
333 334 335



336 337
#define ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(_type, _xsdType, _min, _max)     \
    int                                                                       \
338
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type **number)      \
339
    {                                                                         \
M
Matthias Bolte 已提交
340
        int result = -1;                                                      \
341 342 343
        char *string;                                                         \
        long long value;                                                      \
                                                                              \
344
        if (!number || *number) {                                             \
345 346
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
347 348 349
            return -1;                                                        \
        }                                                                     \
                                                                              \
350
        if (esxVI_##_type##_Alloc(number) < 0) {                              \
351 352 353
            return -1;                                                        \
        }                                                                     \
                                                                              \
354
        string = (char *)xmlNodeListGetString(node->doc, node->children, 1);  \
355
                                                                              \
356
        if (!string) {                                                        \
357 358 359
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
                           _("XML node doesn't contain text, expecting an %s "\
                             "value"), _xsdType);                             \
M
Matthias Bolte 已提交
360
            goto cleanup;                                                     \
361 362 363
        }                                                                     \
                                                                              \
        if (virStrToLong_ll(string, NULL, 10, &value) < 0) {                  \
364 365
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
                           _("Unknown value '%s' for %s"), string, _xsdType); \
M
Matthias Bolte 已提交
366
            goto cleanup;                                                     \
367 368
        }                                                                     \
                                                                              \
369 370
        if (((_min) != INT64_MIN && value < (_min))                           \
            || ((_max) != INT64_MAX && value > (_max))) {                     \
371 372 373
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
                           _("Value '%s' is not representable as %s"),        \
                           string, _xsdType);                                 \
M
Matthias Bolte 已提交
374
            goto cleanup;                                                     \
375 376 377 378
        }                                                                     \
                                                                              \
        (*number)->value = value;                                             \
                                                                              \
M
Matthias Bolte 已提交
379 380
        result = 0;                                                           \
                                                                              \
381
      cleanup:                                                                \
M
Matthias Bolte 已提交
382 383 384 385
        if (result < 0) {                                                     \
            esxVI_##_type##_Free(number);                                     \
        }                                                                     \
                                                                              \
386 387 388 389 390 391 392
        VIR_FREE(string);                                                     \
                                                                              \
        return result;                                                        \
    }



393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/*
 * Macros for property handling to be used as part of other macros
 */

#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY(_type, _name)                   \
    if (esxVI_##_type##_DeepCopy(&(*dest)->_name, src->_name) < 0) {          \
        goto failure;                                                         \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_LIST(_type, _name)              \
    if (esxVI_##_type##_DeepCopyList(&(*dest)->_name, src->_name) < 0) {      \
        goto failure;                                                         \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(_type, _name)             \
    if (esxVI_##_type##_DeepCopyValue(&(*dest)->_name, src->_name) < 0) {     \
        goto failure;                                                         \
    }



418 419
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE(_type, _name)                   \
    if (esxVI_##_type##_Serialize(item->_name, #_name, output) < 0) {         \
420 421 422 423 424
        return -1;                                                            \
    }



425 426
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_VALUE(_type, _name)             \
    if (esxVI_##_type##_SerializeValue(item->_name, #_name, output) < 0) {    \
427 428 429 430 431
        return -1;                                                            \
    }



432 433
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_LIST(_type, _name)              \
    if (esxVI_##_type##_SerializeList(item->_name, #_name, output) < 0) {     \
434 435 436 437 438 439 440
        return -1;                                                            \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(_type, _name)                 \
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
441
        if (esxVI_##_type##_Deserialize(childNode, &(*ptrptr)->_name) < 0) {  \
442 443 444 445 446 447 448 449
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



450
#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(_name)                 \
451 452 453 454 455 456
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
        continue;                                                             \
    }



457
#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(_type, _name)           \
458
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
459 460
        if (esxVI_##_type##_DeserializeValue(childNode,                       \
                                             &(*ptrptr)->_name) < 0) {        \
461 462 463 464 465 466 467 468 469 470 471 472
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_LIST(_type, _name)            \
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
        esxVI_##_type *_name##Item = NULL;                                    \
                                                                              \
473
        if (esxVI_##_type##_Deserialize(childNode, &_name##Item) < 0) {       \
474 475 476
            goto failure;                                                     \
        }                                                                     \
                                                                              \
477
        if (esxVI_##_type##_AppendToList(&(*ptrptr)->_name,                   \
478 479 480 481 482 483 484 485 486 487 488 489 490
                                         _name##Item) < 0) {                  \
            esxVI_##_type##_Free(&_name##Item);                               \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



/*
 * A required property must be != 0 (NULL for pointers, "undefined" == 0 for
 * enumeration values).
491 492
 *
 * To be used as part of ESX_VI__TEMPLATE__VALIDATE.
493
 */
494 495
#define ESX_VI__TEMPLATE__PROPERTY__REQUIRE(_name)                            \
    if (item->_name == 0) {                                                   \
496 497 498
        virReportError(VIR_ERR_INTERNAL_ERROR,                                \
                       _("%s object is missing the required '%s' property"),  \
                       typeName, #_name);                                     \
499
        return -1;                                                            \
500 501 502 503
    }



504 505 506 507
/*
 * Macros to implement enumerations
 */

508 509
#define ESX_VI__TEMPLATE__ENUMERATION__CAST_FROM_ANY_TYPE(_type)              \
    int                                                                       \
510
    esxVI_##_type##_CastFromAnyType(esxVI_AnyType *anyType,                   \
511 512 513
                                    esxVI_##_type *value)                     \
    {                                                                         \
        return esxVI_Enumeration_CastFromAnyType                              \
514
                 (&_esxVI_##_type##_Enumeration, anyType, (int *)value);      \
515 516 517 518 519 520
    }



#define ESX_VI__TEMPLATE__ENUMERATION__SERIALIZE(_type)                       \
    int                                                                       \
521
    esxVI_##_type##_Serialize(esxVI_##_type value, const char *element,       \
522
                              virBufferPtr output)                            \
523
    {                                                                         \
524
        return esxVI_Enumeration_Serialize(&_esxVI_##_type##_Enumeration,     \
525
                                           value, element, output);           \
526 527 528 529 530 531
    }



#define ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(_type)                     \
    int                                                                       \
532
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type *value)        \
533
    {                                                                         \
534
        return esxVI_Enumeration_Deserialize(&_esxVI_##_type##_Enumeration,   \
535 536 537 538 539
                                             node, (int *)value);             \
    }



540 541 542 543
/*
 * Macros to implement dynamic dispatched functions
 */

544 545
#define ESX_VI__TEMPLATE__DISPATCH(_actual_type, _actual_type_name, __type,   \
                                   _dispatch,  _error_return)                 \
546
    switch (_actual_type) {                                                   \
547 548 549 550 551 552
      _dispatch                                                               \
                                                                              \
      case esxVI_Type_##__type:                                               \
        break;                                                                \
                                                                              \
      default:                                                                \
553
        virReportError(VIR_ERR_INTERNAL_ERROR,                                \
554 555
                       _("Call to %s for unexpected type '%s'"),              \
                       __FUNCTION__, _actual_type_name);                      \
556 557 558 559 560 561 562 563 564 565 566 567 568 569
        return _error_return;                                                 \
    }



#define ESX_VI__TEMPLATE__DISPATCH__FREE(_type)                               \
    case esxVI_Type_##_type:                                                  \
      esxVI_##_type##_Free((esxVI_##_type **)ptrptr);                         \
      return;



#define ESX_VI__TEMPLATE__DISPATCH__DEEP_COPY(_type)                          \
    case esxVI_Type_##_type:                                                  \
570
      return esxVI_##_type##_DeepCopy((esxVI_##_type **)dest,                 \
571 572 573 574
                                      (esxVI_##_type *)src);



575 576 577 578 579 580 581
#define ESX_VI__TEMPLATE__DISPATCH__CAST_FROM_ANY_TYPE(_type)                 \
    case esxVI_Type_##_type:                                                  \
      return esxVI_##_type##_Deserialize(anyType->node,                       \
                                         (esxVI_##_type **)ptrptr);



582 583 584 585 586 587 588
#define ESX_VI__TEMPLATE__DISPATCH__SERIALIZE(_type)                          \
    case esxVI_Type_##_type:                                                  \
      return esxVI_##_type##_Serialize((esxVI_##_type *)item, element,        \
                                       output);



589 590 591 592 593 594
#define ESX_VI__TEMPLATE__DISPATCH__DESERIALIZE(_type)                        \
    case esxVI_Type_##_type:                                                  \
      return esxVI_##_type##_Deserialize(node, (esxVI_##_type **)ptrptr);



595 596
#define ESX_VI__TEMPLATE__DYNAMIC_FREE(__type, _dispatch, _body)              \
    ESX_VI__TEMPLATE__FREE(__type,                                            \
597 598 599
      ESX_VI__TEMPLATE__DISPATCH(item->_type,                                 \
                                 esxVI_Type_ToString(item->_type),            \
                                 __type, _dispatch,                           \
600
                                 /* nothing */)                               \
601 602 603 604
      _body)



605 606 607 608 609 610 611
#define ESX_VI__TEMPLATE__DYNAMIC_CAST__ACCEPT(__type)                        \
    if (((esxVI_Object *)item)->_type == esxVI_Type_##__type) {               \
        return item;                                                          \
    }



612 613 614 615
#define ESX_VI__TEMPLATE__DYNAMIC_CAST(__type, _accept)                       \
    esxVI_##__type *                                                          \
    esxVI_##__type##_DynamicCast(void *item)                                  \
    {                                                                         \
616
        if (!item) {                                                          \
617 618
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
619 620 621
            return NULL;                                                      \
        }                                                                     \
                                                                              \
622 623
        ESX_VI__TEMPLATE__DYNAMIC_CAST__ACCEPT(__type)                        \
                                                                              \
624 625 626 627 628 629 630
        _accept                                                               \
                                                                              \
        return NULL;                                                          \
    }



631
#define ESX_VI__TEMPLATE__DYNAMIC_DEEP_COPY(__type, _dispatch, _deep_copy)    \
M
Matthias Bolte 已提交
632 633 634
    int                                                                       \
    esxVI_##__type##_DeepCopy(esxVI_##__type **dest, esxVI_##__type *src)     \
    {                                                                         \
635
        if (!dest || *dest) {                                                 \
M
Matthias Bolte 已提交
636 637 638 639 640
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                      \
                           _("Invalid argument"));                            \
            return -1;                                                        \
        }                                                                     \
                                                                              \
641
        if (!src) {                                                           \
M
Matthias Bolte 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
            return 0;                                                         \
        }                                                                     \
                                                                              \
        ESX_VI__TEMPLATE__DISPATCH(src->_type,                                \
                                   esxVI_Type_ToString(src->_type),           \
                                   __type, _dispatch, -1)                     \
                                                                              \
        if (esxVI_##__type##_Alloc(dest) < 0) {                               \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        _deep_copy                                                            \
                                                                              \
        return 0;                                                             \
                                                                              \
      failure:                                                                \
        esxVI_##__type##_Free(dest);                                          \
                                                                              \
        return -1;                                                            \
    }
662 663 664



665 666
#define ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(__type, _dispatch)       \
    ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE_EXTRA(__type, esxVI_##__type,        \
667 668 669
      ESX_VI__TEMPLATE__DISPATCH(anyType->type,                               \
                                 esxVI_AnyType_TypeToString(anyType),         \
                                __type, _dispatch, -1),                       \
670 671 672 673
      /* nothing */)



674 675
#define ESX_VI__TEMPLATE__DYNAMIC_SERIALIZE(__type, _dispatch, _serialize)    \
    ESX_VI__TEMPLATE__SERIALIZE_EXTRA(__type,                                 \
676 677 678
      ESX_VI__TEMPLATE__DISPATCH(item->_type,                                 \
                                 esxVI_Type_ToString(item->_type),            \
                                 __type, _dispatch, -1),                      \
679 680 681 682
      _serialize)



683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
#define ESX_VI__TEMPLATE__DYNAMIC_DESERIALIZE(__type, _dispatch,              \
                                              _deserialize)                   \
    ESX_VI__TEMPLATE__DESERIALIZE_EXTRA(__type,                               \
      esxVI_Type type = esxVI_Type_Undefined;                                 \
                                                                              \
      if (esxVI_GetActualObjectType(node, esxVI_Type_##__type, &type) < 0) {  \
          return -1;                                                          \
      }                                                                       \
                                                                              \
      switch (type) {                                                         \
        _dispatch                                                             \
                                                                              \
        case esxVI_Type_##__type:                                             \
          break;                                                              \
                                                                              \
        default:                                                              \
699 700 701
          virReportError(VIR_ERR_INTERNAL_ERROR,                              \
                         _("Call to %s for unexpected type '%s'"),            \
                         __FUNCTION__, esxVI_Type_ToString(type));            \
702 703
          return -1;                                                          \
      },                                                                      \
704
      /* nothing */,                                                          \
705 706 707 708 709 710 711 712 713 714 715
      _deserialize)



static int
esxVI_GetActualObjectType(xmlNodePtr node, esxVI_Type baseType,
                          esxVI_Type *actualType)
{
    int result = -1;
    char *type = NULL;

716
    if (!actualType || *actualType != esxVI_Type_Undefined) {
717
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
718 719 720 721 722 723 724
        return -1;
    }

    type = (char *)xmlGetNsProp
                     (node, BAD_CAST "type",
                      BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");

725
    if (!type) {
726 727 728
        /* no actual type specified, use base type instead */
        *actualType = baseType;
        return 0;
729 730 731 732
    }

    *actualType = esxVI_Type_FromString(type);

733
    if (*actualType == esxVI_Type_Undefined || *actualType == esxVI_Type_Other) {
734 735 736
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown value '%s' for %s 'type' property"),
                       type, esxVI_Type_ToString(baseType));
737 738 739 740 741
        goto cleanup;
    }

    result = 0;

742
 cleanup:
743 744 745 746 747 748 749
    VIR_FREE(type);

    return result;
}



750 751 752 753 754 755 756 757 758
/*
 * Macros to implement managed objects
 */

#define ESX_VI__TEMPLATE__PROPERTY__MANAGED_REQUIRE(_name)                    \
    /* FIXME: This results in O(n^2) runtime in case of missing required, but \
     * unselected properties. */                                              \
    if (item->_name == 0 &&                                                   \
        esxVI_String_ListContainsValue(selectedPropertyNameList, #_name)) {   \
759 760 761
        virReportError(VIR_ERR_INTERNAL_ERROR,                                \
                       _("%s object is missing the required '%s' property"),  \
                       typeName, #_name);                                     \
762 763 764 765 766 767 768 769 770 771 772 773 774 775
        return -1;                                                            \
    }



#define ESX_VI__TEMPLATE__MANAGED_VALIDATE(__type, _require)                  \
    int                                                                       \
    esxVI_##__type##_Validate(esxVI_##__type *item,                           \
                              esxVI_String *selectedPropertyNameList)         \
    {                                                                         \
        const char *typeName = esxVI_Type_ToString(esxVI_Type_##__type);      \
                                                                              \
        if (item->_type <= esxVI_Type_Undefined ||                            \
            item->_type >= esxVI_Type_Other) {                                \
776 777
            virReportError(VIR_ERR_INTERNAL_ERROR,                            \
                           _("%s object has invalid dynamic type"), typeName);\
778 779 780 781 782 783 784 785 786 787
            return -1;                                                        \
        }                                                                     \
                                                                              \
        _require                                                              \
                                                                              \
        return 0;                                                             \
    }



788 789 790 791 792
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSI: Type
 */

const char *
793
esxVI_Type_ToString(esxVI_Type type)
794 795
{
    switch (type) {
796
      default:
797
      case esxVI_Type_Undefined:
798
        return "<undefined>";
799 800 801 802

      case esxVI_Type_Boolean:
        return "xsd:boolean";

803 804 805
      case esxVI_Type_AnyType:
        return "xsd:anyType";

806 807 808
      case esxVI_Type_String:
        return "xsd:string";

A
Ata E Husain Bohra 已提交
809 810 811
      case esxVI_Type_Byte:
        return "xsd:byte";

812 813 814 815 816 817 818 819 820
      case esxVI_Type_Short:
        return "xsd:short";

      case esxVI_Type_Int:
        return "xsd:int";

      case esxVI_Type_Long:
        return "xsd:long";

821 822
      case esxVI_Type_DateTime:
        return "xsd:dateTime";
823

824 825 826
      case esxVI_Type_Fault:
        return "Fault";

827 828 829
      case esxVI_Type_MethodFault:
        return "MethodFault";

830 831 832
      case esxVI_Type_ManagedObjectReference:
        return "ManagedObjectReference";

833 834 835
      case esxVI_Type_Event:
        return "Event";

836 837 838 839
#include "esx_vi_types.generated.typetostring"

      case esxVI_Type_Other:
        return "<other>";
840 841 842
    }
}

843 844 845
esxVI_Type
esxVI_Type_FromString(const char *type)
{
846
    if (!type || STREQ(type, "<undefined>")) {
847 848 849 850 851 852 853
        return esxVI_Type_Undefined;
    } else if (STREQ(type, "xsd:boolean")) {
        return esxVI_Type_Boolean;
    } else if (STREQ(type, "xsd:anyType")) {
        return esxVI_Type_AnyType;
    } else if (STREQ(type, "xsd:string")) {
        return esxVI_Type_String;
A
Ata E Husain Bohra 已提交
854 855
    } else if (STREQ(type, "xsd:byte")) {
        return esxVI_Type_Byte;
856 857 858 859 860 861 862 863 864 865
    } else if (STREQ(type, "xsd:short")) {
        return esxVI_Type_Short;
    } else if (STREQ(type, "xsd:int")) {
        return esxVI_Type_Int;
    } else if (STREQ(type, "xsd:long")) {
        return esxVI_Type_Long;
    } else if (STREQ(type, "xsd:dateTime")) {
        return esxVI_Type_DateTime;
    } else if (STREQ(type, "Fault")) {
        return esxVI_Type_Fault;
866 867
    } else if (STREQ(type, "MethodFault")) {
        return esxVI_Type_MethodFault;
868 869
    } else if (STREQ(type, "ManagedObjectReference")) {
        return esxVI_Type_ManagedObjectReference;
870 871
    } else if (STREQ(type, "Event")) {
        return esxVI_Type_Event;
872 873 874 875
    }

#include "esx_vi_types.generated.typefromstring"

876
    return esxVI_Type_Other;
877
}
878 879 880 881 882 883 884


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Boolean
 */

static const esxVI_Enumeration _esxVI_Boolean_Enumeration = {
885
    esxVI_Type_Boolean, {
886 887 888 889 890 891 892
        { "true", esxVI_Boolean_True },
        { "false", esxVI_Boolean_False },
        { NULL, -1 },
    },
};

/* esxVI_Boolean_Serialize */
893
ESX_VI__TEMPLATE__ENUMERATION__SERIALIZE(Boolean)
894 895

/* esxVI_Boolean_Deserialize */
896
ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(Boolean)
897 898 899 900 901 902 903 904



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: AnyType
 */

/* esxVI_AnyType_Alloc */
905
ESX_VI__TEMPLATE__ALLOC(AnyType)
906 907 908 909

/* esxVI_AnyType_Free */
ESX_VI__TEMPLATE__FREE(AnyType,
{
910
    xmlFreeNode(item->node);
911 912
    VIR_FREE(item->other);
    VIR_FREE(item->value);
913
})
914

915 916 917 918 919 920 921 922 923 924
const char *
esxVI_AnyType_TypeToString(esxVI_AnyType *anyType)
{
    if (anyType->type == esxVI_Type_Other) {
        return anyType->other;
    } else {
        return esxVI_Type_ToString(anyType->type);
    }
}

925
int
926
esxVI_AnyType_ExpectType(esxVI_AnyType *anyType, esxVI_Type type)
927 928
{
    if (anyType->type != type) {
929 930 931
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting type '%s' but found '%s'"),
                       esxVI_Type_ToString(type),
932
                       esxVI_AnyType_TypeToString(anyType));
933 934 935 936 937 938 939
        return -1;
    }

    return 0;
}

int
940
esxVI_AnyType_DeepCopy(esxVI_AnyType **dest, esxVI_AnyType *src)
941
{
942
    if (!dest || *dest) {
943
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
944 945 946
        return -1;
    }

947
    if (!src)
948 949
        return 0;

950
    if (esxVI_AnyType_Alloc(dest) < 0)
951 952
        goto failure;

953 954
    (*dest)->_type = src->_type;
    (*dest)->node = xmlCopyNode(src->node, 1);
955

956
    if (!(*dest)->node) {
957 958
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not copy an XML node"));
959 960 961 962 963
        goto failure;
    }

    (*dest)->type = src->type;

964 965
    if (VIR_STRDUP((*dest)->other, src->other) < 0 ||
        VIR_STRDUP((*dest)->value, src->value) < 0) {
966 967 968 969 970 971 972 973 974 975 976 977
        goto failure;
    }

    switch (src->type) {
      case esxVI_Type_Boolean:
        (*dest)->boolean = src->boolean;
        break;

      case esxVI_Type_String:
        (*dest)->string = (*dest)->value;
        break;

A
Ata E Husain Bohra 已提交
978 979 980 981
      case esxVI_Type_Byte:
        (*dest)->int8 = src->int8;
        break;

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
      case esxVI_Type_Short:
        (*dest)->int16 = src->int16;
        break;

      case esxVI_Type_Int:
        (*dest)->int32 = src->int32;
        break;

      case esxVI_Type_Long:
        (*dest)->int64 = src->int64;
        break;

      default:
        break;
    }

    return 0;

1000
 failure:
1001 1002 1003 1004 1005 1006
    esxVI_AnyType_Free(dest);

    return -1;
}

int
1007
esxVI_AnyType_Deserialize(xmlNodePtr node, esxVI_AnyType **anyType)
1008
{
1009
    long long int number;
1010

1011
    if (!anyType || *anyType) {
1012
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1013 1014 1015
        return -1;
    }

1016
    if (esxVI_AnyType_Alloc(anyType) < 0)
1017 1018
        return -1;

1019
    (*anyType)->node = xmlCopyNode(node, 1);
1020

1021
    if (!(*anyType)->node) {
1022 1023
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not copy an XML node"));
1024 1025 1026 1027 1028 1029 1030 1031
        goto failure;
    }

    (*anyType)->other =
      (char *)xmlGetNsProp
                (node, BAD_CAST "type",
                 BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");

1032
    if (!(*anyType)->other) {
1033 1034
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("AnyType is missing 'type' property"));
1035 1036 1037
        goto failure;
    }

1038 1039 1040
    (*anyType)->type = esxVI_Type_FromString((*anyType)->other);

    if ((*anyType)->type == esxVI_Type_Undefined) {
1041 1042 1043
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown value '%s' for AnyType 'type' property"),
                       (*anyType)->other);
1044 1045 1046
        goto failure;
    }

1047
    (*anyType)->value =
1048
      (char *)xmlNodeListGetString(node->doc, node->children, 1);
1049

1050 1051
    if (!(*anyType)->value && VIR_STRDUP((*anyType)->value, "") < 0)
        goto failure;
1052

1053
#define _DESERIALIZE_NUMBER(_type, _xsdType, _name, _min, _max)               \
1054 1055
        do {                                                                  \
            if (virStrToLong_ll((*anyType)->value, NULL, 10, &number) < 0) {  \
1056 1057 1058
                virReportError(VIR_ERR_INTERNAL_ERROR,                        \
                               _("Unknown value '%s' for %s"),                \
                               (*anyType)->value, _xsdType);                  \
1059 1060 1061
                goto failure;                                                 \
            }                                                                 \
                                                                              \
1062 1063
            if (((_min) != INT64_MIN && number < (_min))                      \
                || ((_max) != INT64_MAX && number > (_max))) {                \
1064 1065 1066
                virReportError(VIR_ERR_INTERNAL_ERROR,                        \
                               _("Value '%s' is out of %s range"),            \
                               (*anyType)->value, _xsdType);                  \
1067 1068 1069 1070 1071 1072
                goto failure;                                                 \
            }                                                                 \
                                                                              \
            (*anyType)->_name = number;                                       \
        } while (0)

1073 1074
    switch ((*anyType)->type) {
      case esxVI_Type_Boolean:
1075 1076 1077 1078 1079
        if (STREQ((*anyType)->value, "true")) {
            (*anyType)->boolean = esxVI_Boolean_True;
        } else if (STREQ((*anyType)->value, "false")) {
            (*anyType)->boolean = esxVI_Boolean_False;
        } else {
1080 1081 1082
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown value '%s' for xsd:boolean"),
                           (*anyType)->value);
1083 1084
            goto failure;
        }
1085 1086 1087 1088

        break;

      case esxVI_Type_String:
1089
        (*anyType)->string = (*anyType)->value;
1090 1091
        break;

A
Ata E Husain Bohra 已提交
1092 1093 1094 1095
      case esxVI_Type_Byte:
        _DESERIALIZE_NUMBER(Byte, "xsd:byte", int8, INT8_MIN, INT8_MAX);
        break;

1096
      case esxVI_Type_Short:
1097
        _DESERIALIZE_NUMBER(Short, "xsd:short", int16, INT16_MIN, INT16_MAX);
1098 1099 1100
        break;

      case esxVI_Type_Int:
1101
        _DESERIALIZE_NUMBER(Int, "xsd:int", int32, INT32_MIN, INT32_MAX);
1102 1103 1104
        break;

      case esxVI_Type_Long:
1105
        _DESERIALIZE_NUMBER(Long, "xsd:long", int64, INT64_MIN, INT64_MAX);
1106 1107 1108 1109
        break;

      default:
        break;
1110 1111
    }

1112
#undef _DESERIALIZE_NUMBER
1113 1114 1115

    return 0;

1116
 failure:
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    esxVI_AnyType_Free(anyType);

    return -1;
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: String
 */

/* esxVI_String_Alloc */
1129
ESX_VI__TEMPLATE__ALLOC(String)
1130 1131 1132 1133 1134 1135 1136

/* esxVI_String_Free */
ESX_VI__TEMPLATE__FREE(String,
{
    esxVI_String_Free(&item->_next);

    VIR_FREE(item->value);
1137
})
1138

1139 1140 1141 1142 1143 1144
/* esxVI_String_Validate */
ESX_VI__TEMPLATE__VALIDATE(String,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(value)
})

1145 1146 1147 1148 1149
bool
esxVI_String_ListContainsValue(esxVI_String *stringList, const char *value)
{
    esxVI_String *string;

1150
    for (string = stringList; string; string = string->_next) {
1151
        if (STREQ(string->value, value))
1152 1153 1154 1155 1156 1157
            return true;
    }

    return false;
}

1158
/* esxVI_String_AppendToList */
1159
ESX_VI__TEMPLATE__LIST__APPEND(String)
1160 1161

int
1162
esxVI_String_AppendValueToList(esxVI_String **stringList, const char *value)
1163 1164 1165
{
    esxVI_String *string = NULL;

1166
    if (esxVI_String_Alloc(&string) < 0)
M
Matthias Bolte 已提交
1167
        return -1;
1168

1169
    if (VIR_STRDUP(string->value, value) < 0)
1170 1171
        goto failure;

1172
    if (esxVI_String_AppendToList(stringList, string) < 0)
1173 1174 1175 1176
        goto failure;

    return 0;

1177
 failure:
1178 1179 1180 1181 1182 1183
    esxVI_String_Free(&string);

    return -1;
}

int
1184
esxVI_String_AppendValueListToList(esxVI_String **stringList,
1185 1186 1187 1188 1189
                                   const char *valueList)
{
    esxVI_String *stringListToAppend = NULL;
    const char *value = valueList;

1190
    while (value && *value != '\0') {
1191
        if (esxVI_String_AppendValueToList(&stringListToAppend, value) < 0)
1192 1193 1194 1195 1196
            goto failure;

        value += strlen(value) + 1;
    }

1197
    if (esxVI_String_AppendToList(stringList, stringListToAppend) < 0)
1198 1199 1200 1201
        goto failure;

    return 0;

1202
 failure:
1203 1204 1205 1206 1207
    esxVI_String_Free(&stringListToAppend);

    return -1;
}

1208 1209
/* esxVI_String_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(String,
1210
{
1211 1212
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})
1213 1214

/* esxVI_String_DeepCopyList */
1215
ESX_VI__TEMPLATE__LIST__DEEP_COPY(String)
1216 1217

int
1218
esxVI_String_DeepCopyValue(char **dest, const char *src)
1219
{
1220
    if (!dest || *dest) {
1221
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1222 1223 1224
        return -1;
    }

1225
    if (!src)
1226 1227
        return 0;

1228
    return VIR_STRDUP(*dest, src);
1229 1230
}

1231 1232 1233 1234 1235 1236
/* esxVI_String_CastFromAnyType */
ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(String)

/* esxVI_String_CastValueFromAnyType */
ESX_VI__TEMPLATE__CAST_VALUE_FROM_ANY_TYPE(String, char)

1237
int
1238
esxVI_String_Serialize(esxVI_String *string, const char *element,
1239
                       virBufferPtr output)
1240
{
1241
    return esxVI_String_SerializeValue(string ? string->value : NULL,
1242
                                       element, output);
1243 1244 1245
}

/* esxVI_String_SerializeList */
1246
ESX_VI__TEMPLATE__LIST__SERIALIZE(String)
1247 1248

int
1249
esxVI_String_SerializeValue(const char *value, const char *element,
1250
                            virBufferPtr output)
1251
{
1252
    if (!element || !output) {
1253
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1254 1255 1256
        return -1;
    }

1257
    if (!value)
1258
        return 0;
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

    ESV_VI__XML_TAG__OPEN(output, element, "xsd:string");

    virBufferAdd(output, value, -1);

    ESV_VI__XML_TAG__CLOSE(output, element);

    return 0;
}

1269 1270
int
esxVI_String_Deserialize(xmlNodePtr node, esxVI_String **string)
1271
{
1272
    if (!string || *string) {
1273
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
        return -1;
    }

    if (esxVI_String_Alloc(string) < 0 ||
        esxVI_String_DeserializeValue(node, &(*string)->value) < 0) {
        goto failure;
    }

    return 0;

1284
 failure:
1285 1286 1287 1288
    esxVI_String_Free(string);

    return -1;
}
1289 1290

/* esxVI_String_DeserializeList */
1291
ESX_VI__TEMPLATE__LIST__DESERIALIZE(String)
1292 1293

int
1294
esxVI_String_DeserializeValue(xmlNodePtr node, char **value)
1295
{
1296
    if (!value || *value) {
1297
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1298 1299 1300
        return -1;
    }

1301
    *value = (char *)xmlNodeListGetString(node->doc, node->children, 1);
1302

1303
    return *value ? 0 : VIR_STRDUP(*value, "");
1304 1305 1306 1307
}



A
Ata E Husain Bohra 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Byte
 */

/* esxVI_Byte_Alloc */
ESX_VI__TEMPLATE__ALLOC(Byte)

/* esxVI_Byte_Free */
ESX_VI__TEMPLATE__FREE(Byte,
{
    esxVI_Byte_Free(&item->_next);
})

/* esxVI_Byte_Validate */
ESX_VI__TEMPLATE__VALIDATE(Byte,
{
})

/* esxVI_Byte_AppendToList */
ESX_VI__TEMPLATE__LIST__APPEND(Byte)

/* esxVI_Byte_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(Byte,
{
    (*dest)->value = src->value;
})

/* esxVI_Byte_DeepCopyList */
ESX_VI__TEMPLATE__LIST__DEEP_COPY(Byte)

/* esxVI_Byte_Serialize */
ESX_VI__TEMPLATE__SERIALIZE(Byte,
{
    virBufferAsprintf(output, "%d", (int)item->value);
})

/* esxVI_Byte_SerializeList */
ESX_VI__TEMPLATE__LIST__SERIALIZE(Byte)

/* esxVI_Byte_Deserialize */
ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(Byte, "xsd:byte", INT8_MIN, INT8_MAX);



1352 1353 1354 1355 1356
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Int
 */

/* esxVI_Int_Alloc */
1357
ESX_VI__TEMPLATE__ALLOC(Int)
1358 1359 1360 1361 1362

/* esxVI_Int_Free */
ESX_VI__TEMPLATE__FREE(Int,
{
    esxVI_Int_Free(&item->_next);
1363
})
1364

1365
/* esxVI_Int_Validate */
1366 1367 1368
ESX_VI__TEMPLATE__VALIDATE(Int,
{
})
1369

1370
/* esxVI_Int_AppendToList */
1371
ESX_VI__TEMPLATE__LIST__APPEND(Int)
1372

1373 1374
/* esxVI_Int_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(Int,
1375 1376
{
    (*dest)->value = src->value;
1377
})
1378 1379

/* esxVI_Int_Serialize */
1380
ESX_VI__TEMPLATE__SERIALIZE(Int,
1381
{
1382
    virBufferAsprintf(output, "%d", (int)item->value);
1383
})
1384 1385

/* esxVI_Int_SerializeList */
1386
ESX_VI__TEMPLATE__LIST__SERIALIZE(Int)
1387 1388

/* esxVI_Int_Deserialize */
1389
ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(Int, "xsd:int", INT32_MIN, INT32_MAX)
1390 1391 1392 1393 1394 1395 1396 1397



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Long
 */

/* esxVI_Long_Alloc */
1398
ESX_VI__TEMPLATE__ALLOC(Long)
1399 1400 1401 1402 1403

/* esxVI_Long_Free */
ESX_VI__TEMPLATE__FREE(Long,
{
    esxVI_Long_Free(&item->_next);
1404
})
1405

1406
/* esxVI_Long_Validate */
1407 1408 1409
ESX_VI__TEMPLATE__VALIDATE(Long,
{
})
1410

1411
/* esxVI_Long_AppendToList */
1412
ESX_VI__TEMPLATE__LIST__APPEND(Long)
1413

1414 1415 1416 1417 1418 1419
/* esxVI_Long_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(Long,
{
    (*dest)->value = src->value;
})

1420
/* esxVI_Long_CastFromAnyType */
1421
ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(Long)
1422

1423
/* esxVI_Long_Serialize */
1424
ESX_VI__TEMPLATE__SERIALIZE(Long,
1425
{
1426
    virBufferAsprintf(output, "%lld", (long long int)item->value);
1427
})
1428 1429

/* esxVI_Long_SerializeList */
1430
ESX_VI__TEMPLATE__LIST__SERIALIZE(Long)
1431 1432

/* esxVI_Long_Deserialize */
1433
ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(Long, "xsd:long", INT64_MIN, INT64_MAX)
1434 1435 1436 1437 1438 1439 1440 1441



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: DateTime
 */

/* esxVI_DateTime_Alloc */
1442
ESX_VI__TEMPLATE__ALLOC(DateTime)
1443 1444 1445 1446 1447

/* esxVI_DateTime_Free */
ESX_VI__TEMPLATE__FREE(DateTime,
{
    VIR_FREE(item->value);
1448
})
1449

1450 1451 1452 1453
/* esxVI_DateTime_Validate */
ESX_VI__TEMPLATE__VALIDATE(DateTime,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(value);
1454
})
1455

1456 1457 1458 1459 1460 1461
/* esxVI_DateTime_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(DateTime,
{
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})

1462
/* esxVI_DateTime_Serialize */
1463
ESX_VI__TEMPLATE__SERIALIZE(DateTime,
1464 1465
{
    virBufferAdd(output, item->value, -1);
1466
})
1467 1468

int
1469
esxVI_DateTime_Deserialize(xmlNodePtr node, esxVI_DateTime **dateTime)
1470
{
1471
    if (!dateTime || *dateTime) {
1472
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1473 1474 1475
        return -1;
    }

1476
    if (esxVI_DateTime_Alloc(dateTime) < 0)
1477 1478 1479
        return -1;

    (*dateTime)->value =
1480
      (char *)xmlNodeListGetString(node->doc, node->children, 1);
1481

1482
    if (!(*dateTime)->value) {
1483 1484 1485
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("XML node doesn't contain text, expecting an "
                         "xsd:dateTime value"));
1486 1487 1488 1489 1490
        goto failure;
    }

    return 0;

1491
 failure:
1492 1493 1494 1495 1496
    esxVI_DateTime_Free(dateTime);

    return -1;
}

1497 1498
int
esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
1499
                                     long long *secondsSinceEpoch)
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
{
    char value[64] = "";
    char *tmp;
    struct tm tm;
    int milliseconds;
    char sign;
    int tz_hours;
    int tz_minutes;
    int tz_offset = 0;

1510
    if (!dateTime || !secondsSinceEpoch) {
1511
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1512 1513 1514
        return -1;
    }

1515
    if (!virStrcpyStatic(value, dateTime->value)) {
1516 1517 1518
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("xsd:dateTime value '%s' too long for destination"),
                       dateTime->value);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
        return -1;
    }

    /*
     * expected format: [-]CCYY-MM-DDTHH:MM:SS[.ssssss][((+|-)HH:MM|Z)]
     * typical example: 2010-04-05T12:13:55.316789+02:00
     *
     * see http://www.w3.org/TR/xmlschema-2/#dateTime
     *
     * map negative years to 0, since the base for time_t is the year 1970.
     */
    if (*value == '-') {
        *secondsSinceEpoch = 0;
        return 0;
    }

    tmp = strptime(value, "%Y-%m-%dT%H:%M:%S", &tm);

1537
    if (!tmp) {
1538 1539 1540
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("xsd:dateTime value '%s' has unexpected format"),
                       dateTime->value);
1541 1542 1543 1544 1545 1546 1547
        return -1;
    }

    if (*tmp != '\0') {
        /* skip .ssssss part if present */
        if (*tmp == '.' &&
            virStrToLong_i(tmp + 1, &tmp, 10, &milliseconds) < 0) {
1548 1549 1550
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("xsd:dateTime value '%s' has unexpected format"),
                           dateTime->value);
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
            return -1;
        }

        /* parse timezone offset if present. if missing assume UTC */
        if (*tmp == '+' || *tmp == '-') {
            sign = *tmp;

            if (virStrToLong_i(tmp + 1, &tmp, 10, &tz_hours) < 0 ||
                *tmp != ':' ||
                virStrToLong_i(tmp + 1, NULL, 10, &tz_minutes) < 0) {
1561 1562 1563
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("xsd:dateTime value '%s' has unexpected format"),
                               dateTime->value);
1564 1565 1566 1567 1568
                return -1;
            }

            tz_offset = tz_hours * 60 * 60 + tz_minutes * 60;

1569
            if (sign == '-')
1570 1571 1572 1573
                tz_offset = -tz_offset;
        } else if (STREQ(tmp, "Z")) {
            /* Z refers to UTC. tz_offset is already initialized to zero */
        } else {
1574 1575 1576
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("xsd:dateTime value '%s' has unexpected format"),
                           dateTime->value);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
            return -1;
        }
    }

    /*
     * xsd:dateTime represents local time relative to the optional timezone
     * given as offset. pretend the local time is in UTC and use timegm in
     * order to avoid interference with the timezone to this computer.
     * apply timezone correction afterwards, because it's simpler than
     * handling all the possible over- and underflows when trying to apply
     * it to the tm struct.
     */
    *secondsSinceEpoch = timegm(&tm) - tz_offset;

    return 0;
}

1594 1595


1596
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1597
 * SOAP: Fault
1598 1599
 */

1600 1601
/* esxVI_Fault_Alloc */
ESX_VI__TEMPLATE__ALLOC(Fault);
1602

1603 1604 1605 1606 1607
/* esxVI_Fault_Free */
ESX_VI__TEMPLATE__FREE(Fault,
{
    VIR_FREE(item->faultcode);
    VIR_FREE(item->faultstring);
1608
})
1609

1610 1611 1612 1613 1614
/* esxVI_Fault_Validate */
ESX_VI__TEMPLATE__VALIDATE(Fault,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(faultcode);
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(faultstring);
1615
})
1616

1617 1618 1619 1620 1621 1622
/* esxVI_Fault_Deserialize */
ESX_VI__TEMPLATE__DESERIALIZE(Fault,
{
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, faultcode);
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, faultstring);
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(detail); /* FIXME */
1623
})
1624 1625 1626



1627
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1628
 * VI Object: MethodFault
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
 */

/* esxVI_MethodFault_Alloc */
ESX_VI__TEMPLATE__ALLOC(MethodFault);

/* esxVI_MethodFault_Free */
ESX_VI__TEMPLATE__FREE(MethodFault,
{
    VIR_FREE(item->_actualType);
})

int
esxVI_MethodFault_Deserialize(xmlNodePtr node, esxVI_MethodFault **methodFault)
{
1643
    if (!methodFault || *methodFault) {
1644
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1645 1646 1647
        return -1;
    }

1648
    if (esxVI_MethodFault_Alloc(methodFault) < 0)
1649 1650 1651 1652 1653 1654
        return -1;

    (*methodFault)->_actualType =
      (char *)xmlGetNsProp(node, BAD_CAST "type",
                           BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");

1655
    if (!(*methodFault)->_actualType) {
1656 1657
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("MethodFault is missing 'type' property"));
1658 1659 1660 1661 1662
        goto failure;
    }

    return 0;

1663
 failure:
1664 1665 1666 1667 1668 1669 1670
    esxVI_MethodFault_Free(methodFault);

    return -1;
}



1671
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1672
 * VI Object: ManagedObjectReference
1673 1674
 */

1675
/* esxVI_ManagedObjectReference_Alloc */
1676
ESX_VI__TEMPLATE__ALLOC(ManagedObjectReference)
1677

1678 1679 1680 1681
/* esxVI_ManagedObjectReference_Free */
ESX_VI__TEMPLATE__FREE(ManagedObjectReference,
{
    esxVI_ManagedObjectReference_Free(&item->_next);
1682

1683 1684
    VIR_FREE(item->type);
    VIR_FREE(item->value);
1685
})
1686

1687 1688 1689 1690 1691 1692
/* esxVI_ManagedObjectReference_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(ManagedObjectReference,
{
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, type)
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})
1693

1694
/* esxVI_ManagedObjectReference_AppendToList */
1695
ESX_VI__TEMPLATE__LIST__APPEND(ManagedObjectReference)
1696

1697
/* esxVI_ManagedObjectReference_CastFromAnyType */
1698
ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(ManagedObjectReference)
1699

1700 1701
/* esxVI_ManagedObjectReference_CastListFromAnyType */
ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(ManagedObjectReference)
1702

1703 1704
int
esxVI_ManagedObjectReference_Serialize
1705
  (esxVI_ManagedObjectReference *managedObjectReference,
1706
   const char *element, virBufferPtr output)
1707
{
1708
    if (!element || !output) {
1709
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1710 1711 1712
        return -1;
    }

1713
    if (!managedObjectReference)
1714
        return 0;
1715 1716 1717

    virBufferAddLit(output, "<");
    virBufferAdd(output, element, -1);
1718
    virBufferAsprintf(output,
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
                      " xmlns=\"urn:vim25\" "
                      "xsi:type=\"ManagedObjectReference\" type=\"%s\">",
                      managedObjectReference->type);

    virBufferAdd(output, managedObjectReference->value, -1);

    ESV_VI__XML_TAG__CLOSE(output, element);

    return 0;
}

/* esxVI_ManagedObjectReference_SerializeList */
1731
ESX_VI__TEMPLATE__LIST__SERIALIZE(ManagedObjectReference)
1732 1733 1734

int
esxVI_ManagedObjectReference_Deserialize
1735
  (xmlNodePtr node, esxVI_ManagedObjectReference **managedObjectReference)
1736
{
1737
    if (!managedObjectReference || *managedObjectReference) {
1738
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1739 1740 1741
        return -1;
    }

1742
    if (esxVI_ManagedObjectReference_Alloc(managedObjectReference) < 0)
1743 1744 1745 1746 1747
        return -1;

    (*managedObjectReference)->type =
      (char *)xmlGetNoNsProp(node, BAD_CAST "type");

1748
    if (!(*managedObjectReference)->type) {
1749 1750
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("ManagedObjectReference is missing 'type' property"));
1751 1752 1753
        goto failure;
    }

1754
    if (esxVI_String_DeserializeValue(node,
1755 1756 1757 1758 1759 1760
                                      &(*managedObjectReference)->value) < 0) {
        goto failure;
    }

    return 0;

1761
 failure:
1762 1763 1764 1765 1766
    esxVI_ManagedObjectReference_Free(managedObjectReference);

    return -1;
}

1767 1768


1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VI Type: Event
 */

/* esxVI_Event_Alloc */
ESX_VI__TEMPLATE__ALLOC(Event)

/* esxVI_Event_Free */
ESX_VI__TEMPLATE__FREE(Event,
{
    esxVI_Event_Free(&item->_next);
    VIR_FREE(item->_actualType);

    esxVI_Int_Free(&item->key);
    esxVI_Int_Free(&item->chainId);
    esxVI_DateTime_Free(&item->createdTime);
    VIR_FREE(item->userName);
    /* FIXME: datacenter is currently ignored */
    /* FIXME: computeResource is currently ignored */
    /* FIXME: host is currently ignored */
    esxVI_VmEventArgument_Free(&item->vm);
    VIR_FREE(item->fullFormattedMessage);
})

/* esxVI_Event_Validate */
ESX_VI__TEMPLATE__VALIDATE(Event,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(key)
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(chainId)
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(createdTime)
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(userName)
    /* FIXME: datacenter is currently ignored */
    /* FIXME: computeResource is currently ignored */
    /* FIXME: host is currently ignored */
})

/* esxVI_Event_AppendToList */
ESX_VI__TEMPLATE__LIST__APPEND(Event)

/* esxVI_Event_CastFromAnyType */
ESX_VI__TEMPLATE__DYNAMIC_CAST_FROM_ANY_TYPE(Event,
{
      case esxVI_Type_Other:
        /* Just accept everything here */
        break;
})

/* esxVI_Event_CastListFromAnyType */
ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(Event)

/* esxVI_Event_Deserialize */
ESX_VI__TEMPLATE__DESERIALIZE_EXTRA(Event, /* nothing */,
{
    (*ptrptr)->_actualType =
      (char *)xmlGetNsProp(node, BAD_CAST "type",
                           BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");

1826
    if (!(*ptrptr)->_actualType) {
1827 1828 1829
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("%s is missing 'type' property"),
                       esxVI_Type_ToString((*ptrptr)->_type));
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
        goto failure;
    }
},
{
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(Int, key)
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(Int, chainId)
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(DateTime, createdTime)
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, userName)
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(datacenter) /* FIXME */
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(computeResource) /* FIXME */
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(host) /* FIXME */
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(VmEventArgument, vm)
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, fullFormattedMessage)

    /* Don't warn about unexpected properties */
    continue;
})

/* esxVI_Event_DeserializeList */
ESX_VI__TEMPLATE__LIST__DESERIALIZE(Event)



1853
#include "esx_vi_types.generated.c"
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VI Enum: VirtualMachinePowerState (Additions)
 */

int
esxVI_VirtualMachinePowerState_ConvertToLibvirt
  (esxVI_VirtualMachinePowerState powerState)
{
    switch (powerState) {
      case esxVI_VirtualMachinePowerState_PoweredOff:
        return VIR_DOMAIN_SHUTOFF;

      case esxVI_VirtualMachinePowerState_PoweredOn:
        return VIR_DOMAIN_RUNNING;

      case esxVI_VirtualMachinePowerState_Suspended:
        return VIR_DOMAIN_PAUSED;

      default:
        return VIR_DOMAIN_NOSTATE;
    }
}