2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.model.jsontitan.operations;
19 import fj.data.Either;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
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;
43 @org.springframework.stereotype.Component("interfaces-operation")
44 public class InterfaceOperation extends BaseOperation {
47 private ArtifactCassandraDao artifactCassandraDao;
49 public Either<InterfaceDefinition, StorageOperationStatus> addInterface(String componentId,
50 InterfaceDefinition interfaceDefinition) {
51 return addOrUpdateInterface(false, componentId, interfaceDefinition);
54 public Either<InterfaceDefinition, StorageOperationStatus> updateInterface(String componentId,
55 InterfaceDefinition interfaceDefinition) {
56 return addOrUpdateInterface(true, componentId, interfaceDefinition);
59 private Either<InterfaceDefinition, StorageOperationStatus> addOrUpdateInterface(
60 boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) {
62 StorageOperationStatus statusRes;
63 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
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);
71 GraphVertex componentVertex = getToscaElementRes.left().value();
72 if (!isUpdateAction) {
73 interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
75 statusRes = performUpdateToscaAction(isUpdateAction, componentVertex,
76 Collections.singletonList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
77 if (!statusRes.equals(StorageOperationStatus.OK)) {
78 return Either.right(statusRes);
80 return Either.left(interfaceDefinition);
83 public Either<Operation, StorageOperationStatus> addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
84 return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation);
87 public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
88 return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation);
91 private Either<Operation, StorageOperationStatus> addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) {
93 StorageOperationStatus statusRes;
94 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
95 Either<GraphVertex, TitanOperationStatus> getToscaElementInt;
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));
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);
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);
121 GraphVertex interfaceVertex = getToscaElementInt.left().value();
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);
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);
135 return Either.left(operation);
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;
144 getComponentVertex = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
145 if (getComponentVertex.isRight()) {
146 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
149 getInterfaceVertex = titanDao.getChildVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
150 if (getInterfaceVertex.isRight()) {
151 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getInterfaceVertex.right().value()));
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);
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));
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);
189 status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
190 if (status != StorageOperationStatus.OK) {
191 return Either.right(status);
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);
201 status = removeToscaDataVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
202 if (status != StorageOperationStatus.OK) {
203 return Either.right(status);
207 Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
208 if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
209 return Either.right(status);
213 return Either.left(operation);
216 private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
217 List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
219 return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
221 return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
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);
232 operationMap.remove(operationId);
233 interfaceDef.setOperationsMap(operationMap);