Beispiel #1
0
class UcmdbTargetSystem(AbstractTargetSystem):
    class OshBuilder(CiBuilder):
        def __init__(self, targetCiType):
            self.__type = targetCiType
            self.__osh = ObjectStateHolder(self.__type)

        def setCiAttribute(self, name, value):
            attributeType = self.__getAttributeType(self.__type, name)
            if value:
                self.__setValue(name, attributeType, value)
            else:
                logger.debug("Meet none value for %s, type:%s" % (name, attributeType))

        def build(self):
            return self.__osh

        def __setValue(self, name, attributeType, value):
            if attributeType == 'string':
                self.__osh.setStringAttribute(name, str(value))
            elif attributeType == 'integer':
                self.__osh.setIntegerAttribute(name, int(value))
            elif attributeType.endswith('enum'):
                if isinstance(value, int):
                    self.__osh.setEnumAttribute(name, value)
                else:
                    self.__osh.setAttribute(name, value)
            elif attributeType == 'string_list':
                self.__osh.setListAttribute(name, value)
            else:
                raise ValueError('no setter defined for type %s' % attributeType)

        def __getAttributeType(self, ciType, attributeName):
            try:
                attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName)
                return attributeDefinition.getType()
            except:
                if DEBUG:
                    if attributeName in ['memory_size', 'port_index']:
                        return 'integer'
                    return 'string'
                raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))

    def __init__(self):
        self.__vector = ObjectStateHolderVector()
        self.__cis = {}
        self.__links = []
        self.sourceCIMap = {}

    def addCi(self, osh, sourceCi, sourceType):
        "@type: ObjectStateHolder, str, str"
        sourceCiId = sourceCi.getId()
        targetType = osh.getObjectClass()
        ciId = self.__createComplexId(sourceCiId, sourceType, targetType)
        self.__cis[ciId] = osh
        self.sourceCIMap[ciId] = sourceCi

    # logger.info('adding osh for %s' % ciId)

    def addLink(self, linkMapping, link):
        "@types: LinkMapping, Link"

        sourceType1 = linkMapping.getSourceEnd1Type()
        sourceType2 = linkMapping.getSourceEnd2Type()
        targetType1 = linkMapping.getTargetEnd1Type()
        targetType2 = linkMapping.getTargetEnd2Type()
        sourceId1 = link.getEnd1Id()
        sourceId2 = link.getEnd2Id()

        targetEnd1Id = self.__createComplexId(sourceId1, sourceType1, targetType1)
        targetEnd2Id = self.__createComplexId(sourceId2, sourceType2, targetType2)

        if not self.__hasOsh(targetEnd1Id) or not self.__hasOsh(targetEnd2Id):
            failurePolicy = linkMapping.getFailurePolicy()

            if failurePolicy == 'exclude_end1':
                self.__excludeCi(targetEnd1Id)

            elif failurePolicy == 'exclude_end2':
                self.__excludeCi(targetEnd2Id)

            elif failurePolicy == 'exclude_both':
                self.__excludeCi(targetEnd1Id)
                self.__excludeCi(targetEnd2Id)
        else:
            logger.info('adding %s -- %s --> %s' % (targetEnd1Id, linkMapping.getTargetType(), targetEnd2Id))
            self.__links.append((linkMapping, link))

    def createCiBuilder(self, targetCiType):
        "@types: str -> OshBuilder"
        return UcmdbTargetSystem.OshBuilder(targetCiType)

    def getTopology(self):
        self.linksMap = defaultdict(list)
        for (linkMapping, link) in self.__links:
            targetType = linkMapping.getTargetType()
            sourceType1 = linkMapping.getSourceEnd1Type()
            sourceType2 = linkMapping.getSourceEnd2Type()
            sourceId1 = link.getEnd1Id()
            sourceId2 = link.getEnd2Id()
            targetType1 = linkMapping.getTargetEnd1Type()
            targetType2 = linkMapping.getTargetEnd2Type()
            isContainer = linkMapping.isContainer()

            targetEnd1Id = self.__createComplexId(sourceId1, sourceType1, targetType1)
            targetEnd2Id = self.__createComplexId(sourceId2, sourceType2, targetType2)

            msg = "%s -- %s --> %s" % (targetEnd1Id, targetType, targetEnd2Id)
            logger.warn(msg)
            if self.__hasOsh(targetEnd1Id) and self.__hasOsh(targetEnd2Id):
                logger.info(msg)

                (osh1, osh2) = (self.__getOsh(targetEnd1Id), self.__getOsh(targetEnd2Id))
                if linkMapping.isReverse():
                    (osh1, osh2) = (osh2, osh1)

                link_osh = modeling.createLinkOSH(targetType, osh1, osh2)
                self.__vector.add(link_osh)
                self.linksMap[osh1].append(link_osh)
                self.linksMap[osh2].append(link_osh)
                if targetType == 'composition' or isContainer:
                    osh2.setContainer(osh1)

        self.addValidCis()

        return self.__vector

    def addCisWithRootContainer(self, osh):
        if self.__vector.contains(osh) or osh not in self.needContainerCIs:  # already in or doesn't need container
            return True
        rootContainer = osh.getAttributeValue("root_container")
        if rootContainer and self.addAllDependencies(rootContainer):  # root container in, in too
            self.__vector.add(osh)
            return True
        else:
            logger.debug('No root container for osh', osh)
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Remove links of isolated CIs")
                for link in links:
                    self.__vector.remove(link)
            return False

    def addCisWithDependency(self, osh):
        if self.__vector.contains(osh) or osh not in self.needRelationshipCIs:  # already in or doesn't need dependency
            return True
        deps = self.needRelationshipCIs[osh].split(',')
        allFound = True
        for dep in deps:
            relationship, targetCIType = dep.split(':')
            singleFound = False
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Get link of target ci", osh)

                for link in links:
                    if link.getObjectClass() == relationship:
                        end1 = link.getAttributeValue('link_end1')
                        end2 = link.getAttributeValue('link_end2')
                        that = end1 if end2 == osh else end2
                        if that.getObjectClass() == targetCIType:
                            singleFound = self.addAllDependencies(that)
                            if singleFound:
                                break
            if not singleFound:
                allFound = False
                break
        if allFound:
            self.__vector.add(osh)
        else:
            for link in self.linksMap[osh]:
                self.__vector.remove(link)
        return allFound

    def addAllDependencies(self, targetOsh):
        return self.addCisWithRootContainer(targetOsh) and self.addCisWithDependency(targetOsh)

    def addValidCis(self):
        self.needContainerCIs = []
        self.needRelationshipCIs = {}

        for key, osh in self.__cis.items():
            sourceCI = self.sourceCIMap[key]
            standalone = True
            if sourceCI.getMapping().needContainer():
                self.needContainerCIs.append(osh)
                standalone = False
            if sourceCI.getMapping().needRelationship():
                logger.debug('Need relationship:', sourceCI.getMapping().needRelationship())
                self.needRelationshipCIs[osh] = sourceCI.getMapping().needRelationship()
                standalone = False
            if standalone:
                self.__vector.add(osh)

        allDependencyCIs = []
        allDependencyCIs.extend(self.needContainerCIs)
        allDependencyCIs.extend(self.needRelationshipCIs.keys())
        for osh in allDependencyCIs:
            self.addAllDependencies(osh)

    def __excludeCi(self, complexId):
        if self.__hasOsh(complexId):
            logger.info("Excluding %s" % complexId)
            del self.__cis[complexId]

    def __getOsh(self, complexId):
        return self.__cis[complexId]

    def __hasOsh(self, complexId):
        return complexId in self.__cis.keys()

    def __createComplexId(self, sourceId, sourceType, targetType):
        return targetType and "%s: %s_%s" % (targetType, sourceId, sourceType) or "%s_%s" % (sourceId, sourceType)
