Added oparent to sdc main
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ExternalReferencesOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23
24 import fj.data.Either;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
29 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
31 import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper;
33 import org.openecomp.sdc.be.model.operations.impl.OperationUtils;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import java.util.*;
38
39 import static java.util.Collections.emptyMap;
40
41 /**
42  * Created by yavivi on 26/01/2018.
43  */
44 @Component
45 public class ExternalReferencesOperation extends BaseOperation {
46
47     @Autowired
48     private IdMapper idMapper;
49
50     @Autowired
51     private OperationUtils operationUtils;
52
53
54     /**
55      * Constructor
56      */
57     public ExternalReferencesOperation(JanusGraphDao janusGraphDao, NodeTypeOperation nto, TopologyTemplateOperation tto, IdMapper idMapper){
58         this.janusGraphDao = janusGraphDao;
59         this.topologyTemplateOperation = tto;
60         this.nodeTypeOperation = nto;
61         this.idMapper = idMapper;
62     }
63
64     public Either<String, ActionStatus> addExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
65         Either<String, ActionStatus> addResult = addExternalReference(serviceUuid, componentInstanceName, objectType, reference);
66         janusGraphDao.commit();
67         return addResult;
68     }
69
70     public Either<String, ActionStatus> deleteExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
71         Either<String, ActionStatus> result = deleteExternalReference(serviceUuid, componentInstanceName, objectType, reference);
72         janusGraphDao.commit();
73         return result;
74     }
75
76     public Either<String, ActionStatus> updateExternalReferenceWithCommit(String serviceVertexUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
77         Either<String, ActionStatus> updateResult = updateExternalReference(serviceVertexUuid, componentInstanceName, objectType, oldRef, newRef);
78         janusGraphDao.commit();
79         return updateResult;
80     }
81
82     public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {
83
84         //Get Container vertex
85         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(assetUuid);
86         if (vertexById.isRight()){
87             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
88         }
89
90         GraphVertex serviceVertex = vertexById.left().value();
91
92         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
93         if (compInstanceUniqueId == null) {
94             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
95         }
96
97         //Get the external references map vertex
98         final Either<GraphVertex, JanusGraphOperationStatus> dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
99
100         //Check whether data vertex found
101         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
102
103         //instanceId -> externalRefsMap
104         Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData;
105         if (externalRefsVertex == null) {
106             //External Refs vertex does not exist, create its map.
107             externalReferencesFullData = new HashMap<>();
108             externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
109         } else {
110             externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
111             externalReferencesFullData.computeIfAbsent(compInstanceUniqueId, k -> new MapComponentInstanceExternalRefs());
112         }
113
114         boolean isAdded = addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
115         updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
116
117         return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
118     }
119
120     public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
121         //Get Service vertex
122         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(assetUuid);
123         if (vertexById.isRight()){
124             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
125         }
126         GraphVertex serviceVertex = vertexById.left().value();
127
128         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
129         if (compInstanceUniqueId == null) {
130             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
131         }
132
133         //Get the external references map vertex
134         final Either<GraphVertex, JanusGraphOperationStatus> dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
135
136         //Check whether data vertex found
137         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
138         boolean refDeleted = false;
139         if (externalRefsVertex != null) {
140             Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
141             if (externalReferencesFullData != null) {
142                 refDeleted = deleteExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
143                 updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
144             }
145         }
146
147         if (refDeleted) {
148             return Either.left(reference);
149         } else {
150             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
151         }
152     }
153
154     public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
155         //Get Service vertex
156         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(assetUuid);
157         if (vertexById.isRight()){
158             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
159         }
160
161         GraphVertex serviceVertex = vertexById.left().value();
162
163         //Map instance_name -> uuid
164         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
165         if (compInstanceUniqueId == null) {
166             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
167         }
168
169         //Get the external references map vertex
170         final Either<GraphVertex, JanusGraphOperationStatus> dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
171
172         //Check whether data vertex found
173         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
174         boolean refReplaced = false;
175         if (externalRefsVertex != null) {
176             Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
177             if (externalReferencesFullData != null) {
178                 refReplaced = updateExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, oldRef, newRef);
179                 updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
180             }
181         }
182         if (refReplaced) {
183             return Either.left(newRef);
184         } else {
185             return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
186         }
187     }
188
189     public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
190         //Get Service vertex
191         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(assetUuid);
192         if (vertexById.isRight()){
193             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
194         }
195
196         GraphVertex serviceVertex = vertexById.left().value();
197
198         Map<String, List<String>> result = new HashMap();
199
200         //Get the external references map vertex
201         final Either<GraphVertex, JanusGraphOperationStatus> dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
202         //Check whether data vertex found
203         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
204         if (externalRefsVertex != null) {
205             Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
206             if (externalReferencesFullData != null) {
207                 externalReferencesFullData.entrySet().forEach(
208                         s -> {
209                             List<String> externalRefsByObjectType = externalReferencesFullData.get(s.getKey()).getExternalRefsByObjectType(objectType);
210                             List<String> refList = externalRefsByObjectType == null ? new ArrayList<>() : externalRefsByObjectType;
211                             String key = idMapper.mapUniqueIdToComponentNameTo(s.getKey(), serviceVertex);
212                             result.put(key, refList);
213                         }
214                 );
215                 return Either.left(result);
216             }
217         }
218         //No external References Node found on this asset
219         return Either.left(new HashMap<>());
220     }
221
222     public void addAllExternalReferences(String containerUniqueId,
223                                          String compInstanceUniqueId,
224                                          Map<String, List<String>> instanceExternalReferences) {
225
226         GraphVertex serviceVertex = janusGraphDao.getVertexById(containerUniqueId)
227                 .left()
228                 .on(operationUtils::onJanusGraphOperationFailure);
229         Either<GraphVertex, JanusGraphOperationStatus> dataVertex = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
230         Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData;
231         if (dataVertex.isLeft()) {
232             externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) dataVertex.left().value().getJson();
233         } else {
234             externalReferencesFullData = new HashMap<>();
235         }
236         externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs(instanceExternalReferences));
237         updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
238     }
239
240     public Map<String, List<String>> getAllExternalReferences(String containerUniqueId,
241                                                               String compInstanceUniqueId) {
242         GraphVertex serviceVertex = janusGraphDao.getVertexById(containerUniqueId)
243             .left()
244             .on(operationUtils::onJanusGraphOperationFailure);
245
246         Either<GraphVertex, JanusGraphOperationStatus> dataVertex = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
247         if (dataVertex.isRight()) {
248             return new HashMap<>();
249         }
250         GraphVertex externalRefsVertex = dataVertex.left().value();
251         Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = externalRefsVertex == null ? null : (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
252         if (externalReferencesFullData != null) {
253             return externalReferencesFullData
254                     .getOrDefault(compInstanceUniqueId, new MapComponentInstanceExternalRefs())
255                     .getComponentInstanceExternalRefs();
256         }
257         return emptyMap();
258     }
259
260     public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
261         //Get Service vertex
262         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(assetUuid);
263         if (vertexById.isRight()){
264             return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
265         }
266
267         GraphVertex serviceVertex = vertexById.left().value();
268         final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
269         if (compInstanceUniqueId == null) {
270             return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
271         }
272
273         //Get the external references map vertex
274         final Either<GraphVertex, JanusGraphOperationStatus> dataVertexResult = getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
275
276         //Check whether data vertex found
277         GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
278         if (externalRefsVertex != null) {
279             Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
280             if (externalReferencesFullData != null) {
281                 return Either.left(getExternalReferencesByObjectId(externalReferencesFullData, compInstanceUniqueId, objectType));
282             }
283         }
284
285         //No external References Node found on this asset
286         return Either.left(new LinkedList());
287     }
288
289     public IdMapper getIdMapper() {
290         return idMapper;
291     }
292
293     public void setIdMapper(IdMapper idMapper) {
294         this.idMapper = idMapper;
295     }
296
297     private List<String> getExternalReferencesByObjectId(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType) {
298         MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
299         List<String> externalRefsByObjectType = externalRefsMap.getExternalRefsByObjectType(objectType);
300         return externalRefsByObjectType != null ? externalRefsByObjectType : new LinkedList<>();
301     }
302
303     private boolean updateExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String oldRef, String newRef) {
304         MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
305         return externalRefsMap.replaceExternalRef(objectType, oldRef, newRef);
306     }
307
308     private boolean deleteExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
309         MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
310         return externalRefsMap.deleteExternalRef(objectType, reference);
311     }
312
313     private boolean addExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
314         MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
315         return externalRefsMap.addExternalRef(objectType, reference);
316     }
317 }