static ClRcT corAmfObjectCommit(void)
{
    ClRcT rc = CL_OK;
    if(sessionId) 
    {
        rc = clCorTxnSessionCommit(sessionId);
        sessionId = 0;
    }
    return rc;
}
ClRcT clProvObjectDelete(ClCorObjectHandleT pHandle)
{
    ClRcT rc = CL_OK;
    ClCorTxnSessionIdT tid = 0;
    ClCorMOIdPtrT pMoId = NULL;
    ClCorObjectHandleT objHandle = NULL;
    ClCorServiceIdT svcId = 0;
    ClUint32T i = 0;

    CL_FUNC_ENTER();

    rc = clCorMoIdAlloc(&pMoId);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to allocate MoId. rc [0x%x]", rc));
        CL_FUNC_EXIT();
        return rc;
    }
    
    rc = clCorObjectHandleToMoIdGet(pHandle, pMoId, &svcId);
    if (rc != CL_OK)
    {   
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to get the MoId from object handle. rc [0x%x]", rc));
        clCorMoIdFree(pMoId);
        CL_FUNC_EXIT();
        return rc;
    }

    for (i = CL_COR_SVC_ID_DEFAULT + 1; i < CL_COR_SVC_ID_MAX; i++)
    {
        rc = clCorMoIdServiceSet(pMoId, (ClCorMOServiceIdT) i);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the service Id in the MoId. rc [0x%x]", rc));
            clCorMoIdFree(pMoId);
            CL_FUNC_EXIT();
            return rc;
        }

        rc = clCorObjectHandleGet(pMoId, &objHandle);
        if ((rc == CL_OK) && (CL_COR_SVC_ID_PROVISIONING_MANAGEMENT != i))
        {
            /* Some other mso object exists. 
             * Delete the provisioning mso and exit.
             */
            /* Free the object handle */
            clCorObjectHandleFree(&objHandle);

            rc = clCorMoIdServiceSet(pMoId, (ClCorMOServiceIdT) CL_COR_SVC_ID_PROVISIONING_MANAGEMENT);
            if (rc != CL_OK)
            {
                CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the service Id in the MoId. rc [0x%x]", rc));
                clCorMoIdFree(pMoId);
                CL_FUNC_EXIT();
                return rc;
            }

            rc = clCorObjectHandleGet(pMoId, &objHandle);
            if (rc == CL_OK)
            {
                rc = clCorObjectDelete(CL_COR_SIMPLE_TXN, objHandle);
                if (rc != CL_OK)
                {
                    CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to delete the MSo object. rc [0x%x]", rc));
                    clCorMoIdFree(pMoId);
                    CL_FUNC_EXIT();
                    return rc;
                }
            }

            clCorMoIdFree(pMoId);
            CL_FUNC_EXIT();
            return CL_OK;
        }
    }
    
    tid = 0;
    
    rc = clCorMoIdServiceSet(pMoId, (ClCorMOServiceIdT) CL_COR_SVC_ID_PROVISIONING_MANAGEMENT);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the service Id in the MoId. rc [0x%x]", rc));
        clCorMoIdFree(pMoId);
        CL_FUNC_EXIT();
        return rc;
    }
   
    rc = clCorObjectHandleGet(pMoId, &objHandle);
    if (rc == CL_OK)
    {
        rc = clCorObjectDelete(&tid, objHandle);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed delete the prov MSO object. rc [0x%x]\n", rc));
            clCorMoIdFree(pMoId);
            CL_FUNC_EXIT();
            return rc;
        }
    }
    
    rc = clCorMoIdServiceSet(pMoId, CL_COR_INVALID_SRVC_ID);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the svc id. rc [0x%x]", rc));
        clCorMoIdFree(pMoId);
        clCorTxnSessionFinalize(tid);
        CL_FUNC_EXIT();
        return rc;
    }

    rc = clCorObjectHandleGet(pMoId, &objHandle);
    if (rc == CL_OK)
    {
        rc = clCorObjectDelete(&tid, objHandle);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to delete the object. rc [0x%x]", rc));
            clCorMoIdFree(pMoId);   
            clCorTxnSessionFinalize(tid);
            CL_FUNC_EXIT();
            return rc;
        }
    }

    rc = clCorTxnSessionCommit(tid);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to commit the transaction. rc [0x%x]", rc));
        clCorMoIdFree(pMoId);
        clCorTxnSessionFinalize(tid);
        CL_FUNC_EXIT();
        return rc;
    }

    /* Free the handle given by the user */
    clCorObjectHandleFree(&pHandle);

    clCorMoIdFree(pMoId);
    CL_FUNC_EXIT();
    return CL_OK;
}
ClRcT clAmsMgmtOIInitialize(ClAmsMgmtHandleT *pHandle, 
                            ClRcT (*pClAmsMgmtOIConfigAttributesGet)
                            (ClAmsEntityConfigT*, ClCorAttributeValueListPtrT pAttrList),
                            ClRcT (*pClAmsMgmtOIExtendedConfigAttributesGet)
                            (ClAmsMgmtOIExtendedClassTypeT type, 
                             ClAmsMgmtOIExtendedEntityConfigT *pConfig,
                             ClCorClassTypeT *pClassType,
                             ClCorAttributeValueListT *pAttrList))
{
    ClRcT rc = CL_OK;
    ClAmsMgmtHandleT handle = 0;
    ClAmsEntityBufferT buffer[CL_AMS_ENTITY_TYPE_MAX+2] = {{0}};
    ClVersionT version = {'B', 0x1, 0x1};
    ClCorTxnSessionIdT txnSession = 0;
    ClUint32T i;
    ClCorAddrT appAddress = {0};
    SaNameT chassisInstance = {0};
    ClUint32T chassisId = 0;

    if(!pClAmsMgmtOIConfigAttributesGet)
        return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);

    rc = clOsalMutexInit(&gClAmsMgmtOICacheMutex);
    CL_ASSERT(rc == CL_OK);

    rc = clEoMyEoIocPortGet(&appAddress.portId);
    if(rc != CL_OK) 
        return rc;
    appAddress.nodeAddress = clIocLocalAddressGet();

    rc = clAmsMgmtInitialize(&handle, NULL, &version);
    if(rc != CL_OK)
    {
        return rc;
    }
    snprintf(chassisInstance.value, sizeof(chassisInstance.value),
             "%s:%d", "\\Chassis", chassisId);
    chassisInstance.length = strlen(chassisInstance.value);
    rc = clCorMoIdNameToMoIdGet(&chassisInstance, &gClChassisMoid);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "COR moid get for [%s] returned [%#x]",
                   chassisInstance.value, rc);
        goto out_free;
    }
    rc = clAmsMgmtGetSGList(handle, &buffer[CL_AMS_ENTITY_TYPE_SG]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get SG list returned [%#x]", rc);
        goto out_free;
    }
    rc = clAmsMgmtGetSIList(handle, &buffer[CL_AMS_ENTITY_TYPE_SI]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get SI list returned [%#x]", rc);
        goto out_free;
    }
    rc = clAmsMgmtGetCSIList(handle, &buffer[CL_AMS_ENTITY_TYPE_CSI]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get CSI list returned [%#x]", rc);
        goto out_free;
    }
    rc = clAmsMgmtGetNodeList(handle, &buffer[CL_AMS_ENTITY_TYPE_NODE]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get NODE list returned [%#x]", rc);
        goto out_free;
    }
    rc = clAmsMgmtGetSUList(handle, &buffer[CL_AMS_ENTITY_TYPE_SU]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get SU list returned [%#x]", rc);
        goto out_free;
    }
    rc = clAmsMgmtGetCompList(handle, &buffer[CL_AMS_ENTITY_TYPE_COMP]);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Get COMP list returned [%#x]", rc);
        goto out_free;
    }
    /*
     * Now fetch the moid for each of the entities and build the cache.
     */
    for(i = 0; i <= CL_AMS_ENTITY_TYPE_MAX; ++i)
    {
        ClAmsEntityBufferT *pBuffer = &buffer[i];
        ClUint32T j;
        if(!pBuffer->count || !pBuffer->entity)
            continue;
        gClAmsMgmtOIIndexTable[i] = pBuffer->count;
        for(j = 0; j < pBuffer->count; ++j)
        {
            ClCorMOIdT moid = {{{0}}};
            rc = clAmsMgmtOIMoIdGet(handle, pBuffer->entity+j, &moid);
            if(rc != CL_OK)
            {
                continue;
            }
            clAmsMgmtOICacheAdd(clCorMoIdToInstanceGet(&moid), pBuffer->entity+j);
            /*
             * Required for instances exceeding the pre-configured limit.
             */
             rc = clCorOIRegister(&moid, &appAddress);
             if(rc != CL_OK)
             {
                 continue;
             }
            rc = clCorPrimaryOISet(&moid, &appAddress);
            if(rc != CL_OK)
            {
                /*
                 * Ignore as it could be already set by the active.
                 */
                continue;
            }
            clAmsMgmtOIConfigAttributeSet(handle, &txnSession, &moid, pBuffer->entity+j, pClAmsMgmtOIConfigAttributesGet);
            clAmsMgmtOIExtendedConfigAttributeSet(handle, &txnSession, pBuffer->entity+j,
                                                  pClAmsMgmtOIExtendedConfigAttributesGet);
        }
    }
    rc = clCorTxnSessionCommit(txnSession);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "AMF OI config commit returned [%#x]", rc);
    }
    else
    {
        clLogNotice("AMF", "MGMT", "Entity cache successfully initialized");
        gClAmsMgmtOIInitialized = CL_TRUE;
    }

    out_free:
    for(i = 0; i <= CL_AMS_ENTITY_TYPE_MAX; ++i)
    {
        if(buffer[i].entity) clHeapFree(buffer[i].entity);
        if(rc != CL_OK) clAmsMgmtOICacheDestroy(i);
    }
    if(rc != CL_OK)
    {
        for(i = 0; i < CL_AMS_MGMT_OI_EXTENDED_CLASS_MAX; ++i)
            clAmsMgmtOIExtendedCacheDestroy(i);
    }
    if(rc == CL_OK && pHandle)
        *pHandle = handle;
    else 
        clAmsMgmtFinalize(handle);

    return rc;
}
ClRcT
clProvObjectCreate(ClCorMOIdPtrT pMoId, ClCorAttributeValueListPtrT attrList, ClCorObjectHandleT* pHandle)
{
    ClRcT rc = CL_OK;
    ClCorMOIdPtrT pTempMoId = NULL;
    ClCorObjectHandleT moHandle = NULL;
    ClCorObjectHandleT msoHandle = NULL;
    ClCorTxnSessionIdT tid = 0;
    ClCorAddrT provAddr = {0};

    CL_FUNC_ENTER();
    
    /* Create the MO object */
    if(NULL == pMoId)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("\nNULL parameter passed\n"));
        CL_FUNC_EXIT();
        return CL_COR_SET_RC(CL_COR_ERR_NULL_PTR);
    }

    rc = clCorMoIdClone(pMoId, &pTempMoId);
    if(CL_OK != rc)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,("Failed while cloning the MoId. rc[0x%x]", rc));
        CL_FUNC_EXIT();
        return rc;
    }

    rc = clCorMoIdServiceSet(pTempMoId, CL_COR_INVALID_SRVC_ID);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the service id of the MoId. rc [0x%x]", rc));
        clCorMoIdFree(pTempMoId);
        CL_FUNC_EXIT();
        return rc;
    }
    
    tid = 0;
    
    if (CL_OK != clCorObjectHandleGet(pTempMoId, &moHandle))
    {
        rc = clCorObjectCreate(&tid, pTempMoId, NULL);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to add MO object creation in txn. rc [0x%x]", rc));
            clCorMoIdFree(pTempMoId);
            CL_FUNC_EXIT();
            return rc;
        }        
    }
    else
        clCorObjectHandleFree(&moHandle);

    rc = clCorMoIdServiceSet(pTempMoId, CL_COR_SVC_ID_PROVISIONING_MANAGEMENT);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to set the service id of the MoId. rc [0x%x]\n", rc));
        clCorMoIdFree(pTempMoId);
        clCorTxnSessionFinalize(tid);
        CL_FUNC_EXIT();
        return rc;
    }

    if (CL_OK != clCorObjectHandleGet(pTempMoId, &msoHandle))
    {
        rc = clCorObjectCreateAndSet(&tid, pTempMoId, attrList, &msoHandle);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, 
                ("Failed to add create and set for the MSO object in txn. rc [0x%x]", rc));
            clCorMoIdFree(pTempMoId);
            clCorTxnSessionFinalize(tid);
            CL_FUNC_EXIT();
            return (rc);
        }

        rc = clCorTxnSessionCommit(tid);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to commit the transaction. rc [0x%x]", rc));
            clCorObjectHandleFree(&msoHandle);
            clCorMoIdFree(pTempMoId);
            clCorTxnSessionFinalize(tid);
            CL_FUNC_EXIT();
            return rc;            
        }

        provAddr.nodeAddress = clIocLocalAddressGet();
        rc = clEoMyEoIocPortGet(&provAddr.portId);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to get the IOC port. rc [0x%x]", rc));
            clCorObjectHandleFree(&msoHandle);
            clCorMoIdFree(pTempMoId);
            CL_FUNC_EXIT();
            return rc;
        }
        
        rc = clCorOIRegister(pTempMoId, &provAddr);
        if (rc != CL_OK)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to register the OI with COR. rc [0x%x]", rc));
            clCorObjectHandleFree(&msoHandle);
            clCorMoIdFree(pTempMoId);
            CL_FUNC_EXIT();
            return rc;
        }
    }
    
    clCorMoIdFree(pTempMoId);

    /* Assign the prov mso handle */
    *pHandle = msoHandle;

    CL_FUNC_EXIT();
    return (CL_OK);
}