Beispiel #2
0
class UcmdbTargetSystem(AbstractTargetSystem):
    class OshBuilder(CiBuilder):
        def __init__(self, targetCiType):
            self.__type = targetCiType
            self.__osh = ObjectStateHolder(self.__type)

        def setCiAttribute(self, name, value):
            attributeType = self.__getAttributeType(self.__type, name)
            if value:
                self.__setValue(name, attributeType, value)
            else:
                logger.debug("Meet none value for %s, type:%s" %
                             (name, attributeType))

        def build(self):
            return self.__osh

        def __setValue(self, name, attributeType, value):
            if attributeType == 'string':
                self.__osh.setStringAttribute(name, str(value))
            elif attributeType == 'integer':
                self.__osh.setIntegerAttribute(name, int(value))
            elif attributeType.endswith('enum'):
                if isinstance(value, int):
                    self.__osh.setEnumAttribute(name, value)
                else:
                    self.__osh.setAttribute(name, value)
            elif attributeType == 'string_list':
                self.__osh.setListAttribute(name, value)
            else:
                raise ValueError('no setter defined for type %s' %
                                 attributeType)

        def __getAttributeType(self, ciType, attributeName):
            try:
                attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(
                    ciType, attributeName)
                return attributeDefinition.getType()
            except:
                if DEBUG:
                    return 'string'
                logger.errorException("%s.%s" % (ciType, attributeName))
                raise ValueError("Failed to determine type of %s.%s" %
                                 (ciType, attributeName))

    def __init__(self):
        self.__vector = ObjectStateHolderVector()
        self.__cis = {}
        self.__links = []
        self.sourceCIMap = {}

    def addCi(self, osh, sourceCi, sourceType):
        "@type: ObjectStateHolder, str, str"
        sourceCiId = sourceCi.getId()
        targetType = osh.getObjectClass()
        ciId = self.__createComplexId(sourceCiId, sourceType, targetType)
        self.__cis[ciId] = osh
        self.sourceCIMap[ciId] = sourceCi

    # logger.info('adding osh for %s' % ciId)

    def addLink(self, linkMapping, link):
        "@types: LinkMapping, Link"

        sourceType1 = linkMapping.getSourceEnd1Type()
        sourceType2 = linkMapping.getSourceEnd2Type()
        targetType1 = linkMapping.getTargetEnd1Type()
        targetType2 = linkMapping.getTargetEnd2Type()
        sourceId1 = link.getEnd1Id()
        sourceId2 = link.getEnd2Id()

        targetEnd1Id = self.__createComplexId(sourceId1, sourceType1,
                                              targetType1)
        targetEnd2Id = self.__createComplexId(sourceId2, sourceType2,
                                              targetType2)

        if not self.__hasOsh(targetEnd1Id) or not self.__hasOsh(targetEnd2Id):
            failurePolicy = linkMapping.getFailurePolicy()

            if failurePolicy == 'exclude_end1':
                self.__excludeCi(targetEnd1Id)

            if failurePolicy == 'exclude_end2':
                self.__excludeCi(targetEnd2Id)

            if failurePolicy == 'exclude_both':
                self.__excludeCi(targetEnd1Id)
                self.__excludeCi(targetEnd2Id)
        else:
            logger.info(
                'adding %s -- %s --> %s' %
                (targetEnd1Id, linkMapping.getTargetType(), targetEnd2Id))
            self.__links.append((linkMapping, link))

    def createCiBuilder(self, targetCiType):
        "@types: str -> OshBuilder"
        return UcmdbTargetSystem.OshBuilder(targetCiType)

    def getTopology(self):
        self.linksMap = defaultdict(list)
        for (linkMapping, link) in self.__links:
            targetType = linkMapping.getTargetType()
            sourceType1 = linkMapping.getSourceEnd1Type()
            sourceType2 = linkMapping.getSourceEnd2Type()
            sourceId1 = link.getEnd1Id()
            sourceId2 = link.getEnd2Id()
            targetType1 = linkMapping.getTargetEnd1Type()
            targetType2 = linkMapping.getTargetEnd2Type()
            isContainer = linkMapping.isContainer()

            targetEnd1Id = self.__createComplexId(sourceId1, sourceType1,
                                                  targetType1)
            targetEnd2Id = self.__createComplexId(sourceId2, sourceType2,
                                                  targetType2)

            msg = "%s -- %s --> %s" % (targetEnd1Id, targetType, targetEnd2Id)
            if self.__hasOsh(targetEnd1Id) and self.__hasOsh(targetEnd2Id):
                logger.info(msg)

                (osh1, osh2) = (self.__getOsh(targetEnd1Id),
                                self.__getOsh(targetEnd2Id))
                if linkMapping.isReverse():
                    (osh1, osh2) = (osh2, osh1)

                link_osh = modeling.createLinkOSH(targetType, osh1, osh2)
                self.__vector.add(link_osh)
                self.linksMap[osh1].append(link_osh)
                self.linksMap[osh2].append(link_osh)
                if targetType == 'composition' or isContainer:
                    osh2.setContainer(osh1)

        self.addValidCis()

        return self.__vector

    def addCisWithRootContainer(self, osh):
        if self.__vector.contains(osh):  # already in
            return True
        rootContainer = osh.getAttributeValue("root_container")
        if rootContainer and self.addCisWithRootContainer(
                rootContainer):  # root container in, in too
            self.__vector.add(osh)
            return True
        else:
            logger.debug('No root container for osh', osh)
            print 'No root container for osh', osh
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Remove links of isolated CIs")
                for link in links:
                    self.__vector.remove(link)
            return False

    def addValidCis(self):
        needContainerCIs = []
        for key, osh in self.__cis.items():
            sourceCI = self.sourceCIMap[key]
            if sourceCI.getMapping().needContainer():
                needContainerCIs.append(osh)
            else:
                self.__vector.add(osh)

        for ci in needContainerCIs:
            self.addCisWithRootContainer(ci)

    def __excludeCi(self, complexId):
        if self.__hasOsh(complexId):
            logger.info("Excluding %s" % complexId)
            del self.__cis[complexId]

    def __getOsh(self, complexId):
        return self.__cis[complexId]

    def __hasOsh(self, complexId):
        return complexId in self.__cis.keys()

    def __createComplexId(self, sourceId, sourceType, targetType):
        return targetType and "%s: %s_%s" % (
            targetType, sourceId, sourceType) or "%s_%s" % (sourceId,
                                                            sourceType)
