os-cmpi-xen

view src/Xen_ConsoleSettingData_Resource.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents 315d491aec5f
children
line source
1 // Copyright (C) 2007 Novell, Inc
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Authors: Jim Fehlig <jfehlig@novell.com>
18 //
19 // Contributors:
20 //
21 // Description:
22 // ============================================================================
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
28 #include <xen_common.h>
29 #include <xen_vm.h>
30 #include <xen_console.h>
32 #include "Xen_ConsoleSettingData_Resource.h"
34 /* Include the required CMPI data types, function headers, and macros. */
35 #include "cmpidt.h"
36 #include "cmpift.h"
37 #include "cmpimacs.h"
39 #include "xen_utils.h"
40 #include "provider_common.h"
42 /* Include _SBLIM_TRACE() logging support */
43 #include "cmpitrace.h"
45 // ----------------------------------------------------------------------------
47 static int concat_console_lists(xen_console_set **target, xen_console_set *source)
48 {
50 int total_size;
51 int i, j;
53 /* Nothing to concatenate if source is empty */
54 if (source == NULL || source->size == 0)
55 return 1;
57 /* If targe is empty, assign source to target */
58 if (*target == NULL) {
59 *target = source;
60 return 1;
61 }
63 /* realloc memory and append source to target */
64 total_size = (*target)->size + source->size;
65 *target = realloc(*target, sizeof(xen_console_set) + (total_size * sizeof(xen_console)));
66 if (*target == NULL)
67 return 0;
69 for (j = (*target)->size, i = 0; i < source->size; i++) {
70 (*target)->contents[j + i] = source->contents[i];
71 source->contents[i] = NULL;
72 }
73 (*target)->size = total_size;
75 /* Free source list - it has been copied to target */
76 xen_console_set_free(source);
77 return 1;
78 }
81 /* Get a handle to the list of all system resources for this class. */
82 int Xen_ConsoleSettingData_getResources(xen_utils_session *session,
83 _RESOURCES ** resources)
84 {
85 xen_domain_resources *res;
86 xen_console_set *all_consoles = NULL;
87 xen_vm_record *vm_rec = NULL;
89 /* malloc a new handle for the resources list. */
90 *resources = (_RESOURCES *)calloc(1, sizeof(_RESOURCES));
91 if (*resources == NULL)
92 return 0;
94 /* Get a list of domain resources. */
95 if (!xen_utils_get_domain_resources(session, &res)) {
96 *resources = NULL;
97 return 0;
98 }
100 /* Create list of console resources from consoles found in each domain. */
101 while (xen_utils_get_next_domain_resource(session, res, &vm_rec)) {
102 xen_console_set *console_set;
103 if (!xen_vm_get_consoles(session->xen, &console_set, vm_rec->handle))
104 goto Error;
106 if (!concat_console_lists(&all_consoles, console_set))
107 goto Error;
109 xen_vm_record_free(vm_rec);
110 }
112 (*resources)->consoles = all_consoles;
113 (*resources)->currentconsolenum = 0;
115 return 1;
117 Error:
118 xen_vm_record_free(vm_rec);
119 xen_utils_free_domain_resources(res);
120 free(*resources);
121 *resources = NULL;
122 return 0;
123 }
125 // ----------------------------------------------------------------------------
127 /* Free/deallocate/cleanup the resources list after use. */
128 int Xen_ConsoleSettingData_freeResources(_RESOURCES * resources)
129 {
130 if (resources != NULL) {
131 if (resources->consoles != NULL)
132 xen_console_set_free(resources->consoles);
133 free(resources);
134 resources = NULL;
135 }
137 return 1;
138 }
140 // ----------------------------------------------------------------------------
142 /* Iterator to get the next resource from the resources list. */
143 int Xen_ConsoleSettingData_getNextResource(xen_utils_session *session,
144 _RESOURCES * resources,
145 _RESOURCE ** resource)
146 {
147 if (resources == NULL || resources->consoles == NULL)
148 return 0;
150 /* Check if reached the end of the list of consoles. */
151 if (resources->currentconsolenum == resources->consoles->size)
152 return 0;
154 /* Get the current console record. */
155 if (!xen_console_get_record(session->xen, resource, resources->consoles->contents[resources->currentconsolenum]))
156 return 0;
158 resources->currentconsolenum++;
159 return 1;
160 }
162 // ----------------------------------------------------------------------------
164 /* Get the specific resource that matches the CMPI object path. */
165 int Xen_ConsoleSettingData_getResourceForObjectPath(xen_utils_session *session,
166 _RESOURCE ** resource,
167 const CMPIObjectPath * objectpath)
168 {
169 char *instId;
170 char uuid[MAX_SYSTEM_NAME_LEN];
171 int ccode;
172 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
174 if (CMIsNullObject(objectpath)) return 0;
176 /* Obtain the target resource identifier from the CMPIObjectPath key(s). */
177 CMPIData iddata = CMGetKey(objectpath, "InstanceID", &status);
178 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(iddata)) return 0;
180 instId = CMGetCharPtr(iddata.value.string);
181 if ((instId == NULL) || (*instId == '\0')) return 0;
183 /* Extract the console uuid from InstanceID property. */
184 if (!_CMPIStrncpyDeviceNameFromID(uuid, instId, MAX_SYSTEM_NAME_LEN))
185 return 0;
187 xen_console console;
188 if (!xen_console_get_by_uuid(session->xen, &console, uuid))
189 return 0;
191 ccode = xen_console_get_record(session->xen, resource, console);
192 xen_console_free(console);
193 return ccode;
194 }
196 // ----------------------------------------------------------------------------
198 /* Free/deallocate/cleanup the resource after use. */
199 int Xen_ConsoleSettingData_freeResource(_RESOURCE * resource)
200 {
201 xen_console_record_free(resource);
202 return 1;
203 }
205 // ----------------------------------------------------------------------------
207 /* Set the property values of a CMPI instance from a specific resource. */
208 int Xen_ConsoleSettingData_setInstanceFromResource(xen_utils_session *session,
209 _RESOURCE * resource,
210 const CMPIInstance * instance,
211 const CMPIBroker * broker)
212 {
213 char buf[MAX_INSTANCEID_LEN];
214 xen_vm_record *alloced_vm_rec = NULL;
215 xen_vm_record *vm_rec;
217 if (resource == NULL) return 0;
218 if (CMIsNullObject(instance)) return 0;
220 xen_vm_record_opt *vm_rec_opt = resource->vm;
221 if (vm_rec_opt->is_record) {
222 vm_rec = vm_rec_opt->u.record;
223 }
224 else {
225 if (!xen_vm_get_record(session->xen, &vm_rec, vm_rec_opt->u.handle)) {
226 /* Error description in session object! */
227 return 0;
228 }
229 alloced_vm_rec = vm_rec;
230 }
232 /* Set the CMPIInstance properties from the resource data. */
233 snprintf(buf, MAX_INSTANCEID_LEN, "Xen:%s:%s", vm_rec->name_label, resource->uuid);
234 CMSetProperty(instance, "InstanceID",(CMPIValue *)buf, CMPI_chars);
235 CMSetProperty(instance, "ElementName",(CMPIValue *)"Console", CMPI_chars);
237 int type = 24; /* 24 == Graphics Controller */
238 CMSetProperty(instance, "ResourceType",(CMPIValue *)&type, CMPI_uint16);
240 int consumerVisibility = 3; /* 3 == Virtualized */
241 CMSetProperty(instance, "ConsumerVisibility" , (CMPIValue *)&consumerVisibility, CMPI_uint16);
243 unsigned long long consoles = 1;
244 CMSetProperty(instance, "Reservation",(CMPIValue *)&consoles, CMPI_uint64);
245 CMSetProperty(instance, "Limit",(CMPIValue *)&consoles, CMPI_uint64);
246 CMSetProperty(instance, "AllocationUnits",(CMPIValue *)"Console Devices", CMPI_chars);
247 CMSetProperty(instance, "VirtualQuantity",(CMPIValue *)&consoles, CMPI_uint64);
249 int alloctype = 1;
250 CMSetProperty(instance, "AutomaticAllocation" , (CMPIValue *)&alloctype, CMPI_boolean);
251 CMSetProperty(instance, "AutomaticDeallocation" , (CMPIValue *)&alloctype, CMPI_boolean);
253 if (resource->location)
254 CMSetProperty(instance, "URI", (CMPIValue *)resource->location, CMPI_chars);
256 int protocol = -1;
257 if (resource->protocol == XEN_CONSOLE_PROTOCOL_VT100)
258 protocol = 0; /* 0 == VT100 */
259 else if (resource->protocol == XEN_CONSOLE_PROTOCOL_RFB)
260 protocol = 1; /* 1 == VNC */
261 else if (resource->protocol == XEN_CONSOLE_PROTOCOL_RDP)
262 protocol = 2; /* 2 == SDL */
264 if (protocol != -1)
265 CMSetProperty(instance, "Protocol",(CMPIValue *)&protocol, CMPI_uint16);
267 if (alloced_vm_rec)
268 xen_vm_record_free(alloced_vm_rec);
270 return 1;
271 }
273 // ----------------------------------------------------------------------------
275 /* Delete the specified resource from the system. */
276 int Xen_ConsoleSettingData_deleteResource(_RESOURCES * resources,
277 _RESOURCE * resource)
278 {
279 /* Unsupported. */
280 return -1;
281 }
283 // ----------------------------------------------------------------------------
285 /* Modify the specified resource using the property values of a CMPI instance. */
286 int Xen_ConsoleSettingData_setResourceFromInstance(_RESOURCE * resource,
287 const CMPIInstance * instance,
288 const CMPIBroker * broker)
289 {
290 /* Unsupported. */
291 return -1;
292 }
294 // ----------------------------------------------------------------------------
296 /* Create a new resource using the property values of a CMPI instance. */
297 int Xen_ConsoleSettingData_createResourceFromInstance(_RESOURCES * resources,
298 _RESOURCE ** resource,
299 const CMPIInstance * instance,
300 const CMPIBroker * broker)
301 {
302 /* Unsupported. */
303 return -1;
304 }