ClRcT corAmfObjectCreate(ClUint32T entityId, ClAmsEntityT *entity, ClCorMOIdT *pMoId)
{
    ClRcT rc = CL_OK;
    ClBufferHandleT msg = 0;
    ClUint32T moIdBufferLen = 0;
    ClUint8T *moIdBuffer = NULL;
    ClCorMOIdT tempMoId;
    ClCorMOServiceIdT svcId = CL_COR_SVC_ID_PROVISIONING_MANAGEMENT;
    ClCorAttributeValueListT attrValueList = {0};

    if(!gClAmfMibLoaded) 
        svcId = CL_COR_SVC_ID_AMF_MANAGEMENT;

    /*
     *Faster than a clone
     */
    memcpy(&tempMoId, pMoId, sizeof(tempMoId));

    rc = clCorObjectCreate(&sessionId, &tempMoId, NULL);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "COR MO object create for entity [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    rc = clCorMoIdServiceSet(&tempMoId, svcId);
    CL_ASSERT(rc == CL_OK);

    rc = corAmfEntityAttributeListGet(&entityId, entity, NULL, &attrValueList);
    if(rc != CL_OK) goto out;
    
    rc = clCorObjectCreateAndSet(&sessionId, &tempMoId, &attrValueList, NULL);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "COR MSO object create for entity [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    rc = clBufferCreate(&msg);
    CL_ASSERT(rc == CL_OK);

    rc = VDECL_VER(clXdrMarshallClCorMOIdT, 4, 0, 0)((ClUint8T*)&tempMoId, msg, 0);
    CL_ASSERT(rc == CL_OK);

    rc = clBufferLengthGet(msg, &moIdBufferLen);
    CL_ASSERT(rc == CL_OK);

    rc = clBufferFlatten(msg, &moIdBuffer);
    CL_ASSERT(rc == CL_OK);

    /*
     * Now store this moid buffer into the entity user area for the entity key.
     */
    rc = clAmsMgmtEntityUserDataSetKey(mgmtHandle, entity, &entity->name, 
                                       (ClCharT*)moIdBuffer, moIdBufferLen);
    if(rc != CL_OK)
    {
        clLogError("COR", "AMF", "Entity user data set for [%s] returned [%#x]",
                   entity->name.value, rc);
        goto out;
    }

    out:
    if(attrValueList.pAttributeValue) clHeapFree(attrValueList.pAttributeValue);
    if(moIdBuffer) clHeapFree(moIdBuffer);
    if(msg) clBufferDelete(&msg);
    return rc;
}
static ClRcT corAmfExtendedClassObjectCreate(const ClCharT *pExtendedClassName,
                                             ClAmsEntityT *pEntity,
                                             ClCorMOIdT *pChassisMoId,
                                             ClUint32T rankListCount,
                                             ClUint32T *pClassIndex,
                                             ClUint32T *pIndex)
{
    ClCorAttributeValueListT attrList = {0};
    ClCorClassTypeT classType = 0;
    ClCorMOIdT moId;
    ClRcT rc = CL_OK;
    ClUint32T i;
    ClUint32T classIndex = 0;

    if(!pExtendedClassName || !pEntity || !pChassisMoId || !pClassIndex || !pIndex)
        return CL_COR_SET_RC(CL_ERR_INVALID_PARAMETER);
    if(!rankListCount)
        return CL_OK;
    rc = corAmfExtendedClassAttributeListGet(pExtendedClassName, &attrList, &classType);
    if(rc != CL_OK)
        return rc;
    if(attrList.numOfValues != 2)
    {
        clLogError("COR", "AMF", "Expected [%d] keys but got [%d] keys in table [%s]",
                   2, attrList.numOfValues, pExtendedClassName);
        if(attrList.pAttributeValue)
        {
            clHeapFree(attrList.pAttributeValue);
            return CL_COR_SET_RC(CL_ERR_NOT_EXIST);
        }
    }
    classIndex = *pClassIndex;
    for(i = 0; i < rankListCount; ++i)
    {
        memcpy(&moId, pChassisMoId, sizeof(moId));
        rc = clCorMoIdAppend(&moId, classType, classIndex);
        CL_ASSERT(rc == CL_OK);
        rc = clCorObjectCreate(&sessionId, &moId, NULL);
        if(rc != CL_OK)
        {
            clLogError("COR", "AMF", "COR MO object create for entity [%s], table [%s] returned [%#x]",
                       pEntity->name.value, pExtendedClassName, rc);
            goto out_free;
        }
        rc = clCorMoIdServiceSet(&moId, CL_COR_SVC_ID_PROVISIONING_MANAGEMENT);
        CL_ASSERT(rc == CL_OK);
        attrList.pAttributeValue[0].bufferPtr = (ClPtrT)pIndex;
        attrList.pAttributeValue[0].bufferSize = (ClUint32T)sizeof(*pIndex);
        attrList.pAttributeValue[1].bufferPtr = (ClPtrT)&i;
        attrList.pAttributeValue[1].bufferSize = (ClUint32T)sizeof(i);
        rc = clCorObjectCreateAndSet(&sessionId, &moId, &attrList, NULL);
        if(rc != CL_OK)
        {
            clLogError("COR", "AMF", "Cor MSO object create for entity [%s], table [%s] returned [%#x]",
                       pEntity->name.value, pExtendedClassName, rc);
            goto out_free;
        }
        ++classIndex;
    }

    out_free:
    if(attrList.pAttributeValue)
    {
        clHeapFree(attrList.pAttributeValue);
    }
    *pClassIndex = classIndex;
    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);
}