Sync Integ to Master
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / ExternalReferencesOperation.java
1 package org.openecomp.sdc.be.model.jsontitan.operations;
2
3 import fj.data.Either;
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;
16
17 import java.util.*;
18
19 /**
20  * Created by yavivi on 26/01/2018.
21  */
22 @Component
23 public class ExternalReferencesOperation extends BaseOperation {
24
25     private static final Logger log = LoggerFactory.getLogger(ExternalReferencesOperation.class);
26
27     public IdMapper getIdMapper() {
28         return idMapper;
29     }
30
31     public void setIdMapper(IdMapper idMapper) {
32         this.idMapper = idMapper;
33     }
34
35     @Autowired
36     protected IdMapper idMapper;
37
38     /**
39      * Constructor
40      */
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;
46     }
47
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();
51         return addResult;
52     }
53
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();
57         return result;
58     }
59
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();
63         return updateResult;
64     }
65
66     public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {
67
68         //Get Service vertex
69         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
70         if (vertexById.isRight()){
71             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
72         }
73
74         GraphVertex serviceVertex = vertexById.left().value();
75
76         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
77         if (compInstanceUniqueId == null) {
78             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
79         }
80
81         //Get the external references map vertex
82         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
83
84         //Check whether data vertex found
85         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
86
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>() {
92                 {
93                     MapComponentInstanceExternalRefs externalRefsMap = new MapComponentInstanceExternalRefs();
94                     put(compInstanceUniqueId, externalRefsMap);
95                 }
96             };
97         } else {
98             externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
99             if (externalReferencesFullData.get(compInstanceUniqueId) == null){
100                 externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
101             }
102         }
103
104         boolean isAdded = this.addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
105         this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
106
107         return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
108     }
109
110     public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
111         //Get Service vertex
112         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
113         if (vertexById.isRight()){
114             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
115         }
116         GraphVertex serviceVertex = vertexById.left().value();
117
118         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
119         if (compInstanceUniqueId == null) {
120             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
121         }
122
123         //Get the external references map vertex
124         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
125
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
134             }
135         }
136
137         if (refDeleted) {
138             return Either.left(reference);
139         } else {
140             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
141         }
142     }
143
144     public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
145         //Get Service vertex
146         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
147         if (vertexById.isRight()){
148             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
149         }
150
151         GraphVertex serviceVertex = vertexById.left().value();
152
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);
157         }
158
159         //Get the external references map vertex
160         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
161
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);
170             }
171         }
172         if (refReplaced) {
173             return Either.left(newRef);
174         } else {
175             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
176         }
177     }
178
179     public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
180         //Get Service vertex
181         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
182         if (vertexById.isRight()){
183             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
184         }
185
186         GraphVertex serviceVertex = vertexById.left().value();
187
188         Map<String, List<String>> result = new HashMap();
189
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(
198                         s -> {
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);
203                         }
204                 );
205                 return Either.left(result);
206             }
207         }
208         //No external References Node found on this asset
209         return Either.left(new HashMap<>());
210     }
211
212     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
213         //Get Service vertex
214         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
215         if (vertexById.isRight()){
216             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
217         }
218
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);
223         }
224
225         //Get the external references map vertex
226         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
227
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));
234             }
235         }
236
237         //No external References Node found on this asset
238         return Either.left(new LinkedList());
239     }
240
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>();
245     }
246
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);
250     }
251
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);
255     }
256
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);
260     }
261 }