1 package org.openecomp.sdc.be.model.jsontitan.operations;
4 import org.openecomp.sdc.be.dao.api.ActionStatus;
5 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
6 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
7 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
8 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
9 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
10 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
11 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component;
20 * Created by yavivi on 26/01/2018.
23 public class ExternalReferencesOperation extends BaseOperation {
25 private static final Logger log = LoggerFactory.getLogger(ExternalReferencesOperation.class);
27 public IdMapper getIdMapper() {
31 public void setIdMapper(IdMapper idMapper) {
32 this.idMapper = idMapper;
36 protected IdMapper idMapper;
41 public ExternalReferencesOperation(TitanDao titanDao, NodeTypeOperation nto, TopologyTemplateOperation tto, IdMapper idMapper){
42 this.titanDao = titanDao;
43 this.topologyTemplateOperation = tto;
44 this.nodeTypeOperation = nto;
45 this.idMapper = idMapper;
48 public Either<String, ActionStatus> addExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
49 Either<String, ActionStatus> addResult = this.addExternalReference(serviceUuid, componentInstanceName, objectType, reference);
50 this.titanDao.commit();
54 public Either<String, ActionStatus> deleteExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
55 Either<String, ActionStatus> result = this.deleteExternalReference(serviceUuid, componentInstanceName, objectType, reference);
56 this.titanDao.commit();
60 public Either<String, ActionStatus> updateExternalReferenceWithCommit(String serviceVertexUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
61 Either<String, ActionStatus> updateResult = this.updateExternalReference(serviceVertexUuid, componentInstanceName, objectType, oldRef, newRef);
62 this.titanDao.commit();
66 public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {
69 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
70 if (vertexById.isRight()){
71 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
74 GraphVertex serviceVertex = vertexById.left().value();
76 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
77 if (compInstanceUniqueId == null) {
78 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
81 //Get the external references map vertex
82 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
84 //Check whether data vertex found
85 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
87 //instanceId -> externalRefsMap
88 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = null;
89 if (externalRefsVertex == null) {
90 //External Refs vertext does not exist, create its map.
91 externalReferencesFullData = new HashMap<String, MapComponentInstanceExternalRefs>() {
93 MapComponentInstanceExternalRefs externalRefsMap = new MapComponentInstanceExternalRefs();
94 put(compInstanceUniqueId, externalRefsMap);
98 externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
99 if (externalReferencesFullData.get(compInstanceUniqueId) == null){
100 externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
104 boolean isAdded = this.addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
105 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
107 return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
110 public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
112 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
113 if (vertexById.isRight()){
114 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
116 GraphVertex serviceVertex = vertexById.left().value();
118 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
119 if (compInstanceUniqueId == null) {
120 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
123 //Get the external references map vertex
124 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
126 //Check whether data vertex found
127 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
128 boolean refDeleted = false;
129 if (externalRefsVertex != null) {
130 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
131 if (externalReferencesFullData != null) {
132 refDeleted = this.deleteExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
133 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); //@ TODO if ref deleted
138 return Either.left(reference);
140 return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
144 public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
146 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
147 if (vertexById.isRight()){
148 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
151 GraphVertex serviceVertex = vertexById.left().value();
153 //Map instance_name -> uuid
154 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
155 if (compInstanceUniqueId == null) {
156 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
159 //Get the external references map vertex
160 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
162 //Check whether data vertex found
163 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
164 boolean refReplaced = false;
165 if (externalRefsVertex != null) {
166 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
167 if (externalReferencesFullData != null) {
168 refReplaced = this.updateExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, oldRef, newRef);
169 this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
173 return Either.left(newRef);
175 return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
179 public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
181 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
182 if (vertexById.isRight()){
183 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
186 GraphVertex serviceVertex = vertexById.left().value();
188 Map<String, List<String>> result = new HashMap();
190 //Get the external references map vertex
191 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
192 //Check whether data vertex found
193 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
194 if (externalRefsVertex != null) {
195 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
196 if (externalReferencesFullData != null) {
197 externalReferencesFullData.entrySet().forEach(
199 List<String> externalRefsByObjectType = externalReferencesFullData.get(s.getKey()).getExternalRefsByObjectType(objectType);
200 List<String> refList = externalRefsByObjectType == null ? new ArrayList<>() : externalRefsByObjectType;
201 String key = idMapper.mapUniqueIdToComponentNameTo(s.getKey(), serviceVertex);
202 result.put(key, refList);
205 return Either.left(result);
208 //No external References Node found on this asset
209 return Either.left(new HashMap<>());
212 public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
214 Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
215 if (vertexById.isRight()){
216 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
219 GraphVertex serviceVertex = vertexById.left().value();
220 final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
221 if (compInstanceUniqueId == null) {
222 return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
225 //Get the external references map vertex
226 final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
228 //Check whether data vertex found
229 GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
230 if (externalRefsVertex != null) {
231 Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
232 if (externalReferencesFullData != null) {
233 return Either.left(this.getExternalReferencesByObjectId(externalReferencesFullData, compInstanceUniqueId, objectType));
237 //No external References Node found on this asset
238 return Either.left(new LinkedList());
241 private List<String> getExternalReferencesByObjectId(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType) {
242 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
243 List<String> externalRefsByObjectType = externalRefsMap.getExternalRefsByObjectType(objectType);
244 return externalRefsByObjectType != null ? externalRefsByObjectType : new LinkedList<String>();
247 private boolean updateExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String oldRef, String newRef) {
248 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
249 return externalRefsMap.replaceExternalRef(objectType, oldRef, newRef);
252 private boolean deleteExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
253 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
254 return externalRefsMap.deleteExternalRef(objectType, reference);
257 private boolean addExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
258 MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
259 return externalRefsMap.addExternalRef(objectType, reference);