2b3569928924e8fcb6fa1001a34694246eef3f13
[sdc.git] /
1 package org.openecomp.sdc.be.model.jsontitan.operations;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.List;
7 import java.util.Map;
8
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;
21
22 import fj.data.Either;
23
24 /**
25  * Created by yavivi on 26/01/2018.
26  */
27 @Component
28 public class ExternalReferencesOperation extends BaseOperation {
29
30     private static final Logger log = LoggerFactory.getLogger(ExternalReferencesOperation.class);
31
32     public IdMapper getIdMapper() {
33         return idMapper;
34     }
35
36     public void setIdMapper(IdMapper idMapper) {
37         this.idMapper = idMapper;
38     }
39
40     @Autowired
41     protected IdMapper idMapper;
42
43     /**
44      * Constructor
45      */
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;
51     }
52
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();
56         return addResult;
57     }
58
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();
62         return result;
63     }
64
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();
68         return updateResult;
69     }
70
71     public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {
72
73         //Get Service vertex
74         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
75         if (vertexById.isRight()){
76             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
77         }
78
79         GraphVertex serviceVertex = vertexById.left().value();
80
81         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
82         if (compInstanceUniqueId == null) {
83             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
84         }
85
86         //Get the external references map vertex
87         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
88
89         //Check whether data vertex found
90         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
91
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>() {
97                 {
98                     MapComponentInstanceExternalRefs externalRefsMap = new MapComponentInstanceExternalRefs();
99                     put(compInstanceUniqueId, externalRefsMap);
100                 }
101             };
102         } else {
103             externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
104             if (externalReferencesFullData.get(compInstanceUniqueId) == null){
105                 externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
106             }
107         }
108
109         boolean isAdded = this.addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
110         this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
111
112         return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
113     }
114
115     public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
116         //Get Service vertex
117         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
118         if (vertexById.isRight()){
119             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
120         }
121         GraphVertex serviceVertex = vertexById.left().value();
122
123         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
124         if (compInstanceUniqueId == null) {
125             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
126         }
127
128         //Get the external references map vertex
129         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
130
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
139             }
140         }
141
142         if (refDeleted) {
143             return Either.left(reference);
144         } else {
145             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
146         }
147     }
148
149     public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
150         //Get Service vertex
151         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
152         if (vertexById.isRight()){
153             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
154         }
155
156         GraphVertex serviceVertex = vertexById.left().value();
157
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);
162         }
163
164         //Get the external references map vertex
165         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
166
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);
175             }
176         }
177         if (refReplaced) {
178             return Either.left(newRef);
179         } else {
180             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
181         }
182     }
183
184     public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
185         //Get Service vertex
186         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
187         if (vertexById.isRight()){
188             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
189         }
190
191         GraphVertex serviceVertex = vertexById.left().value();
192
193         Map<String, List<String>> result = new HashMap();
194
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(
203                         s -> {
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);
208                         }
209                 );
210                 return Either.left(result);
211             }
212         }
213         //No external References Node found on this asset
214         return Either.left(new HashMap<>());
215     }
216
217     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
218         //Get Service vertex
219         Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
220         if (vertexById.isRight()){
221             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
222         }
223
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);
228         }
229
230         //Get the external references map vertex
231         final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
232
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));
239             }
240         }
241
242         //No external References Node found on this asset
243         return Either.left(new LinkedList());
244     }
245
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>();
250     }
251
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);
255     }
256
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);
260     }
261
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);
265     }
266 }