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.Collections;
21 import java.util.List;
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;
42 @org.springframework.stereotype.Component("interfaces-operation")
43 public class InterfaceOperation extends BaseOperation {
46 private ArtifactCassandraDao artifactCassandraDao;
48 public Either<InterfaceDefinition, StorageOperationStatus> addInterface(String componentId,
49 InterfaceDefinition interfaceDefinition) {
50 return addOrUpdateInterface(false, componentId, interfaceDefinition);
53 public Either<InterfaceDefinition, StorageOperationStatus> updateInterface(String componentId,
54 InterfaceDefinition interfaceDefinition) {
55 return addOrUpdateInterface(true, componentId, interfaceDefinition);
58 private Either<InterfaceDefinition, StorageOperationStatus> addOrUpdateInterface(
59 boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDefinition) {
61 StorageOperationStatus statusRes;
62 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
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);
70 GraphVertex componentVertex = getToscaElementRes.left().value();
71 if (!isUpdateAction) {
72 interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
74 statusRes = performUpdateToscaAction(isUpdateAction, componentVertex,
75 Collections.singletonList(interfaceDefinition), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
76 if (!statusRes.equals(StorageOperationStatus.OK)) {
77 return Either.right(statusRes);
79 return Either.left(interfaceDefinition);
82 public Either<Operation, StorageOperationStatus> addInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
83 return addOrUpdateInterfaceOperation(false, componentId, interfaceDef, interfaceOperation);
86 public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String componentId, InterfaceDefinition interfaceDef, Operation interfaceOperation) {
87 return addOrUpdateInterfaceOperation(true, componentId, interfaceDef, interfaceOperation);
90 private Either<Operation, StorageOperationStatus> addOrUpdateInterfaceOperation(boolean isUpdateAction, String componentId, InterfaceDefinition interfaceDef, Operation operation) {
92 StorageOperationStatus statusRes;
93 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
94 Either<GraphVertex, TitanOperationStatus> getToscaElementInt;
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);
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);
109 GraphVertex interfaceVertex = getToscaElementInt.left().value();
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);
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);
123 return Either.left(operation);
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;
132 getComponentVertex = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
133 if (getComponentVertex.isRight()) {
134 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
137 getInterfaceVertex = titanDao.getChildVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.NoParse);
138 if (getInterfaceVertex.isRight()) {
139 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getInterfaceVertex.right().value()));
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));
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);
162 status = removeToscaDataVertex(getInterfaceVertex.left().value(), EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION);
163 if (status != StorageOperationStatus.OK) {
164 return Either.right(status);
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);
174 status = removeToscaDataVertex(getComponentVertex.left().value(), EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
175 if (status != StorageOperationStatus.OK) {
176 return Either.right(status);
180 Either<InterfaceDefinition, StorageOperationStatus> intUpdateStatus = updateInterface(componentId, interfaceDef);
181 if (intUpdateStatus.isRight() && !intUpdateStatus.right().value().equals(StorageOperationStatus.OK)) {
182 return Either.right(status);
186 return Either.left(operation);
189 private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
190 List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
192 return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
194 return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
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);
205 operationMap.remove(operationId);
206 interfaceDef.setOperationsMap(operationMap);