Interface operation support for service - BE
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / InterfaceOperation.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.model.jsontitan.operations;
18
19 import fj.data.Either;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25 import java.util.UUID;
26 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
27 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
32 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.model.InterfaceDefinition;
37 import org.openecomp.sdc.be.model.Operation;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
40 import org.springframework.beans.factory.annotation.Autowired;
41
42 @org.springframework.stereotype.Component("interfaces-operation")
43 public class InterfaceOperation extends BaseOperation {
44
45   @Autowired
46   private ArtifactCassandraDao artifactCassandraDao;
47
48   public Either<InterfaceDefinition, StorageOperationStatus> addInterface(String componentId,
49       InterfaceDefinition interfaceDefinition) {
50     return addOrUpdateInterface(false, componentId, interfaceDefinition);
51   }
52
53   public Either<InterfaceDefinition, StorageOperationStatus> updateInterface(String componentId,
54       InterfaceDefinition interfaceDefinition) {
55     return addOrUpdateInterface(true, componentId, interfaceDefinition);
56   }
57
58   private Either<InterfaceDefinition, StorageOperationStatus> addOrUpdateInterface(
59       boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) {
60
61     StorageOperationStatus statusRes;
62     Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
63
64     getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
65     if (getToscaElementRes.isRight()) {
66       TitanOperationStatus status = getToscaElementRes.right().value();
67       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
68       return Either.right(statusRes);
69     }
70     GraphVertex componentVertex = getToscaElementRes.left().value();
71     if (!isUpdateAction) {
72       interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
73     }
74     statusRes = performUpdateToscaAction(isUpdateAction, componentVertex,
75         Collections.singletonList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
76     if (!statusRes.equals(StorageOperationStatus.OK)) {
77       return Either.right(statusRes);
78     }
79     return Either.left(interfaceDefinition);
80   }
81
82   public Either<Operation, StorageOperationStatus> addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
83     return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation);
84   }
85
86   public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
87     return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation);
88   }
89
90   private Either<Operation, StorageOperationStatus> addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) {
91
92     StorageOperationStatus statusRes;
93     Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
94     Either<GraphVertex, TitanOperationStatus> getToscaElementInt;
95
96     getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
97     if (getToscaElementRes.isRight()) {
98       TitanOperationStatus status = getToscaElementRes.right().value();
99       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
100       return Either.right(statusRes);
101     }
102     GraphVertex componentVertex = getToscaElementRes.left().value();
103     getToscaElementInt = titanDao.getChildVertex(componentVertex, EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
104     if (getToscaElementInt.isRight()) {
105       TitanOperationStatus status = getToscaElementInt.right().value();
106       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
107       return Either.right(statusRes);
108     }
109     GraphVertex interfaceVertex = getToscaElementInt.left().value();
110
111     statusRes = performUpdateToscaAction(isUpdateAction, interfaceVertex,
112         Collections.singletonList(operation), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
113     if (!statusRes.equals(StorageOperationStatus.OK)) {
114       return Either.right(statusRes);
115     }
116
117     getUpdatedInterfaceDef(interfaceDef, operation, operation.getUniqueId());
118     Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
119     if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
120       return Either.right(statusRes);
121     }
122
123     return Either.left(operation);
124   }
125
126   public Either<Operation, StorageOperationStatus> deleteInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, String operationToDelete) {
127     Either<GraphVertex, TitanOperationStatus> getInterfaceVertex;
128     Either<GraphVertex, TitanOperationStatus> getComponentVertex;
129     Operation operation = new Operation();
130     StorageOperationStatus status;
131
132     getComponentVertex = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
133     if (getComponentVertex.isRight()) {
134       return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
135     }
136
137     getInterfaceVertex = titanDao.getChildVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
138     if (getInterfaceVertex.isRight()) {
139       return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getInterfaceVertex.right().value()));
140     }
141
142     Optional<Entry<String, Operation>> operationToRemove = interfaceDef.getOperationsMap().entrySet().stream()
143         .filter(entry -> entry.getValue().getUniqueId().equals(operationToDelete)).findAny();
144     if (operationToRemove.isPresent()){
145       Map.Entry<String, Operation> stringOperationEntry = operationToRemove.get();
146       operation = stringOperationEntry.getValue();
147       ArtifactDefinition implementationArtifact = operation.getImplementationArtifact();
148       if(implementationArtifact != null){
149         String artifactUUID = implementationArtifact.getArtifactUUID();
150         CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID);
151         if (cassandraStatus != CassandraOperationStatus.OK) {
152           return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus));
153         }
154       }
155
156       if(interfaceDef.getOperationsMap().size() > 1){
157         status = deleteToscaDataElements(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, Collections.singletonList(operationToDelete));
158         if (status != StorageOperationStatus.OK) {
159           return Either.right(status);
160         }
161       } else {
162         status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
163         if (status != StorageOperationStatus.OK) {
164           return Either.right(status);
165         }
166       }
167
168       getUpdatedInterfaceDef(interfaceDef, null, operationToDelete);
169       if (interfaceDef.getOperations().isEmpty()) {
170         status = deleteToscaDataElements(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, Collections.singletonList(interfaceDef.getUniqueId()));
171         if (status != StorageOperationStatus.OK) {
172           return Either.right(status);
173         }
174         status = removeToscaDataVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
175         if (status != StorageOperationStatus.OK) {
176           return Either.right(status);
177         }
178       }
179       else {
180         Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
181         if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
182           return Either.right(status);
183         }
184       }
185     }
186     return Either.left(operation);
187   }
188
189   private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
190       List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
191     if (isUpdate) {
192       return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
193     } else {
194       return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
195     }
196   }
197
198   private void getUpdatedInterfaceDef(InterfaceDefinition interfaceDef, Operation operation, String operationId){
199     Map<String, Operation> operationMap = interfaceDef.getOperationsMap();
200     if(operation != null){
201       operationMap.put(operationId, operation);
202       interfaceDef.setOperationsMap(operationMap);
203     }
204     else {
205       operationMap.remove(operationId);
206       interfaceDef.setOperationsMap(operationMap);
207     }
208   }
209
210 }
211
212