Beispiel #3
0
class UcmdbTargetSystem(AbstractTargetSystem):
    class OshBuilder(CiBuilder):
        def __init__(self, targetCiType):
            self.__type = targetCiType
            self.__osh = ObjectStateHolder(self.__type)

        def setCiAttribute(self, name, value):
            attributeType = self.__getAttributeType(self.__type, name)
            if value:
                self.__setValue(name, attributeType, value)
            else:
                logger.debug("Meet none value for %s, type:%s" % (name, attributeType))

        def build(self):
            return self.__osh

        def __setValue(self, name, attributeType, value):
            if attributeType == 'string':
                self.__osh.setStringAttribute(name, str(value))
            elif attributeType == 'integer':
                self.__osh.setIntegerAttribute(name, int(value))
            elif attributeType.endswith('enum'):
                if isinstance(value, int):
                    self.__osh.setEnumAttribute(name, value)
                else:
                    self.__osh.setAttribute(name, value)
            elif attributeType == 'string_list':
                self.__osh.setListAttribute(name, value)
            else:
                raise ValueError('no setter defined for type %s' % attributeType)

        def __getAttributeType(self, ciType, attributeName):
            try:
                attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName)
                return attributeDefinition.getType()
            except:
                if DEBUG:
                    return 'string'
                logger.errorException("%s.%s" % (ciType, attributeName))
                raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))

    def __init__(self):
        self.__vector = ObjectStateHolderVector()
        self.__cis = {}
        self.__links = []
        self.sourceCIMap = {}

    def addCi(self, osh, sourceCi, sourceType):
        "@type: ObjectStateHolder, str, str"
        sourceCiId = sourceCi.getId()
        targetType = osh.getObjectClass()
        ciId = self.__createComplexId(sourceCiId, sourceType, targetType)
        self.__cis[ciId] = osh
        self.sourceCIMap[ciId] = sourceCi

    # logger.info('adding osh for %s' % ciId)

    def addLink(self, linkMapping, link):
        "@types: LinkMapping, Link"

        sourceType1 = linkMapping.getSourceEnd1Type()
        sourceType2 = linkMapping.getSourceEnd2Type()
        targetType1 = linkMapping.getTargetEnd1Type()
        targetType2 = linkMapping.getTargetEnd2Type()
        sourceId1 = link.getEnd1Id()
        sourceId2 = link.getEnd2Id()

        targetEnd1Id = self.__createComplexId(sourceId1, sourceType1, targetType1)
        targetEnd2Id = self.__createComplexId(sourceId2, sourceType2, targetType2)

        if not self.__hasOsh(targetEnd1Id) or not self.__hasOsh(targetEnd2Id):
            failurePolicy = linkMapping.getFailurePolicy()

            if failurePolicy == 'exclude_end1':
                self.__excludeCi(targetEnd1Id)

            if failurePolicy == 'exclude_end2':
                self.__excludeCi(targetEnd2Id)

            if failurePolicy == 'exclude_both':
                self.__excludeCi(targetEnd1Id)
                self.__excludeCi(targetEnd2Id)
        else:
            logger.info('adding %s -- %s --> %s' % (targetEnd1Id, linkMapping.getTargetType(), targetEnd2Id))
            self.__links.append((linkMapping, link))

    def createCiBuilder(self, targetCiType):
        "@types: str -> OshBuilder"
        return UcmdbTargetSystem.OshBuilder(targetCiType)

    def getTopology(self):
        self.linksMap = defaultdict(list)
        for (linkMapping, link) in self.__links:
            targetType = linkMapping.getTargetType()
            sourceType1 = linkMapping.getSourceEnd1Type()
            sourceType2 = linkMapping.getSourceEnd2Type()
            sourceId1 = link.getEnd1Id()
            sourceId2 = link.getEnd2Id()
            targetType1 = linkMapping.getTargetEnd1Type()
            targetType2 = linkMapping.getTargetEnd2Type()
            isContainer = linkMapping.isContainer()

            targetEnd1Id = self.__createComplexId(sourceId1, sourceType1, targetType1)
            targetEnd2Id = self.__createComplexId(sourceId2, sourceType2, targetType2)

            msg = "%s -- %s --> %s" % (targetEnd1Id, targetType, targetEnd2Id)
            if self.__hasOsh(targetEnd1Id) and self.__hasOsh(targetEnd2Id):
                logger.info(msg)

                (osh1, osh2) = (self.__getOsh(targetEnd1Id), self.__getOsh(targetEnd2Id))
                if linkMapping.isReverse():
                    (osh1, osh2) = (osh2, osh1)

                link_osh = modeling.createLinkOSH(targetType, osh1, osh2)
                self.__vector.add(link_osh)
                self.linksMap[osh1].append(link_osh)
                self.linksMap[osh2].append(link_osh)
                if targetType == 'composition' or isContainer:
                    osh2.setContainer(osh1)

        self.addValidCis()

        return self.__vector

    def addCisWithRootContainer(self, osh):
        if self.__vector.contains(osh):  # already in
            return True
        rootContainer = osh.getAttributeValue("root_container")
        if rootContainer and self.addCisWithRootContainer(rootContainer):  # root container in, in too
            self.__vector.add(osh)
            return True
        else:
            logger.debug('No root container for osh', osh)
            print 'No root container for osh', osh
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Remove links of isolated CIs")
                for link in links:
                    self.__vector.remove(link)
            return False

    def addValidCis(self):
        needContainerCIs = []
        for key, osh in self.__cis.items():
            sourceCI = self.sourceCIMap[key]
            if sourceCI.getMapping().needContainer():
                needContainerCIs.append(osh)
            else:
                self.__vector.add(osh)

        for ci in needContainerCIs:
            self.addCisWithRootContainer(ci)

    def __excludeCi(self, complexId):
        if self.__hasOsh(complexId):
            logger.info("Excluding %s" % complexId)
            del self.__cis[complexId]

    def __getOsh(self, complexId):
        return self.__cis[complexId]

    def __hasOsh(self, complexId):
        return complexId in self.__cis.keys()

    def __createComplexId(self, sourceId, sourceType, targetType):
        return targetType and "%s: %s_%s" % (targetType, sourceId, sourceType) or "%s_%s" % (sourceId, sourceType)
