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.jsonjanusgraph.operations;
19 import fj.data.Either;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.apache.commons.collections.MapUtils;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
26 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
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,
39 List<InterfaceDefinition> interfaceDefinitions) {
40 return addOrUpdateInterfaces(false, componentId, interfaceDefinitions);
43 private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction,
44 String componentId, List<InterfaceDefinition> interfaceDefinitions) {
46 List<ToscaDataDefinition> interfaceDataDefinitions =
47 interfaceDefinitions.stream().map(InterfaceDataDefinition::new).collect(Collectors.toList());
48 StorageOperationStatus statusRes =
49 performUpdateToscaAction(isUpdateAction, componentId, interfaceDataDefinitions, EdgeLabelEnum.INTERFACE,
50 VertexTypeEnum.INTERFACE);
51 if (!statusRes.equals(StorageOperationStatus.OK)) {
52 return Either.right(statusRes);
54 return Either.left(interfaceDefinitions);
57 private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, String componentId,
58 List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
60 return updateToscaDataOfToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList,
61 JsonPresentationFields.UNIQUE_ID);
63 return addToscaDataToToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList,
64 JsonPresentationFields.UNIQUE_ID);
68 public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(String componentId,
69 List<InterfaceDefinition> interfaceDefinitions) {
70 return addOrUpdateInterfaces(true, componentId, interfaceDefinitions);
73 public Either<String, StorageOperationStatus> deleteInterface(String componentId, String interfacesToDelete) {
75 StorageOperationStatus statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE,
76 Collections.singletonList(interfacesToDelete));
77 if (!statusRes.equals(StorageOperationStatus.OK)) {
78 return Either.right(statusRes);
81 Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> componentEither =
82 getDataFromGraph(componentId, EdgeLabelEnum.INTERFACE);
83 if (componentEither.isRight()) {
84 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentEither.right().value()));
87 Map<String, InterfaceDataDefinition> interfaceDataDefinitionMap = componentEither.left().value();
88 if (MapUtils.isEmpty(interfaceDataDefinitionMap)) {
89 statusRes = removeToscaData(componentId, EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
90 if (!statusRes.equals(StorageOperationStatus.OK)) {
91 return Either.right(statusRes);
95 return Either.left(interfacesToDelete);