X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-model%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fmodel%2Fjsonjanusgraph%2Foperations%2FToscaOperationFacade.java;h=951dc5aaed1c874f27a7bda34f8dcf347d4e44c5;hb=c469756a1092194adedd590d35f0f1f8feac3a36;hp=7c34da89f0252ba7aed1da2a7e883b41fbd737c2;hpb=ee8876059c520d97bf068734b25a02365d7fe1ea;p=sdc.git diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java index 7c34da89f0..951dc5aaed 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java @@ -19,33 +19,11 @@ */ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; -import static java.util.Objects.requireNonNull; -import static org.apache.commons.collections.CollectionUtils.isEmpty; -import static org.apache.commons.collections.CollectionUtils.isNotEmpty; -import static org.janusgraph.core.attribute.Text.REGEX; - import com.vdurmont.semver4j.Semver; import com.vdurmont.semver4j.Semver.SemverType; import fj.data.Either; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.TreeSet; -import java.util.function.BiPredicate; -import java.util.stream.Collectors; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -68,6 +46,7 @@ import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; @@ -134,6 +113,32 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.ValidationUtils; import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; +import java.util.function.BiPredicate; +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.janusgraph.core.attribute.Text.REGEX; +import static org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum.TOPOLOGY_TEMPLATE; + @org.springframework.stereotype.Component("tosca-operation-facade") public class ToscaOperationFacade { @@ -602,7 +607,7 @@ public class ToscaOperationFacade { public Either getLatestResourceByToscaResourceName(String toscaResourceName) { if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf")) { - return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata); + return getLatestResourceByToscaResourceName(toscaResourceName, TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata); } else { return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata); } @@ -679,7 +684,7 @@ public class ToscaOperationFacade { public Either, StorageOperationStatus> associateResourceInstances(Component component, String componentId, List relations) { Either, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation - .associateResourceInstances(component, componentId, relations); + .associateResourceInstances(componentId, relations); if (component != null) { updateInstancesCapAndReqOnComponentFromDB(component); } @@ -782,7 +787,7 @@ public class ToscaOperationFacade { return updateToscaElement(componentToUpdate, new ComponentParametersView()); } - public Either updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) { + private Either updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) { String componentId = componentToUpdate.getUniqueId(); Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll); if (getVertexEither.isRight()) { @@ -846,44 +851,26 @@ public class ToscaOperationFacade { return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata, modelName); } - public Either, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) { - Either, StorageOperationStatus> result = null; - Either getComponentRes; - List components = new ArrayList<>(); - List componentVertices; - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); + public Either getBySystemNameAndVersion(final ComponentTypeEnum componentType, + final String systemName, + final String version) { + final Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); + final Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName); + propertiesToMatch.put(GraphPropertyEnum.VERSION, version); if (componentType != null) { propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); } propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - Either, JanusGraphOperationStatus> getComponentsRes = janusGraphDao - .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); - if (getComponentsRes.isRight()) { - JanusGraphOperationStatus status = getComponentsRes.right().value(); - log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status); - result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); - } - if (result == null) { - componentVertices = getComponentsRes.left().value(); - for (GraphVertex componentVertex : componentVertices) { - getComponentRes = getToscaElementByOperation(componentVertex); - if (getComponentRes.isRight()) { - log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), - getComponentRes.right().value()); - result = Either.right(getComponentRes.right().value()); - break; - } - T componentBySystemName = getComponentRes.left().value(); - log.debug("Found component, id: {}", componentBySystemName.getUniqueId()); - components.add(componentBySystemName); - } - } - if (result == null) { - result = Either.left(components); + + final Either, JanusGraphOperationStatus> getResourceResult + = janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); + if (getResourceResult.isRight()) { + final JanusGraphOperationStatus status = getResourceResult.right().value(); + log.debug("Failed to find resource with systemName {}, version {}. Status is {} ", systemName, version, status); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } - return result; + return getToscaElementByOperation(getResourceResult.left().value().get(0)); } public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, @@ -893,7 +880,6 @@ public class ToscaOperationFacade { public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) { - Either result; Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.NAME, name); @@ -907,8 +893,7 @@ public class ToscaOperationFacade { if (getResourceRes.isRight()) { JanusGraphOperationStatus status = getResourceRes.right().value(); log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status); - result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); - return result; + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } return getToscaElementByOperation(getResourceRes.left().value().get(0)); } @@ -1320,14 +1305,22 @@ public class ToscaOperationFacade { /** * @return max counter of component instance Id's, null if not found */ - private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) { - List countersInNames = containerComponent.getComponentInstances().stream() + private Integer getMaxCounterFromNamesAndIds(final Component containerComponent, final String normalizedName) { + final Pattern COUNTER_PATTERN = Pattern.compile(normalizedName + "[\\s_:-]?\\d+$"); + final List countersInNames = containerComponent.getComponentInstances().stream() .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)) - .map(ci -> ci.getNormalizedName().split(normalizedName)[1]).collect(Collectors.toList()); - List countersInIds = containerComponent.getComponentInstances().stream() - .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1]) + .filter(ci -> !ci.getNormalizedName().equals(normalizedName)) + .map(ComponentInstance::getNormalizedName) + .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0)) + .map(nn -> nn.replaceAll("\\D", "")) + .collect(Collectors.toList()); + final List countersInIds = containerComponent.getComponentInstances().stream() + .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)) + .map(ComponentInstance::getUniqueId) + .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0)) + .map(nn -> nn.replaceAll("\\D", "")) .collect(Collectors.toList()); - List namesAndIdsList = new ArrayList<>(countersInNames); + final List namesAndIdsList = new ArrayList<>(countersInNames); namesAndIdsList.addAll(countersInIds); return getMaxInteger(namesAndIdsList); } @@ -1342,7 +1335,6 @@ public class ToscaOperationFacade { maxCounter = currCounter; } } catch (NumberFormatException e) { - continue; } } return currCounter == null ? null : maxCounter; @@ -1350,7 +1342,7 @@ public class ToscaOperationFacade { public Either associateResourceInstances(Component component, String componentId, RequirementCapabilityRelDef requirementDef) { - return nodeTemplateOperation.associateResourceInstances(component, componentId, requirementDef); + return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef); } public Either, StorageOperationStatus> createAndAssociateInputs(Map inputs, String componentId) { @@ -1533,7 +1525,7 @@ public class ToscaOperationFacade { if (StorageOperationStatus.OK == status) { log.debug(COMPONENT_CREATED_SUCCESSFULLY); List inputsResList = null; - if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) { + if (CollectionUtils.isNotEmpty(inputsAsDataDef)) { inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList()); } return Either.left(inputsResList); @@ -1665,6 +1657,49 @@ public class ToscaOperationFacade { return Either.right(status); } + public Either, StorageOperationStatus> associateComponentInstanceInterfacesToComponent( + Map> instInterfaces, + String componentId + ) { + Either getVertexEither = janusGraphDao.getVertexById( + componentId, + JsonParseFlagEnum.NoParse + ); + if (getVertexEither.isRight()) { + log.debug( + COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, + componentId, + getVertexEither.right().value() + ); + return Either.right( + DaoStatusConverter.convertJanusGraphStatusToStorageStatus( + getVertexEither.right().value() + ) + ); + } + GraphVertex vertex = getVertexEither.left().value(); + Map instInterfacesMap = new HashMap<>(); + if (instInterfaces != null) { + + for (Map.Entry> entryInstances : instInterfaces.entrySet()) { + Map incomingInterfacesMap = entryInstances.getValue().entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); + MapInterfaceDataDefinition interfacesMap = new MapInterfaceDataDefinition(); + interfacesMap.setMapToscaDataDefinition(incomingInterfacesMap); + instInterfacesMap.put(entryInstances.getKey(), interfacesMap); + } + } + StorageOperationStatus status = topologyTemplateOperation.associateInstInterfacesToComponent( + vertex, + instInterfacesMap + ); + if (StorageOperationStatus.OK == status) { + log.debug(COMPONENT_CREATED_SUCCESSFULLY); + return Either.left(instInterfacesMap); + } + return Either.right(status); + } + public Either>, StorageOperationStatus> addComponentInstanceInputsToComponent( Component containerComponent, Map> instProperties) { requireNonNull(instProperties); @@ -1756,25 +1791,25 @@ public class ToscaOperationFacade { } public Either>, StorageOperationStatus> addComponentInstanceAttributesToComponent( - final Component containerComponent, final Map> instProperties) { - requireNonNull(instProperties); - for (final Entry> entry : instProperties.entrySet()) { - final List props = entry.getValue(); - if (isEmpty(props)) { + final Component containerComponent, final Map> componentInstanceAttribute) { + requireNonNull(componentInstanceAttribute); + for (final Entry> entry : componentInstanceAttribute.entrySet()) { + final List attributes = entry.getValue(); + if (isEmpty(attributes)) { continue; } final String componentInstanceId = entry.getKey(); - final List originalComponentInstProps = containerComponent.getComponentInstancesAttributes() + final List componentInstanceAttributes = containerComponent.getComponentInstancesAttributes() .get(componentInstanceId); - for (final ComponentInstanceAttribute property : props) { + for (final ComponentInstanceAttribute attribute : attributes) { final StorageOperationStatus status = updateOrAddComponentInstanceAttribute(containerComponent, componentInstanceId, - originalComponentInstProps, property); + componentInstanceAttributes, attribute); if (status != StorageOperationStatus.OK) { return Either.right(status); } } } - return Either.left(instProperties); + return Either.left(componentInstanceAttribute); } private StorageOperationStatus populateAndUpdateInstanceCapProperty(Component containerComponent, String componentInstanceId, @@ -1812,19 +1847,19 @@ public class ToscaOperationFacade { } private StorageOperationStatus updateOrAddComponentInstanceAttribute(Component containerComponent, String componentInstanceId, - List originalComponentInstProps, - ComponentInstanceAttribute property) { + List componentInstanceAttributes, + ComponentInstanceAttribute attribute) { StorageOperationStatus status; - // check if the property already exists or not - Optional instanceProperty = originalComponentInstProps.stream() - .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny(); + // check if the attribute already exists or not + Optional instanceProperty = componentInstanceAttributes.stream() + .filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny(); if (instanceProperty.isPresent()) { - status = updateComponentInstanceAttribute(containerComponent, componentInstanceId, property); + status = updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); } else { - status = addComponentInstanceAttribute(containerComponent, componentInstanceId, property); + status = addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); } if (status != StorageOperationStatus.OK) { - log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status); + log.debug("Failed to update instance attribute {} for instance {} error {} ", attribute, componentInstanceId, status); } return status; } @@ -2082,6 +2117,7 @@ public class ToscaOperationFacade { Component updatedComponent = componentEither.left().value(); component.setCapabilities(updatedComponent.getCapabilities()); component.setRequirements(updatedComponent.getRequirements()); + component.setComponentInstancesRelations(updatedComponent.getComponentInstancesRelations()); component.setComponentInstances(updatedComponent.getComponentInstances()); } @@ -2309,7 +2345,7 @@ public class ToscaOperationFacade { } private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) { - return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE; + return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : TOPOLOGY_TEMPLATE; } private Map propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) { @@ -2381,7 +2417,7 @@ public class ToscaOperationFacade { } if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase( internalComponentType)) { - internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE); + internalVertexTypes.add(TOPOLOGY_TEMPLATE); } return internalVertexTypes; } @@ -2742,7 +2778,7 @@ public class ToscaOperationFacade { } propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - if (distStatus != null && !distStatus.isEmpty()) { + if (CollectionUtils.isNotEmpty(distStatus)) { for (DistributionStatusEnum state : distStatus) { propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name()); Either, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, @@ -2763,8 +2799,12 @@ public class ToscaOperationFacade { Map propertiesToMatch, Map propertiesNotToMatch, String modelName) { - Either, JanusGraphOperationStatus> getRes = janusGraphDao - .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName); + Either, JanusGraphOperationStatus> getRes; + if (StringUtils.isEmpty(modelName)) { + getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); + } else { + getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName); + } if (getRes.isRight()) { if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, @@ -3124,7 +3164,7 @@ public class ToscaOperationFacade { String componentInstanceId) { String uniqueId = componentInstance.getUniqueId(); StorageOperationStatus status = nodeTemplateOperation - .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, + .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, uniqueId); if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { CommonUtility @@ -3287,7 +3327,7 @@ public class ToscaOperationFacade { final Component component) { final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName)); - StorageOperationStatus status = StorageOperationStatus.OK; + StorageOperationStatus status; final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component); if (match) { status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS, @@ -3334,14 +3374,14 @@ public class ToscaOperationFacade { public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) { StorageOperationStatus status = topologyTemplateOperation - .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES); + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES); if (status == StorageOperationStatus.OK) { status = topologyTemplateOperation - .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS); + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS); } if (status == StorageOperationStatus.OK) { status = topologyTemplateOperation - .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES); + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES); } return status; } @@ -3416,7 +3456,7 @@ public class ToscaOperationFacade { log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); } else { - if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) { + if (getVertexEither.left().value().getLabel() != TOPOLOGY_TEMPLATE) { log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel()); result = Either.right(StorageOperationStatus.BAD_REQUEST); }