1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "common_functions.h"
30 #include "util.h"
31 
32 /*
33  * Public methods
34  */
35 
36 /*
37  * Method: create_association_instList
38  *
39  * Description: Creates an instance list for the association class as named in
40  * the pClassName parameter.
41  *
42  * Parameters:
43  *	- cimchar *pClassName - The name of the association class to create
44  *	the instances of.
45  *	- CCIMObjectPath *pObjectName - One of the references for the
46  *	association.
47  *	- char *pObjectNameRole - The role that the pObjectName parameter plays
48  *	in the association.
49  *	- CCIMObjectPathList *pObjPathList - The other reference for the
50  *	association
51  *	- char *pRole - The role that the object paths in the
52  *	CCIMObjectPathList play in the association.
53  *
54  * Returns:
55  *	- CCIMInstanceList * - The instance list created from the parameters.
56  *	- NULL if an error occurred.
57  */
58 CCIMInstanceList *
create_association_instList(cimchar * pClassName,CCIMObjectPath * pObjectName,char * pObjectNameRole,CCIMObjectPathList * pObjPathList,char * pRole,int * errp)59 create_association_instList(cimchar *pClassName, CCIMObjectPath *pObjectName,
60 	char *pObjectNameRole, CCIMObjectPathList *pObjPathList, char *pRole,
61 	int *errp) {
62 
63 	CCIMObjectPathList	*currentObjPath;
64 	CCIMProperty		*objectNameProp;
65 	CCIMInstanceList	*instList;
66 	CCIMException		*ex;
67 
68 	instList = cim_createInstanceList();
69 	if (instList == NULL) {
70 		ex = cim_getLastError();
71 		util_handleError("CREATE_ASSOC_INSTLIST", CIM_ERR_FAILED,
72 			CREATE_INSTANCE_LIST_FAILURE, ex, errp);
73 		return ((CCIMInstanceList *)NULL);
74 	}
75 
76 	objectNameProp = cim_createReferenceProperty(pObjectNameRole,
77 		pObjectName, cim_true);
78 	if (objectNameProp == NULL) {
79 		ex = cim_getLastError();
80 		util_handleError("CREATE_ASSOC_INSTLIST", CIM_ERR_FAILED,
81 			CREATE_REFPROP_FAILURE, ex, errp);
82 		cim_freeInstanceList(instList);
83 		return ((CCIMInstanceList *)NULL);
84 	}
85 
86 	currentObjPath = pObjPathList;
87 
88 	while (currentObjPath != NULL) {
89 		CCIMInstance	*inst;
90 		CCIMProperty	*objPathListProp;
91 		CIMBool		returned_val;
92 
93 		/*
94 		 * Create the property from the current object path in the list.
95 		 */
96 		objPathListProp = cim_createReferenceProperty(pRole,
97 			currentObjPath->mDataObject, cim_true);
98 		if (objPathListProp == NULL) {
99 			ex = cim_getLastError();
100 			util_handleError("CREATE_ASSOC_INSTLIST",
101 				CIM_ERR_FAILED, CREATE_REFPROP_FAILURE, ex,
102 				errp);
103 			cim_freeInstanceList(instList);
104 			cim_freeProperty(objectNameProp);
105 			return ((CCIMInstanceList *)NULL);
106 		}
107 
108 		/*
109 		 * Create the instance of the class name as passed in with
110 		 * pClassName and add the properties to the instance.
111 		 */
112 		inst = cim_createInstance(pClassName);
113 		if (inst == NULL) {
114 			ex = cim_getLastError();
115 			util_handleError("CREATE_ASSOC_INSTLIST",
116 				CIM_ERR_FAILED, CREATE_INSTANCE_FAILURE, ex,
117 				errp);
118 			cim_freeInstanceList(instList);
119 			cim_freeProperty(objectNameProp);
120 			cim_freeProperty(objPathListProp);
121 			return ((CCIMInstanceList *)NULL);
122 		}
123 
124 		returned_val = cim_addProperty(inst, objectNameProp);
125 		if (returned_val == cim_false) {
126 			ex = cim_getLastError();
127 			util_handleError("CREATE_ASSOC_INSTLIST",
128 				CIM_ERR_FAILED, ADD_PROPERTY_FAILURE, ex, errp);
129 			cim_freeInstance(inst);
130 			cim_freeInstanceList(instList);
131 			cim_freeProperty(objectNameProp);
132 			cim_freeProperty(objPathListProp);
133 			return ((CCIMInstanceList *)NULL);
134 		}
135 
136 		returned_val = cim_addProperty(inst, objPathListProp);
137 		if (returned_val == cim_false) {
138 			ex = cim_getLastError();
139 			util_handleError("CREATE_ASSOC_INSTLIST",
140 				CIM_ERR_FAILED, ADD_PROPERTY_FAILURE, ex, errp);
141 			cim_freeInstance(inst);
142 			cim_freeInstanceList(instList);
143 			cim_freeProperty(objPathListProp);
144 			return ((CCIMInstanceList *)NULL);
145 		}
146 
147 		instList = cim_addInstance(instList, inst);
148 		if (instList == NULL) {
149 			ex = cim_getLastError();
150 			util_handleError("CREATE_ASSOC_INSTLIST",
151 				CIM_ERR_FAILED, ADD_INSTANCE_FAILURE, ex, errp);
152 			cim_freeInstance(inst);
153 			return ((CCIMInstanceList *)NULL);
154 		}
155 
156 		currentObjPath = currentObjPath->mNext;
157 	}
158 
159 	return (instList);
160 } /* create_association_instList */
161