/* * assoc_utils.c: Utilities for CMPI Providers * * Author: Jia Ming Pan * Copyright (c) 2005 International Business Machines * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include #include #include #include #include #include #include #include "cmpi_utils.h" #include "cluster_info.h" static inline int ClassIsA(const char * source_class, char * name, CMPIBroker * broker, CMPIObjectPath * cop); static CMPIInstance * MakeInstance(CMPIBroker * broker, char * classname, CMPIObjectPath * op, char * left, char * right, CMPIObjectPath * lop, CMPIObjectPath * rop, CMPIStatus * rc); int cmpi_msg2inst(CMPIBroker * broker, CMPIInstance * inst, int mapid, struct ha_msg *msg, CMPIStatus * rc) { int i = 0; const struct map_t *map = NULL; if ( (map = cim_query_map(mapid)) == NULL ) { cl_log(LOG_ERR, "%s: map is NULL.", __FUNCTION__); return HA_FAIL; } for ( i=0; i < map->len; i++) { if(map->entry[i].type == CMPI_chars) { const char *value; value = cl_get_string(msg, map->entry[i].key); if ( value == NULL ) { cl_log(LOG_WARNING, "%s: key %s not found.", __FUNCTION__, map->entry[i].key); continue; } cim_debug2(LOG_INFO, "%s: got %s:%s [CMPI_chars]", map->entry[i].key, value, __FUNCTION__); CMSetProperty(inst,map->entry[i].name, cim_strdup(value),CMPI_chars); } else if ( map->entry[i].type == CMPI_charsA ) { /* set Array */ CMPIArray * array=NULL; int j = 0, len = 0; len = cl_msg_list_length(msg, map->entry[i].key); if (len <= 0) { continue; } array = CMNewArray(broker, len, CMPI_chars, rc); if ( array == NULL ) { DEBUG_LEAVE(); return HA_FAIL; } for ( j = 0; j < len; j++ ) { char *value = NULL; value = (char*)cl_msg_list_nth_data(msg, map->entry[i].key, j); if ( value == NULL ) { continue; } CMSetArrayElementAt(array, j, &value, CMPI_chars); } cim_debug2(LOG_INFO, "%s: got %s [CMPI_charsA]", __FUNCTION__, map->entry[i].key); CMSetProperty(inst, map->entry[i].name, &array, CMPI_charsA); } else if (map->entry[i].type == CMPI_uint32){ /* set CMPI_uint32 */ } } return HA_OK; } int cmpi_inst2msg(CMPIInstance *inst, int mapid, struct ha_msg *msg, CMPIStatus *rc) { int i = 0; const struct map_t *map = cim_query_map(mapid); CMPIData data; DEBUG_ENTER(); if ( map == NULL ) { return HA_FAIL; } for( i =0; i < map->len; i++){ if ( strncmp(map->entry[i].key , "", MAXLEN) == 0 ) { continue; } data = CMGetProperty(inst, map->entry[i].name, rc); if (rc->rc != CMPI_RC_OK){ cl_log(LOG_WARNING, "Property %s missing.", map->entry[i].name); continue; } if ( data.type == CMPI_string) { char * value = CMGetCharPtr(data.value.string); cl_msg_modstring(msg, map->entry[i].key, value); } else if (data.type == CMPI_stringA){ CMPIArray * array = data.value.array; int j, len = CMGetArrayCount(array, rc); cl_msg_remove(msg, map->entry[i].key); for (j=0; jentry[i].key, value); } } else if (data.type == CMPI_uint32){ cl_log(LOG_ERR, "%s: Not support.", __FUNCTION__); } } return HA_OK; } /**************************************** * new association functions ****************************************/ /* is 'name' a 'source_class'? */ static inline int ClassIsA(const char * source_class, char * classname, CMPIBroker * broker, CMPIObjectPath * cop) { CMPIStatus rc; int is_a = 0; if ( strcmp (source_class, classname) == 0){ is_a = 1; }else if (CMClassPathIsA(broker, cop, classname, &rc)){ is_a = 1; } return is_a; } /* make a instance */ static CMPIInstance * MakeInstance(CMPIBroker * broker, char * classname, CMPIObjectPath * op, char * left, char * right, CMPIObjectPath * lop, CMPIObjectPath * rop, CMPIStatus * rc) { CMPIInstance * inst = NULL; if ( (inst = CMNewInstance(broker, op, rc) ) == NULL ) { cl_log(LOG_ERR, "%s:%d: create instance failed for %s", __FUNCTION__, __LINE__, classname); return NULL; } /* set properties */ CMSetProperty(inst, left, (CMPIValue *)&lop, CMPI_ref); CMSetProperty(inst, right, (CMPIValue *)&rop, CMPI_ref); return inst; } /* enumerate instance names */ CMPIArray * cmpi_instance_names(CMPIBroker * broker, char * namespace, char * classname, CMPIContext * ctx, CMPIStatus * rc) { int max_len = 256; CMPIObjectPath * refs[max_len]; CMPIEnumeration * en; CMPIObjectPath * op = NULL; CMPIArray * array; int len = 0, i = 0; DEBUG_ENTER(); /* create ObjectPath for the class */ op = CMNewObjectPath(broker, namespace, classname, rc); RETURN_FAIL_IFNULL_OBJ(op, "ObjectPath"); /* enumerate instance names of the class */ en = CBEnumInstanceNames(broker, ctx, op, rc); if ( CMIsNullObject(en) ){ cl_log(LOG_ERR, "cmpi_instance_names: " "enumerate instance names for %s failed.", classname); return NULL; } len = 0; while(CMHasNext(en, rc)) { CMPIObjectPath * top = CMGetNext(en, rc).value.ref; if ( CMIsNullObject(top) ) { continue; } if ( len == max_len ) { break; } refs[len] = top; len ++; } if ((array = CMNewArray(broker, len, CMPI_ref, rc)) == NULL ) { cl_log(LOG_ERR, "cmpi_instance_names: create array failed"); return NULL; } for (i=0; i