Revert "Interface operation feature enhancements"
[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.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Optional;
26 import java.util.UUID;
27 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
28 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
30 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
33 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
34 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
35 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
36 import org.openecomp.sdc.be.model.ArtifactDefinition;
37 import org.openecomp.sdc.be.model.InterfaceDefinition;
38 import org.openecomp.sdc.be.model.Operation;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
41 import org.springframework.beans.factory.annotation.Autowired;
42
43 @org.springframework.stereotype.Component("interfaces-operation")
44 public class InterfaceOperation extends BaseOperation {
45
46   @Autowired
47   private ArtifactCassandraDao artifactCassandraDao;
48
49   public Either<InterfaceDefinition, StorageOperationStatus> addInterface(String componentId,
50       InterfaceDefinition interfaceDefinition) {
51     return addOrUpdateInterface(false, componentId, interfaceDefinition);
52   }
53
54   public Either<InterfaceDefinition, StorageOperationStatus> updateInterface(String componentId,
55       InterfaceDefinition interfaceDefinition) {
56     return addOrUpdateInterface(true, componentId, interfaceDefinition);
57   }
58
59   private Either<InterfaceDefinition, StorageOperationStatus> addOrUpdateInterface(
60       boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) {
61
62     StorageOperationStatus statusRes;
63     Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
64
65     getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
66     if (getToscaElementRes.isRight()) {
67       TitanOperationStatus status = getToscaElementRes.right().value();
68       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
69       return Either.right(statusRes);
70     }
71     GraphVertex componentVertex = getToscaElementRes.left().value();
72     if (!isUpdateAction) {
73       interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
74     }
75     statusRes = performUpdateToscaAction(isUpdateAction, componentVertex,
76         Collections.singletonList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
77     if (!statusRes.equals(StorageOperationStatus.OK)) {
78       return Either.right(statusRes);
79     }
80     return Either.left(interfaceDefinition);
81   }
82
83   public Either<Operation, StorageOperationStatus> addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
84     return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation);
85   }
86
87   public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
88     return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation);
89   }
90
91   private Either<Operation, StorageOperationStatus> addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) {
92
93     StorageOperationStatus statusRes;
94     Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
95     Either<GraphVertex, TitanOperationStatus> getToscaElementInt;
96
97     if(isUpdateAction && operation.getImplementationArtifact() != null){
98       String artifactUUID = operation.getImplementationArtifact().getArtifactUUID();
99       Either<Long, CassandraOperationStatus> artifactCount = artifactCassandraDao.getCountOfArtifactById(artifactUUID);
100       if(artifactCount.isLeft()){
101         CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID);
102         if (cassandraStatus != CassandraOperationStatus.OK) {
103           return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus));
104         }
105       }
106     }
107
108     getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
109     if (getToscaElementRes.isRight()) {
110       TitanOperationStatus status = getToscaElementRes.right().value();
111       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
112       return Either.right(statusRes);
113     }
114     GraphVertex componentVertex = getToscaElementRes.left().value();
115     getToscaElementInt = titanDao.getChildVertex(componentVertex, EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
116     if (getToscaElementInt.isRight()) {
117       TitanOperationStatus status = getToscaElementInt.right().value();
118       statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
119       return Either.right(statusRes);
120     }
121     GraphVertex interfaceVertex = getToscaElementInt.left().value();
122
123     statusRes = performUpdateToscaAction(isUpdateAction, interfaceVertex,
124         Collections.singletonList(operation), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
125     if (!statusRes.equals(StorageOperationStatus.OK)) {
126       return Either.right(statusRes);
127     }
128
129     getUpdatedInterfaceDef(interfaceDef, operation, operation.getUniqueId());
130     Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
131     if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
132       return Either.right(statusRes);
133     }
134
135     return Either.left(operation);
136   }
137
138   public Either<Operation, StorageOperationStatus> deleteInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, String operationToDelete) {
139     Either<GraphVertex, TitanOperationStatus> getInterfaceVertex;
140     Either<GraphVertex, TitanOperationStatus> getComponentVertex;
141     Operation operation = new Operation();
142     StorageOperationStatus status;
143
144     getComponentVertex = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
145     if (getComponentVertex.isRight()) {
146       return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
147     }
148
149     getInterfaceVertex = titanDao.getChildVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
150     if (getInterfaceVertex.isRight()) {
151       return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getInterfaceVertex.right().value()));
152     }
153
154       if (!interfaceDef.getOperationsMap().isEmpty()) {
155           Either<GraphVertex, TitanOperationStatus> getInterfaceOpVertex =
156                   titanDao.getChildVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION,
157                           JsonParseFlagEnum.NoParse);
158           if (getInterfaceOpVertex.isRight()) {
159               List<ToscaDataDefinition> toscaDataList = new ArrayList<>(interfaceDef.getOperationsMap().values());
160               StorageOperationStatus statusRes =
161                       addToscaDataToToscaElement(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION,
162                               VertexTypeEnum.INTERFACE_OPERATION, toscaDataList, JsonPresentationFields.UNIQUE_ID);
163               if (!statusRes.equals(StorageOperationStatus.OK)) {
164                   return Either.right(statusRes);
165               }
166           }
167       }
168
169     Optional<Entry<String, Operation>> operationToRemove = interfaceDef.getOperationsMap().entrySet().stream()
170         .filter(entry -> entry.getValue().getUniqueId().equals(operationToDelete)).findAny();
171     if (operationToRemove.isPresent()){
172       Map.Entry<String, Operation> stringOperationEntry = operationToRemove.get();
173       operation = stringOperationEntry.getValue();
174       ArtifactDefinition implementationArtifact = operation.getImplementationArtifact();
175       if(implementationArtifact != null){
176         String artifactUUID = implementationArtifact.getArtifactUUID();
177         CassandraOperationStatus cassandraStatus = artifactCassandraDao.deleteArtifact(artifactUUID);
178         if (cassandraStatus != CassandraOperationStatus.OK) {
179           return Either.right(DaoStatusConverter.convertCassandraStatusToStorageStatus(cassandraStatus));
180         }
181       }
182
183       if(interfaceDef.getOperationsMap().size() > 1){
184         status = deleteToscaDataElements(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, Collections.singletonList(operationToDelete));
185         if (status != StorageOperationStatus.OK) {
186           return Either.right(status);
187         }
188       } else {
189         status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
190         if (status != StorageOperationStatus.OK) {
191           return Either.right(status);
192         }
193       }
194
195       getUpdatedInterfaceDef(interfaceDef, null, operationToDelete);
196       if (interfaceDef.getOperations().isEmpty()) {
197         status = deleteToscaDataElements(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, Collections.singletonList(interfaceDef.getUniqueId()));
198         if (status != StorageOperationStatus.OK) {
199           return Either.right(status);
200         }
201         status = removeToscaDataVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
202         if (status != StorageOperationStatus.OK) {
203           return Either.right(status);
204         }
205       }
206       else {
207         Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
208         if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
209           return Either.right(status);
210         }
211       }
212     }
213     return Either.left(operation);
214   }
215
216   private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
217       List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
218     if (isUpdate) {
219       return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
220     } else {
221       return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
222     }
223   }
224
225   private void getUpdatedInterfaceDef(InterfaceDefinition interfaceDef, Operation operation, String operationId){
226     Map<String, Operation> operationMap = interfaceDef.getOperationsMap();
227     if(operation != null){
228       operationMap.put(operationId, operation);
229       interfaceDef.setOperationsMap(operationMap);
230     }
231     else {
232       operationMap.remove(operationId);
233       interfaceDef.setOperationsMap(operationMap);
234     }
235   }
236
237 }
238
239