//
// Class that determines whether or not the origin class in an association
// operation is valid for the given association class, and also determines
// the origin and target "roles". These values generally correspond to the
// role and resultRole parameter of an associators/associatorNames operation.
//
bool InteropProvider::validAssocClassForObject(
    const OperationContext & context,
    const CIMName & assocClass, 
    const CIMObjectPath & objectName,
    const CIMNamespaceName & opNamespace,
    String & originProperty, 
    String & targetProperty)
{
    PEG_METHOD_ENTER(TRC_CONTROLPROVIDER,
        "InteropProvider::validAssocClassForObject()");
    TARGET_CLASS assocClassEnum = translateClassInput(assocClass);
    TARGET_CLASS originClassEnum;
    CIMName originClass = objectName.getClassName();
    // If the association class is PG_ElementConformsToProfile, we'll have to
    // do some special processing in case the origin instance for the operation
    // is managed by another provider.
    if(assocClassEnum == PG_ELEMENTCONFORMSTOPROFILE)
    {
        // Test if the origin is an element managed by another provider
        // that has implemented a registered profile.
        if(opNamespace != PEGASUS_NAMESPACENAME_INTEROP ||
            (originClass != PEGASUS_CLASSNAME_PG_REGISTEREDPROFILE &&
             originClass != PEGASUS_CLASSNAME_PG_OBJECTMANAGER ))
        {
            //
            // Search the cached conformingElements list for the originClass,
            // returning false if it is not found
            //
            bool found = false;

            PEGASUS_ASSERT(conformingElements.size() ==
                elementNamespaces.size());
            for(Uint32 i = 0, n = conformingElements.size(); i < n; ++i)
            {
                CIMNameArray & elementList = conformingElements[i];
                CIMNamespaceArray & namespaceList = elementNamespaces[i];
                PEGASUS_ASSERT(elementList.size() == namespaceList.size());
                for(Uint32 j = 0, m = elementList.size(); j < m; ++j)
                {
                    CIMName & curElement = elementList[j];
                    if((curElement == originClass ||
                      curElement.getString().find(PEGASUS_DYNAMIC) == 0) &&
                      opNamespace == namespaceList[j])
                    {
                        found = true;
                        break;
                    }
                }
                if(found)
                    break;
            }

            if(!found)
            {
                PEG_METHOD_EXIT();
                return false;
            }
        }
    }
    else
    {
        // Otherwise, just get the enum value representing the origin class
        // for this operation
        originClassEnum = translateClassInput(originClass);
    }

    CIMName expectedTargetRole;
    CIMName expectedOriginRole;

    Array<CIMName> propNames;
    String profileName;
    CIMPropertyList propertyList;
    CIMInstance tmpInstance;
    Uint32 index;
    propNames.clear();
    
    //
    // Set the target and origin role values. Note that if these values are
    // not set following the switch block, that implies that the origin class
    // is not valid for the supplied association class.
    //
    switch(assocClassEnum)
    {
      case PG_NAMESPACEINMANAGER:
          if(originClassEnum == PG_OBJECTMANAGER)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_NAMESPACE)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          break;
      case PG_COMMMECHANISMFORMANAGER:
          if(originClassEnum == PG_OBJECTMANAGER)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_CIMXMLCOMMUNICATIONMECHANISM)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          break;
      case PG_ELEMENTCONFORMSTOPROFILE:
          if(originClass.equal(PEGASUS_CLASSNAME_PG_REGISTEREDPROFILE))
          {
              expectedTargetRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_MANAGEDELEMENT;
              expectedOriginRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_CONFORMANTSTANDARD;
          }
          else
          {
              expectedTargetRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_CONFORMANTSTANDARD;
              expectedOriginRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_MANAGEDELEMENT;
          }
          break;
      case PG_ELEMENTCONFORMSTOPROFILE_RP_RP:
          propNames.append(CIMName("RegisteredName"));
          propertyList = CIMPropertyList(propNames);
          tmpInstance = localGetInstance(
              context, 
              objectName,
              propertyList);
          if (!tmpInstance.isUninitialized())
          {
              index = tmpInstance.findProperty("RegisteredName");
              if (index != PEG_NOT_FOUND)
              {
                  const CIMValue &tmpVal = 
                      tmpInstance.getProperty(index).getValue();
                  if (!tmpVal.isNull())
                  {
                      tmpVal.get(profileName);
                  }
              }
          }
          if (String::compareNoCase(profileName, String("SMI-S")) == 0)
          {
              expectedTargetRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_MANAGEDELEMENT;
              expectedOriginRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_CONFORMANTSTANDARD;
          }
          else
          {
              expectedTargetRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_CONFORMANTSTANDARD;
              expectedOriginRole =
                  ELEMENTCONFORMSTOPROFILE_PROPERTY_MANAGEDELEMENT;
          }
          break;
      case PG_SUBPROFILEREQUIRESPROFILE:
          if(originClassEnum == PG_REGISTEREDPROFILE)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_REGISTEREDSUBPROFILE)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          break;
      case PG_REFERENCEDPROFILE:
          if (originClassEnum == PG_REGISTEREDSUBPROFILE)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          else if (originClassEnum == PG_REGISTEREDPROFILE)
          {
              if ((targetProperty.size() != 0) &&
                  (originProperty.size() != 0) &&
                  String::equalNoCase(targetProperty, originProperty))
              {
                  return false;
              }
              if (targetProperty.size() != 0)
              {
                  if (!(String::equalNoCase(targetProperty, "Antecedent") ||
                      String::equalNoCase(targetProperty, "Dependent") ))
                  {
                      return false;
                  }
              }
              if (originProperty.size() != 0)
              {
                  if (!(String::equalNoCase(originProperty, "Antecedent") ||
                      String::equalNoCase(originProperty, "Dependent") ))
                  {
                      return false;
                  }
              }
              if (String::equalNoCase(originProperty, "Antecedent") &&
                  targetProperty.size() == 0)
              {
                  targetProperty = String("Dependent");
              }
              if (String::equalNoCase(originProperty, "Dependent") &&
                  targetProperty.size() == 0)
              {
                  targetProperty = String("Antecedent");
              }
              if (String::equalNoCase(targetProperty, "Antecedent") &&
                  originProperty.size() == 0)
              {
                  originProperty = String("Dependent");
              }
              if (String::equalNoCase(targetProperty, "Dependent") &&
                  originProperty.size() == 0)
              {
                  originProperty = String("Antecedent");
              }
              return true;
          }
          break;
      case PG_ELEMENTSOFTWAREIDENTITY:
          if(originClassEnum == PG_SOFTWAREIDENTITY)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_REGISTEREDPROFILE ||
              originClassEnum == PG_REGISTEREDSUBPROFILE)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          break;
      case PG_INSTALLEDSOFTWAREIDENTITY:
          if(originClassEnum == PG_SOFTWAREIDENTITY)
          {
              expectedTargetRole = INSTALLEDSOFTWAREIDENTITY_PROPERTY_SYSTEM;
              expectedOriginRole =
                  INSTALLEDSOFTWAREIDENTITY_PROPERTY_INSTALLEDSOFTWARE;
          }
          else if(originClassEnum == PG_COMPUTERSYSTEM)
          {
              expectedTargetRole =
                  INSTALLEDSOFTWAREIDENTITY_PROPERTY_INSTALLEDSOFTWARE;
              expectedOriginRole = INSTALLEDSOFTWAREIDENTITY_PROPERTY_SYSTEM;
          }
          break;
      case PG_HOSTEDACCESSPOINT:
          if(originClassEnum == PG_COMPUTERSYSTEM)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_CIMXMLCOMMUNICATIONMECHANISM)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
      case PG_HOSTEDOBJECTMANAGER:
          if(originClassEnum == PG_COMPUTERSYSTEM)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          else if(originClassEnum == PG_OBJECTMANAGER)
          {
              expectedTargetRole = PROPERTY_ANTECEDENT;
              expectedOriginRole = PROPERTY_DEPENDENT;
          }
          break;
