aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ExternalReferencesOperation.java
blob: 2b3569928924e8fcb6fa1001a34694246eef3f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package org.openecomp.sdc.be.model.jsontitan.operations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs;
import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fj.data.Either;

/**
 * Created by yavivi on 26/01/2018.
 */
@Component
public class ExternalReferencesOperation extends BaseOperation {

    private static final Logger log = LoggerFactory.getLogger(ExternalReferencesOperation.class);

    public IdMapper getIdMapper() {
        return idMapper;
    }

    public void setIdMapper(IdMapper idMapper) {
        this.idMapper = idMapper;
    }

    @Autowired
    protected IdMapper idMapper;

    /**
     * Constructor
     */
    public ExternalReferencesOperation(TitanDao titanDao, NodeTypeOperation nto, TopologyTemplateOperation tto, IdMapper idMapper){
        this.titanDao = titanDao;
        this.topologyTemplateOperation = tto;
        this.nodeTypeOperation = nto;
        this.idMapper = idMapper;
    }

    public Either<String, ActionStatus> addExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
        Either<String, ActionStatus> addResult = this.addExternalReference(serviceUuid, componentInstanceName, objectType, reference);
        this.titanDao.commit();
        return addResult;
    }

    public Either<String, ActionStatus> deleteExternalReferenceWithCommit(String serviceUuid, String componentInstanceName, String objectType, String reference) {
        Either<String, ActionStatus> result = this.deleteExternalReference(serviceUuid, componentInstanceName, objectType, reference);
        this.titanDao.commit();
        return result;
    }

    public Either<String, ActionStatus> updateExternalReferenceWithCommit(String serviceVertexUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
        Either<String, ActionStatus> updateResult = this.updateExternalReference(serviceVertexUuid, componentInstanceName, objectType, oldRef, newRef);
        this.titanDao.commit();
        return updateResult;
    }

    public Either<String, ActionStatus> addExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference) {

        //Get Service vertex
        Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
        if (vertexById.isRight()){
            return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
        }

        GraphVertex serviceVertex = vertexById.left().value();

        final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
        if (compInstanceUniqueId == null) {
            return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
        }

        //Get the external references map vertex
        final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);

        //Check whether data vertex found
        GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;

        //instanceId -> externalRefsMap
        Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = null;
        if (externalRefsVertex == null) {
            //External Refs vertext does not exist, create its map.
            externalReferencesFullData = new HashMap<String, MapComponentInstanceExternalRefs>() {
                {
                    MapComponentInstanceExternalRefs externalRefsMap = new MapComponentInstanceExternalRefs();
                    put(compInstanceUniqueId, externalRefsMap);
                }
            };
        } else {
            externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
            if (externalReferencesFullData.get(compInstanceUniqueId) == null){
                externalReferencesFullData.put(compInstanceUniqueId, new MapComponentInstanceExternalRefs());
            }
        }

        boolean isAdded = this.addExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
        this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);

        return isAdded ? Either.left(reference) : Either.right(ActionStatus.EXT_REF_ALREADY_EXIST);
    }

    public Either<String, ActionStatus> deleteExternalReference(String assetUuid, String componentInstanceName, String objectType, String reference){
        //Get Service vertex
        Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
        if (vertexById.isRight()){
            return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
        }
        GraphVertex serviceVertex = vertexById.left().value();

        final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
        if (compInstanceUniqueId == null) {
            return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
        }

        //Get the external references map vertex
        final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);

        //Check whether data vertex found
        GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
        boolean refDeleted = false;
        if (externalRefsVertex != null) {
            Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
            if (externalReferencesFullData != null) {
                refDeleted = this.deleteExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, reference);
                this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData); //@ TODO if ref deleted
            }
        }

        if (refDeleted) {
            return Either.left(reference);
        } else {
            return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
        }
    }

    public Either<String, ActionStatus> updateExternalReference(String assetUuid, String componentInstanceName, String objectType, String oldRef, String newRef) {
        //Get Service vertex
        Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
        if (vertexById.isRight()){
            return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
        }

        GraphVertex serviceVertex = vertexById.left().value();

        //Map instance_name -> uuid
        final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
        if (compInstanceUniqueId == null) {
            return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
        }

        //Get the external references map vertex
        final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);

        //Check whether data vertex found
        GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
        boolean refReplaced = false;
        if (externalRefsVertex != null) {
            Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
            if (externalReferencesFullData != null) {
                refReplaced = this.updateExternalRef(externalReferencesFullData, compInstanceUniqueId, objectType, oldRef, newRef);
                this.updateFullToscaData(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, VertexTypeEnum.EXTERNAL_REF, externalReferencesFullData);
            }
        }
        if (refReplaced) {
            return Either.left(newRef);
        } else {
            return Either.right(ActionStatus.EXT_REF_NOT_FOUND);
        }
    }

    public Either<Map<String, List<String>>, ActionStatus> getExternalReferences(String assetUuid, String objectType) {
        //Get Service vertex
        Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
        if (vertexById.isRight()){
            return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
        }

        GraphVertex serviceVertex = vertexById.left().value();

        Map<String, List<String>> result = new HashMap();

        //Get the external references map vertex
        final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);
        //Check whether data vertex found
        GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
        if (externalRefsVertex != null) {
            Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
            if (externalReferencesFullData != null) {
                externalReferencesFullData.entrySet().forEach(
                        s -> {
                            List<String> externalRefsByObjectType = externalReferencesFullData.get(s.getKey()).getExternalRefsByObjectType(objectType);
                            List<String> refList = externalRefsByObjectType == null ? new ArrayList<>() : externalRefsByObjectType;
                            String key = idMapper.mapUniqueIdToComponentNameTo(s.getKey(), serviceVertex);
                            result.put(key, refList);
                        }
                );
                return Either.left(result);
            }
        }
        //No external References Node found on this asset
        return Either.left(new HashMap<>());
    }

    public Either<List<String>, ActionStatus> getExternalReferences(String assetUuid, String componentInstanceName, String objectType) {
        //Get Service vertex
        Either<GraphVertex, TitanOperationStatus> vertexById = this.titanDao.getVertexById(assetUuid);
        if (vertexById.isRight()){
            return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
        }

        GraphVertex serviceVertex = vertexById.left().value();
        final String compInstanceUniqueId = idMapper.mapComponentNameToUniqueId(componentInstanceName, serviceVertex);
        if (compInstanceUniqueId == null) {
            return Either.right(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
        }

        //Get the external references map vertex
        final Either<GraphVertex, TitanOperationStatus> dataVertexResult = this.getDataVertex(serviceVertex, EdgeLabelEnum.EXTERNAL_REFS);

        //Check whether data vertex found
        GraphVertex externalRefsVertex = dataVertexResult.isLeft() ? dataVertexResult.left().value() : null;
        if (externalRefsVertex != null) {
            Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData = (Map<String, MapComponentInstanceExternalRefs>) externalRefsVertex.getJson();
            if (externalReferencesFullData != null) {
                return Either.left(this.getExternalReferencesByObjectId(externalReferencesFullData, compInstanceUniqueId, objectType));
            }
        }

        //No external References Node found on this asset
        return Either.left(new LinkedList());
    }

    private List<String> getExternalReferencesByObjectId(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType) {
        MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
        List<String> externalRefsByObjectType = externalRefsMap.getExternalRefsByObjectType(objectType);
        return externalRefsByObjectType != null ? externalRefsByObjectType : new LinkedList<String>();
    }

    private boolean updateExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String oldRef, String newRef) {
        MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
        return externalRefsMap.replaceExternalRef(objectType, oldRef, newRef);
    }

    private boolean deleteExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
        MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
        return externalRefsMap.deleteExternalRef(objectType, reference);
    }

    private boolean addExternalRef(Map<String, MapComponentInstanceExternalRefs> externalReferencesFullData, String componentInstanceId, String objectType, String reference) {
        MapComponentInstanceExternalRefs externalRefsMap = externalReferencesFullData.get(componentInstanceId);
        return externalRefsMap.addExternalRef(objectType, reference);
    }
}