1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.LinkedList;
9 import org.openecomp.sdc.be.dao.api.ActionStatus;
10 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
11 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
12 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
13 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
14 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
15 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
16 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Component;
22 import fj.data.Either;
25 * Created by yavivi on 26/01/2018.
28 public class ExternalReferencesOperation extends BaseOperation {
30 private static final Logger log = LoggerFactory.getLogger(ExternalReferencesOperation.class);
32 public IdMapper getIdMapper() {
36 public void setIdMapper(IdMapper idMapper) {
37 this.idMapper = idMapper;
41 protected IdMapper idMapper;
46 public ExternalReferencesOperation(TitanDao titanDao, NodeTypeOperation nto, TopologyTemplateOperation tto, IdMapper idMapper){
47 this.titanDao = titanDao;
48 this.topologyTemplateOperation = tto;
49 this.nodeTypeOperation = nto;
50 this.idMapper = idMapper;
53 public Either<String, ActionStatus> addExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
54 Either<String, ActionStatus> addResult = this.addExternalReference(serviceUuid, componentInstanceName, objectType, reference);
55 this.titanDao.commit();
59 public Either<String, ActionStatus> deleteExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
60 Either<String, ActionStatus> result = this.deleteExternalReference(serviceUuid, componentInstanceName, objectType, reference);
61 this.titanDao.commit();
65 public Either<String, ActionStatus> updateExternalReferenceWithCommit(String serviceVertexUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
66 Either<String, ActionStatus> updateResult = this.updateExternalReference(serviceVertexUuid, componentInstanceName, objectType, oldRef, newRef);
67 this.titanDao.commit();
71 public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {
74 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
75 if (vertexById.isRight()){
76 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
79 GraphVertex serviceVertex = vertexById.left().value();
81 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
82 if (compInstanceUniqueId == null) {
83 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
86 //Get the external references map vertex
87 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
89 //Check whether data vertex found
90 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
92 //instanceId -> externalRefsMap
93 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = null;
94 if (externalRefsVertex == null) {
95 //External Refs vertext does not exist, create its map.
96 externalReferencesFullData = new HashMap<String, MapComponentInstanceExternalRefs>() {
98 MapComponentInstanceExternalRefs externalRefsMap = new MapComponentInstanceExternalRefs();
99 put(compInstanceUniqueId, externalRefsMap);
103 externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
104 if (externalReferencesFullData.get(compInstanceUniqueId) == null){
105 externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
109 boolean isAdded = this.addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
110 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
112 return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
115 public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
117 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
118 if (vertexById.isRight()){
119 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
121 GraphVertex serviceVertex = vertexById.left().value();
123 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
124 if (compInstanceUniqueId == null) {
125 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
128 //Get the external references map vertex
129 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
131 //Check whether data vertex found
132 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
133 boolean refDeleted = false;
134 if (externalRefsVertex != null) {
135 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
136 if (externalReferencesFullData != null) {
137 refDeleted = this.deleteExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
138 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); //@ TODO if ref deleted
143 return Either.left(reference);
145 return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
149 public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
151 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
152 if (vertexById.isRight()){
153 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
156 GraphVertex serviceVertex = vertexById.left().value();
158 //Map instance_name -> uuid
159 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
160 if (compInstanceUniqueId == null) {
161 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
164 //Get the external references map vertex
165 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
167 //Check whether data vertex found
168 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
169 boolean refReplaced = false;
170 if (externalRefsVertex != null) {
171 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
172 if (externalReferencesFullData != null) {
173 refReplaced = this.updateExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, oldRef, newRef);
174 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
178 return Either.left(newRef);
180 return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
184 public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
186 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
187 if (vertexById.isRight()){
188 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
191 GraphVertex serviceVertex = vertexById.left().value();
193 Map<String, List<String>> result = new HashMap();
195 //Get the external references map vertex
196 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
197 //Check whether data vertex found
198 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
199 if (externalRefsVertex != null) {
200 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
201 if (externalReferencesFullData != null) {
202 externalReferencesFullData.entrySet().forEach(
204 List<String> externalRefsByObjectType = externalReferencesFullData.get(s.getKey()).getExternalRefsByObjectType(objectType);
205 List<String> refList = externalRefsByObjectType == null ? new ArrayList<>() : externalRefsByObjectType;
206 String key = idMapper.mapUniqueIdToComponentNameTo(s.getKey(), serviceVertex);
207 result.put(key, refList);
210 return Either.left(result);
213 //No external References Node found on this asset
214 return Either.left(new HashMap<>());
217 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
219 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
220 if (vertexById.isRight()){
221 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
224 GraphVertex serviceVertex = vertexById.left().value();
225 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
226 if (compInstanceUniqueId == null) {
227 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
230 //Get the external references map vertex
231 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
233 //Check whether data vertex found
234 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
235 if (externalRefsVertex != null) {
236 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
237 if (externalReferencesFullData != null) {
238 return Either.left(this.getExternalReferencesByObjectId(externalReferencesFullData, compInstanceUniqueId, objectType));
242 //No external References Node found on this asset
243 return Either.left(new LinkedList());
246 private List<String> getExternalReferencesByObjectId(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType) {
247 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
248 List<String> externalRefsByObjectType = externalRefsMap.getExternalRefsByObjectType(objectType);
249 return externalRefsByObjectType != null ? externalRefsByObjectType : new LinkedList<String>();
252 private boolean updateExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String oldRef, String newRef) {
253 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
254 return externalRefsMap.replaceExternalRef(objectType, oldRef, newRef);
257 private boolean deleteExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
258 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
259 return externalRefsMap.deleteExternalRef(objectType, reference);
262 private boolean addExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
263 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
264 return externalRefsMap.addExternalRef(objectType, reference);