Beispiel #4
0
class UcmdbTargetSystem(AbstractTargetSystem):
    class OshBuilder(CiBuilder):
        def __init__(self, targetCiType):
            self.__type = targetCiType
            self.__osh = ObjectStateHolder(self.__type)

        def setCiAttribute(self, name, value):
            attributeType = self.__getAttributeType(self.__type, name)
            if value:
                self.__setValue(name, attributeType, value)
            else:
                logger.debug("Meet none value for %s, type:%s" %
                             (name, attributeType))

        def build(self):
            return self.__osh

        def __setValue(self, name, attributeType, value):
            if attributeType == 'string':
                self.__osh.setStringAttribute(name, str(value))
            elif attributeType == 'integer':
                self.__osh.setIntegerAttribute(name, int(value))
            elif attributeType.endswith('enum'):
                if isinstance(value, int):
                    self.__osh.setEnumAttribute(name, value)
                else:
                    self.__osh.setAttribute(name, value)
            elif attributeType == 'string_list':
                self.__osh.setListAttribute(name, value)
            else:
                raise ValueError('no setter defined for type %s' %
                                 attributeType)

        def __getAttributeType(self, ciType, attributeName):
            try:
                attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(
                    ciType, attributeName)
                return attributeDefinition.getType()
            except:
                if DEBUG:
                    if attributeName in ['memory_size', 'port_index']:
                        return 'integer'
                    return 'string'
                raise ValueError("Failed to determine type of %s.%s" %
                                 (ciType, attributeName))

    def __init__(self):
        self.__vector = ObjectStateHolderVector()
        self.__cis = {}
        self.__links = []
        self.sourceCIMap = {}

    def addCi(self, osh, sourceCi, sourceType):
        "@type: ObjectStateHolder, str, str"
        sourceCiId = sourceCi.getId()
        targetType = osh.getObjectClass()
        ciId = self.__createComplexId(sourceCiId, sourceType, targetType)
        self.__cis[ciId] = osh
        self.sourceCIMap[ciId] = sourceCi

    # logger.info('adding osh for %s' % ciId)

    def addLink(self, linkMapping, link):
        "@types: LinkMapping, Link"

        sourceType1 = linkMapping.getSourceEnd1Type()
        sourceType2 = linkMapping.getSourceEnd2Type()
        targetType1 = linkMapping.getTargetEnd1Type()
        targetType2 = linkMapping.getTargetEnd2Type()
        sourceId1 = link.getEnd1Id()
        sourceId2 = link.getEnd2Id()

        targetEnd1Id = self.__createComplexId(sourceId1, sourceType1,
                                              targetType1)
        targetEnd2Id = self.__createComplexId(sourceId2, sourceType2,
                                              targetType2)

        if not self.__hasOsh(targetEnd1Id) or not self.__hasOsh(targetEnd2Id):
            failurePolicy = linkMapping.getFailurePolicy()

            if failurePolicy == 'exclude_end1':
                self.__excludeCi(targetEnd1Id)

            elif failurePolicy == 'exclude_end2':
                self.__excludeCi(targetEnd2Id)

            elif failurePolicy == 'exclude_both':
                self.__excludeCi(targetEnd1Id)
                self.__excludeCi(targetEnd2Id)
        else:
            logger.info(
                'adding %s -- %s --> %s' %
                (targetEnd1Id, linkMapping.getTargetType(), targetEnd2Id))
            self.__links.append((linkMapping, link))

    def createCiBuilder(self, targetCiType):
        "@types: str -> OshBuilder"
        return UcmdbTargetSystem.OshBuilder(targetCiType)

    def getTopology(self):
        self.linksMap = defaultdict(list)
        for (linkMapping, link) in self.__links:
            targetType = linkMapping.getTargetType()
            sourceType1 = linkMapping.getSourceEnd1Type()
            sourceType2 = linkMapping.getSourceEnd2Type()
            sourceId1 = link.getEnd1Id()
            sourceId2 = link.getEnd2Id()
            targetType1 = linkMapping.getTargetEnd1Type()
            targetType2 = linkMapping.getTargetEnd2Type()
            isContainer = linkMapping.isContainer()

            targetEnd1Id = self.__createComplexId(sourceId1, sourceType1,
                                                  targetType1)
            targetEnd2Id = self.__createComplexId(sourceId2, sourceType2,
                                                  targetType2)

            msg = "%s -- %s --> %s" % (targetEnd1Id, targetType, targetEnd2Id)
            logger.warn(msg)
            if self.__hasOsh(targetEnd1Id) and self.__hasOsh(targetEnd2Id):
                logger.info(msg)

                (osh1, osh2) = (self.__getOsh(targetEnd1Id),
                                self.__getOsh(targetEnd2Id))
                if linkMapping.isReverse():
                    (osh1, osh2) = (osh2, osh1)

                link_osh = modeling.createLinkOSH(targetType, osh1, osh2)
                self.__vector.add(link_osh)
                self.linksMap[osh1].append(link_osh)
                self.linksMap[osh2].append(link_osh)
                if targetType == 'composition' or isContainer:
                    osh2.setContainer(osh1)

        self.addValidCis()

        return self.__vector

    def addCisWithRootContainer(self, osh):
        if self.__vector.contains(
                osh
        ) or osh not in self.needContainerCIs:  # already in or doesn't need container
            return True
        rootContainer = osh.getAttributeValue("root_container")
        if rootContainer and self.addAllDependencies(
                rootContainer):  # root container in, in too
            self.__vector.add(osh)
            return True
        else:
            logger.debug('No root container for osh', osh)
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Remove links of isolated CIs")
                for link in links:
                    self.__vector.remove(link)
            return False

    def addCisWithDependency(self, osh):
        if self.__vector.contains(
                osh
        ) or osh not in self.needRelationshipCIs:  # already in or doesn't need dependency
            return True
        deps = self.needRelationshipCIs[osh].split(',')
        allFound = True
        for dep in deps:
            relationship, targetCIType = dep.split(':')
            singleFound = False
            if osh in self.linksMap:
                links = self.linksMap[osh]
                logger.debug("Get link of target ci", osh)

                for link in links:
                    if link.getObjectClass() == relationship:
                        end1 = link.getAttributeValue('link_end1')
                        end2 = link.getAttributeValue('link_end2')
                        that = end1 if end2 == osh else end2
                        if that.getObjectClass() == targetCIType:
                            singleFound = self.addAllDependencies(that)
                            if singleFound:
                                break
            if not singleFound:
                allFound = False
                break
        if allFound:
            self.__vector.add(osh)
        else:
            for link in self.linksMap[osh]:
                self.__vector.remove(link)
        return allFound

    def addAllDependencies(self, targetOsh):
        return self.addCisWithRootContainer(
            targetOsh) and self.addCisWithDependency(targetOsh)

    def addValidCis(self):
        self.needContainerCIs = []
        self.needRelationshipCIs = {}

        for key, osh in self.__cis.items():
            sourceCI = self.sourceCIMap[key]
            standalone = True
            if sourceCI.getMapping().needContainer():
                self.needContainerCIs.append(osh)
                standalone = False
            if sourceCI.getMapping().needRelationship():
                logger.debug('Need relationship:',
                             sourceCI.getMapping().needRelationship())
                self.needRelationshipCIs[osh] = sourceCI.getMapping(
                ).needRelationship()
                standalone = False
            if standalone:
                self.__vector.add(osh)

        allDependencyCIs = []
        allDependencyCIs.extend(self.needContainerCIs)
        allDependencyCIs.extend(self.needRelationshipCIs.keys())
        for osh in allDependencyCIs:
            self.addAllDependencies(osh)

    def __excludeCi(self, complexId):
        if self.__hasOsh(complexId):
            logger.info("Excluding %s" % complexId)
            del self.__cis[complexId]

    def __getOsh(self, complexId):
        return self.__cis[complexId]

    def __hasOsh(self, complexId):
        return complexId in self.__cis.keys()

    def __createComplexId(self, sourceId, sourceType, targetType):
        return targetType and "%s: %s_%s" % (
            targetType, sourceId, sourceType) or "%s_%s" % (sourceId,
                                                            sourceType)