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.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.stream.Collectors;
24 import org.apache.commons.collections.MapUtils;
25 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
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;
34
35 @org.springframework.stereotype.Component("interfaces-operation")
36 public class InterfaceOperation extends BaseOperation {
37
38     public Either<List<InterfaceDefinition>, StorageOperationStatus> addInterfaces(String componentId,
39             List<InterfaceDefinition> interfaceDefinitions) {
40         return addOrUpdateInterfaces(false, componentId, interfaceDefinitions);
41     }
42
43     private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction,
44             String componentId, List<InterfaceDefinition> interfaceDefinitions) {
45
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);
53         }
54         return Either.left(interfaceDefinitions);
55     }
56
57     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, String componentId,
58             List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
59         if (isUpdate) {
60             return updateToscaDataOfToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList,
61                     JsonPresentationFields.UNIQUE_ID);
62         } else {
63             return addToscaDataToToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList,
64                     JsonPresentationFields.UNIQUE_ID);
65         }
66     }
67
68     public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(String componentId,
69             List<InterfaceDefinition> interfaceDefinitions) {
70         return addOrUpdateInterfaces(true, componentId, interfaceDefinitions);
71     }
72
73     public Either<String, StorageOperationStatus> deleteInterface(String componentId, String interfacesToDelete) {
74
75         StorageOperationStatus statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE,
76                 Collections.singletonList(interfacesToDelete));
77         if (!statusRes.equals(StorageOperationStatus.OK)) {
78             return Either.right(statusRes);
79         }
80
81         Either<Map<String, InterfaceDataDefinition>, TitanOperationStatus> componentEither =
82                 getDataFromGraph(componentId, EdgeLabelEnum.INTERFACE);
83         if (componentEither.isRight()) {
84             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentEither.right().value()));
85         }
86
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);
92             }
93         }
94
95         return Either.left(interfacesToDelete);
96     }
97
98 }