/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; import static java.util.Collections.emptyMap; import fj.data.Either; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper; import org.openecomp.sdc.be.model.operations.impl.OperationUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * Created by yavivi on 26/01/2018. */ @Component public class ExternalReferencesOperation extends BaseOperation { @Autowired private IdMapper idMapper; @Autowired private OperationUtils operationUtils; /** * Constructor */ public ExternalReferencesOperation(JanusGraphDao janusGraphDao, NodeTypeOperation nto, TopologyTemplateOperation tto, IdMapper idMapper) { this.janusGraphDao = janusGraphDao; this.topologyTemplateOperation = tto; this.nodeTypeOperation = nto; this.idMapper = idMapper; } public Either addExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) { Either addResult = addExternalReference(serviceUuid, componentInstanceName, objectType, reference); janusGraphDao.commit(); return addResult; } public Either deleteExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) { Either result = deleteExternalReference(serviceUuid, componentInstanceName, objectType, reference); janusGraphDao.commit(); return result; } public Either updateExternalReferenceWithCommit(String serviceVertexUuid, String componentInstanceName, String objectType, String oldRef, String newRef) { Either updateResult = updateExternalReference(serviceVertexUuid, componentInstanceName, objectType, oldRef, newRef); janusGraphDao.commit(); return updateResult; } public Either addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) { //Get Container vertex Either vertexById = janusGraphDao.getVertexById(assetUuid); if (vertexById.isRight()) { return Either.right(ActionStatus.RESOURCE_NOT_FOUND); } GraphVertex serviceVertex = vertexById.left().value(); final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex); if (compInstanceUniqueId == null) { return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND); } //Get the external references map vertex final Either dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); //Check whether data vertex found GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null; //instanceId -> externalRefsMap Map externalReferencesFullData; if (externalRefsVertex == null) { //External Refs vertex does not exist, create its map. externalReferencesFullData = new HashMap<>(); externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs()); } else { externalReferencesFullData = (Map) externalRefsVertex.getJson(); externalReferencesFullData.computeIfAbsent(compInstanceUniqueId, k -> new MapComponentInstanceExternalRefs()); } boolean isAdded = addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference); updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST); } public Either deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) { //Get Service vertex Either vertexById = janusGraphDao.getVertexById(assetUuid); if (vertexById.isRight()) { return Either.right(ActionStatus.RESOURCE_NOT_FOUND); } GraphVertex serviceVertex = vertexById.left().value(); final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex); if (compInstanceUniqueId == null) { return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND); } //Get the external references map vertex final Either dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); //Check whether data vertex found GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null; boolean refDeleted = false; if (externalRefsVertex != null) { Map externalReferencesFullData = (Map) externalRefsVertex .getJson(); if (externalReferencesFullData != null) { refDeleted = deleteExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference); updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); } } if (refDeleted) { return Either.left(reference); } else { return Either.right(ActionStatus.EXT_REF_NOT_FOUND); } } public Either updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) { //Get Service vertex Either vertexById = janusGraphDao.getVertexById(assetUuid); if (vertexById.isRight()) { return Either.right(ActionStatus.RESOURCE_NOT_FOUND); } GraphVertex serviceVertex = vertexById.left().value(); //Map instance_name -> uuid final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex); if (compInstanceUniqueId == null) { return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND); } //Get the external references map vertex final Either dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); //Check whether data vertex found GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null; boolean refReplaced = false; if (externalRefsVertex != null) { Map externalReferencesFullData = (Map) externalRefsVertex .getJson(); if (externalReferencesFullData != null) { refReplaced = updateExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, oldRef, newRef); updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); } } if (refReplaced) { return Either.left(newRef); } else { return Either.right(ActionStatus.EXT_REF_NOT_FOUND); } } public Either>, ActionStatus> getExternalReferences(String assetUuid, String objectType) { //Get Service vertex Either vertexById = janusGraphDao.getVertexById(assetUuid); if (vertexById.isRight()) { return Either.right(ActionStatus.RESOURCE_NOT_FOUND); } GraphVertex serviceVertex = vertexById.left().value(); Map> result = new HashMap(); //Get the external references map vertex final Either dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); //Check whether data vertex found GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null; if (externalRefsVertex != null) { Map externalReferencesFullData = (Map) externalRefsVertex .getJson(); if (externalReferencesFullData != null) { externalReferencesFullData.entrySet().forEach(s -> { List externalRefsByObjectType = externalReferencesFullData.get(s.getKey()).getExternalRefsByObjectType(objectType); List refList = externalRefsByObjectType == null ? new ArrayList<>() : externalRefsByObjectType; String key = idMapper.mapUniqueIdToComponentNameTo(s.getKey(), serviceVertex); result.put(key, refList); }); return Either.left(result); } } //No external References Node found on this asset return Either.left(new HashMap<>()); } public void addAllExternalReferences(String containerUniqueId, String compInstanceUniqueId, Map> instanceExternalReferences) { GraphVertex serviceVertex = janusGraphDao.getVertexById(containerUniqueId).left().on(operationUtils::onJanusGraphOperationFailure); Either dataVertex = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); Map externalReferencesFullData; if (dataVertex.isLeft()) { externalReferencesFullData = (Map) dataVertex.left().value().getJson(); } else { externalReferencesFullData = new HashMap<>(); } externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs(instanceExternalReferences)); updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); } public Map> getAllExternalReferences(String containerUniqueId, String compInstanceUniqueId) { GraphVertex serviceVertex = janusGraphDao.getVertexById(containerUniqueId).left().on(operationUtils::onJanusGraphOperationFailure); Either dataVertex = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); if (dataVertex.isRight()) { return new HashMap<>(); } GraphVertex externalRefsVertex = dataVertex.left().value(); Map externalReferencesFullData = externalRefsVertex == null ? null : (Map) externalRefsVertex.getJson(); if (externalReferencesFullData != null) { return externalReferencesFullData.getOrDefault(compInstanceUniqueId, new MapComponentInstanceExternalRefs()) .getComponentInstanceExternalRefs(); } return emptyMap(); } public Either, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) { //Get Service vertex Either vertexById = janusGraphDao.getVertexById(assetUuid); if (vertexById.isRight()) { return Either.right(ActionStatus.RESOURCE_NOT_FOUND); } GraphVertex serviceVertex = vertexById.left().value(); final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex); if (compInstanceUniqueId == null) { return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND); } //Get the external references map vertex final Either dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS); //Check whether data vertex found GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null; if (externalRefsVertex != null) { Map externalReferencesFullData = (Map) externalRefsVertex .getJson(); if (externalReferencesFullData != null) { return Either.left(getExternalReferencesByObjectId(externalReferencesFullData, compInstanceUniqueId, objectType)); } } //No external References Node found on this asset return Either.left(new LinkedList()); } public IdMapper getIdMapper() { return idMapper; } public void setIdMapper(IdMapper idMapper) { this.idMapper = idMapper; } private List getExternalReferencesByObjectId(Map externalReferencesFullData, String componentInstanceId, String objectType) { MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId); List externalRefsByObjectType = externalRefsMap.getExternalRefsByObjectType(objectType); return externalRefsByObjectType != null ? externalRefsByObjectType : new LinkedList<>(); } private boolean updateExternalRef(Map externalReferencesFullData, String componentInstanceId, String objectType, String oldRef, String newRef) { MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId); return externalRefsMap.replaceExternalRef(objectType, oldRef, newRef); } private boolean deleteExternalRef(Map externalReferencesFullData, String componentInstanceId, String objectType, String reference) { MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId); return externalRefsMap.deleteExternalRef(objectType, reference); } private boolean addExternalRef(Map externalReferencesFullData, String componentInstanceId, String objectType, String reference) { MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId); return externalRefsMap.addExternalRef(objectType, reference); } }