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;
22 import java.util.stream.Collectors;
23 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
24 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
25 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
26 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
27 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
28 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
29 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 import org.openecomp.sdc.be.model.InterfaceDefinition;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
35 @org.springframework.stereotype.Component("interfaces-operation")
36 public class InterfaceOperation extends BaseOperation {
38 public Either<List<InterfaceDefinition>, StorageOperationStatus> addInterfaces(String componentId, List<InterfaceDefinition> interfaceDefinitions) {
39 return addOrUpdateInterfaces(false, componentId, interfaceDefinitions);
42 public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(String componentId, List<InterfaceDefinition> interfaceDefinitions) {
43 return addOrUpdateInterfaces(true, componentId, interfaceDefinitions);
46 private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction, String componentId, List<InterfaceDefinition> interfaceDefinitions) {
48 StorageOperationStatus statusRes;
49 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
51 getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
52 if (getToscaElementRes.isRight()) {
53 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value()));
55 GraphVertex componentVertex = getToscaElementRes.left().value();
57 List<ToscaDataDefinition> interfaceDataDefinitions = interfaceDefinitions.stream().map(interfaceDefinition -> new InterfaceDataDefinition(interfaceDefinition)).collect(Collectors.toList());
58 statusRes = performUpdateToscaAction(isUpdateAction, componentVertex, interfaceDataDefinitions, EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
59 if (!statusRes.equals(StorageOperationStatus.OK)) {
60 return Either.right(statusRes);
62 return Either.left(interfaceDefinitions);
65 public Either<String, StorageOperationStatus> deleteInterface(String componentId, String interfacesToDelete) {
66 StorageOperationStatus statusRes;
67 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
69 getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
70 if (getToscaElementRes.isRight()) {
71 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value()));
74 statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE, Collections.singletonList(interfacesToDelete));
75 if (statusRes != StorageOperationStatus.OK) {
76 return Either.right(statusRes);
79 return Either.left(interfacesToDelete);
82 private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
83 List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
85 return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
87 return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);