#ifdef PEGASUS_ENABLE_DMTF_INDICATION_PROFILE_SUPPORT
      case PG_HOSTEDINDICATIONSERVICE:
          if(originClassEnum == PG_COMPUTERSYSTEM)
          {
              expectedTargetRole = PROPERTY_DEPENDENT;
              expectedOriginRole = PROPERTY_ANTECEDENT;
          }
          break;
#endif
      default:
          break;
    }

    //
    // The rest of this method checks to see if target role and origin roles
    // were found for the association and origin class combination and, if
    // found, checks against the input target and origin roles if provided.
    // Failure for any of these tests points to an invalid association
    // traversal request.
    //
    if(expectedTargetRole.isNull() ||
        expectedOriginRole.isNull())
    {
        PEG_METHOD_EXIT();
        return false;
    }

    if(targetProperty.size() == 0)
    {
        targetProperty = expectedTargetRole.getString();
    }
    else if(!expectedTargetRole.equal(targetProperty))
    {
        PEG_METHOD_EXIT();
        return false;
    }

    if(originProperty.size() == 0)
    {
        originProperty = expectedOriginRole.getString();
    }
    else if(!expectedOriginRole.equal(originProperty))
    {
        PEG_METHOD_EXIT();
        return false;
    }

    PEG_METHOD_EXIT();
    return true;
}
Array<CIMInstance> InteropProvider::getReferencedInstances(
    const Array<CIMInstance> &refs,
    const String &targetRole,
    const OperationContext & context,
    const CIMPropertyList & propertyList)
{
    PEG_METHOD_ENTER(TRC_CONTROLPROVIDER,
                     "InteropProvider::getReferencedObjects");

    Array<CIMInstance> referencedInstances;
    Array<CIMInstance> classInstances;
    CIMName prevClassName;

    ConstArrayIterator<CIMInstance> refsIter(refs);
    for(Uint32 i = 0; i < refsIter.size(); i++)
    {
        CIMInstance thisRef = refsIter[i];
        CIMObjectPath thisTarget = getRequiredValue<CIMObjectPath>(
                                       thisRef,
                                       targetRole);

        // Test if we're looking for something outside of our namespace. This
        // will happen during associators calls from PG_RegisteredProfile
        // instances through the PG_ElementConformsToProfile association
        CIMNamespaceName opNamespace = thisTarget.getNameSpace();
        CIMName opClass = thisTarget.getClassName();

        if((opNamespace != PEGASUS_NAMESPACENAME_INTEROP &&
                opClass != PEGASUS_CLASSNAME_PG_ELEMENTCONFORMSTOPROFILE)
                // Get CIM_IndicationService instance from IndicationService.
#ifdef PEGASUS_ENABLE_DMTF_INDICATION_PROFILE_SUPPORT
                || opClass == PEGASUS_CLASSNAME_CIM_INDICATIONSERVICE
#endif
          )
        {
            AutoMutex mut(interopMut);
            CIMInstance gotInstance = cimomHandle.getInstance(
                                          context,
                                          opNamespace,
                                          thisTarget,
                                          false,
                                          false,
                                          false,
                                          propertyList);
            referencedInstances.append(gotInstance);
            continue;
        }

        TARGET_CLASS classEnum  = translateClassInput(opClass);
        CIMInstance retInstance;
        switch(classEnum)
        {
        case PG_SOFTWAREIDENTITY:
        {
            CIMInstance retInstance =
                getSoftwareIdentityInstance(thisTarget);
            normalizeInstance(
                retInstance, thisTarget, false, false, propertyList);
            retInstance.setPath(thisTarget);
            referencedInstances.append(retInstance);
        }
        break;
        case PG_NAMESPACE:
        {
            CIMInstance retInstance = getNameSpaceInstance(thisTarget);
            normalizeInstance(
                retInstance, thisTarget, false, false, propertyList);
            retInstance.setPath(thisTarget);
            referencedInstances.append(retInstance);
        }
        break;
        default:
        {
            if( opClass != prevClassName )
            {
                CIMObjectPath ref;
                ref.setHost(thisTarget.getHost());
                ref.setClassName(thisTarget.getClassName());
                ref.setNameSpace(thisTarget.getNameSpace());
                classInstances = localEnumerateInstances(
                                     context,
                                     ref,
                                     propertyList);
                ArrayIterator<CIMInstance> instsIter(classInstances);
                for(Uint32 n = 0; n < instsIter.size(); n++)
                {
                    CIMObjectPath tmpInst = instsIter[n].getPath();
                    tmpInst.setHost(thisTarget.getHost());
                    tmpInst.setNameSpace(thisTarget.getNameSpace());
                    instsIter[n].setPath(tmpInst);
                }
                prevClassName = opClass;
            }
            ConstArrayIterator<CIMInstance> instsConstIter(classInstances);
            for(Uint32 j = 0; j < instsConstIter.size(); j++)
            {
                if(thisTarget == instsConstIter[j].getPath())
                {
                    referencedInstances.append(instsConstIter[j]);
                    break;
                }
            }
        }
        break;
        }
    }
    PEG_METHOD_EXIT();
    return referencedInstances;
}
//
// Local version of enumerateInstances to be used by other functions in the
// provider. Note that this delivers instances as a group rather than one
// at a time. This design point may need to be revisited if this provider
// is used in environments such that returning segmented responses would have
// significant performance advantages. For now, that doesn't seem to be the
// case.
//
Array<CIMInstance> InteropProvider::localEnumerateInstances(
    const OperationContext & context,
    const CIMObjectPath & ref,
    const CIMPropertyList& propertyList)
{
    PEG_METHOD_ENTER(TRC_CONTROLPROVIDER,
        "InteropProvider::localEnumerateInstances()");
    const CIMName & className = ref.getClassName();
    PEG_TRACE((TRC_CONTROLPROVIDER, Tracer::LEVEL4,
        "%s enumerateInstances. referenc= %s , PropertyList= %s",
        thisProvider,
        (const char *)className.getString().getCString(),
        (const char *)propertyListToString(propertyList).getCString()));

    // Verify that ClassName is correct and get its enum value
    TARGET_CLASS classEnum  = translateClassInput(className);

    Array<CIMInstance> instances;    
    switch(classEnum)
    {
        case PG_OBJECTMANAGER:
        {
            instances.append(getObjectManagerInstance());
            break;
        }
        case PG_CIMXMLCOMMUNICATIONMECHANISM:
        {
            instances = enumCIMXMLCommunicationMechanismInstances();
            break;
        }
        case PG_NAMESPACEINMANAGER:
        {
            instances = enumNamespaceInManagerInstances();
            break;
        }
        case PG_COMMMECHANISMFORMANAGER:
        {
            instances = enumCommMechanismForManagerInstances();
            break;
        }
        case PG_NAMESPACE:
        {
            instances = enumNamespaceInstances();
            break;
        }
        case PG_REGISTEREDPROFILE:
        {
            instances = enumRegisteredProfileInstances();
            break;
        }
        case PG_REGISTEREDSUBPROFILE:
        {
            instances = enumRegisteredSubProfileInstances();
            break;
        }
        case PG_REFERENCEDPROFILE:
        {
            instances = enumReferencedProfileInstances();
            break;
        }
        case PG_ELEMENTCONFORMSTOPROFILE:
        {
            instances = enumElementConformsToProfileInstances(context,
                ref.getNameSpace());
            break;
        }
        case PG_ELEMENTCONFORMSTOPROFILE_RP_RP:
        {
            instances = enumElementConformsToProfileRPRPInstances(
                context,
                ref.getNameSpace());
            break;
        }
        case PG_SUBPROFILEREQUIRESPROFILE:
        {
            instances = enumSubProfileRequiresProfileInstances();
            break;
        }
        case PG_SOFTWAREIDENTITY:
        {
            instances = enumSoftwareIdentityInstances();
            break;
        }
        case PG_ELEMENTSOFTWAREIDENTITY:
        {
            instances = enumElementSoftwareIdentityInstances();
            break;
        }
        case PG_INSTALLEDSOFTWAREIDENTITY:
        {
            instances = enumInstalledSoftwareIdentityInstances();
            break;
        }
        case PG_COMPUTERSYSTEM:
        {
            instances.append(getComputerSystemInstance());
            break;
        }
        case PG_HOSTEDOBJECTMANAGER:
        {
            instances.append(getHostedObjectManagerInstance());
            break;
        }
        case PG_HOSTEDACCESSPOINT:
        {
            instances = enumHostedAccessPointInstances();
            break;
        }
        //We don't support enumerate CIM_Namespace instances. PG_Namespace is
        //supported.
        case CIM_NAMESPACE:
        {
            break;
        }
        case PG_PROVIDERPROFILECAPABILITIES:
        {
            instances = enumProviderProfileCapabilityInstances(false, false);
            break;
        }

#ifdef PEGASUS_ENABLE_DMTF_INDICATION_PROFILE_SUPPORT
        case PG_ELEMENTCAPABILITIES:
        {
            instances = enumElementCapabilityInstances(context);
            break;
        }
        case PG_HOSTEDINDICATIONSERVICE:
        {
            instances = enumHostedIndicationServiceInstances(context);
            break;
        }
        case PG_SERVICEAFFECTSELEMENT:
        {
            instances = enumServiceAffectsElementInstances(context);
            break;
        }
#endif
        default:
            PEG_METHOD_EXIT();
            throw CIMNotSupportedException(className.getString() +
              " not supported by Interop Provider enumerate");
    }

    // Filter and deliver the resulting instances
    for (Uint32 i = 0 ; i < instances.size() ; i++)
    {
        normalizeInstance(instances[i], ref, false,
            false, propertyList);
    }

    PEG_METHOD_EXIT();
    return instances;
}
//
// Local version of getInstance to be used by other functions in the the
// provider. Returns a single instance. Note that it always returns an
// instance. If none was found, it is uninialitized.
//
CIMInstance InteropProvider::localGetInstance(
    const OperationContext & context,
    const CIMObjectPath & instanceName,
    const CIMPropertyList & propertyList)
{
    PEG_METHOD_ENTER(TRC_CONTROLPROVIDER, "InteropProvider::localGetInstance");

    PEG_TRACE((TRC_CONTROLPROVIDER, Tracer::LEVEL4,
               "%s getInstance. instanceName= %s , PropertyList= %s",
               thisProvider,
               (const char *)instanceName.toString().getCString(),
               (const char *)propertyList.toString().getCString()));

    // Test if we're looking for something outside of our namespace. This will
    // happen during associators calls from PG_RegisteredProfile instances
    // through the PG_ElementConformsToProfile association
    CIMNamespaceName opNamespace = instanceName.getNameSpace();
    CIMName opClass = instanceName.getClassName();
    if((opNamespace != PEGASUS_NAMESPACENAME_INTEROP &&
            opClass != PEGASUS_CLASSNAME_PG_ELEMENTCONFORMSTOPROFILE)
            // Get CIM_IndicationService instance from IndicationService.
#ifdef PEGASUS_ENABLE_DMTF_INDICATION_PROFILE_SUPPORT
            || opClass == PEGASUS_CLASSNAME_CIM_INDICATIONSERVICE
#endif
      )
    {
        AutoMutex mut(interopMut);
        CIMInstance gotInstance = cimomHandle.getInstance(
                                      context,
                                      opNamespace,
                                      instanceName,
                                      false,
                                      false,
                                      false,
                                      propertyList);
        PEG_METHOD_EXIT();
        return gotInstance;
    }

    TARGET_CLASS classEnum  = translateClassInput(opClass);
    CIMInstance retInstance;
    switch(classEnum)
    {
    case PG_SOFTWAREIDENTITY:
    {
        retInstance = getSoftwareIdentityInstance(instanceName);
        normalizeInstance(
            retInstance, instanceName, false, false, propertyList);
    }
    break;
    case PG_NAMESPACE:
    {
        retInstance = getNameSpaceInstance(instanceName);
        normalizeInstance(
            retInstance, instanceName, false, false, propertyList);
    }
    break;
    // ATTN: Implement getIntstance for all other classes. Currently
    // this method calls localEnumerateInstances() to select instance
    // which is too expensive.
    default:
    {
        // Create reference from host, namespace, class components of
        // instance name
        CIMObjectPath ref;
        ref.setHost(instanceName.getHost());
        ref.setClassName(opClass);
        ref.setNameSpace(opNamespace);

        // Enumerate instances for this class. Returns all instances
        // Note that this returns paths setup and instances already
        // filtered per the input criteria.
        Array<CIMInstance> instances =  localEnumerateInstances(
                                            context,
                                            ref,
                                            propertyList);
        ConstArrayIterator<CIMInstance> instancesIter(instances);

        // deliver a single instance if found.
        bool found = false;
        for(Uint32 i = 0; i < instancesIter.size(); i++)
        {
            CIMObjectPath currentInstRef = instancesIter[i].getPath();
            currentInstRef.setHost(instanceName.getHost());
            currentInstRef.setNameSpace(instanceName.getNameSpace());
            if(instanceName == currentInstRef)
            {
                retInstance = instancesIter[i];
                found = true;
                break;
            }
        }

        if (!found)
        {
            PEG_METHOD_EXIT();
            throw CIMObjectNotFoundException(instanceName.toString());
        }
    }
    }

    PEG_METHOD_EXIT();
    return retInstance;
}