bc6728103598712c7347f376c0d4a0ffbde2b697
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / 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 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
17
18 import fj.data.Either;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.stream.Collectors;
23 import org.apache.commons.collections.MapUtils;
24 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
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.datatypes.elements.InterfaceDataDefinition;
28 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
29 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
30 import org.openecomp.sdc.be.model.InterfaceDefinition;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
33
34 @org.springframework.stereotype.Component("interfaces-operation")
35 public class InterfaceOperation extends BaseOperation {
36
37     public Either<List<InterfaceDefinition>, StorageOperationStatus> addInterfaces(String componentId,
38                                                                                    List<InterfaceDefinition> interfaceDefinitions) {
39         return addOrUpdateInterfaces(false, componentId, interfaceDefinitions);
40     }
41
42     private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction, String componentId,
43                                                                                             List<InterfaceDefinition> interfaceDefinitions) {
44         List<ToscaDataDefinition> interfaceDataDefinitions = interfaceDefinitions.stream().map(InterfaceDataDefinition::new)
45             .collect(Collectors.toList());
46         StorageOperationStatus statusRes = performUpdateToscaAction(isUpdateAction, componentId, interfaceDataDefinitions, EdgeLabelEnum.INTERFACE,
47             VertexTypeEnum.INTERFACE);
48         if (!statusRes.equals(StorageOperationStatus.OK)) {
49             return Either.right(statusRes);
50         }
51         return Either.left(interfaceDefinitions);
52     }
53
54     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, String componentId, List<ToscaDataDefinition> toscaDataList,
55                                                             EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
56         if (isUpdate) {
57             return updateToscaDataOfToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
58         } else {
59             return addToscaDataToToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
60         }
61     }
62
63     public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(String componentId,
64                                                                                       List<InterfaceDefinition> interfaceDefinitions) {
65         return addOrUpdateInterfaces(true, componentId, interfaceDefinitions);
66     }
67
68     public Either<String, StorageOperationStatus> deleteInterface(String componentId, String interfacesToDelete) {
69         StorageOperationStatus statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE,
70             Collections.singletonList(interfacesToDelete));
71         if (!statusRes.equals(StorageOperationStatus.OK)) {
72             return Either.right(statusRes);
73         }
74         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> componentEither = getDataFromGraph(componentId,
75             EdgeLabelEnum.INTERFACE);
76         if (componentEither.isRight()) {
77             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentEither.right().value()));
78         }
79         Map<String, InterfaceDataDefinition> interfaceDataDefinitionMap = componentEither.left().value();
80         if (MapUtils.isEmpty(interfaceDataDefinitionMap)) {
81             statusRes = removeToscaData(componentId, EdgeLabelEnum.INTERFACE, VertexTypeEnum.INTERFACE);
82             if (!statusRes.equals(StorageOperationStatus.OK)) {
83                 return Either.right(statusRes);
84             }
85         }
86         return Either.left(interfacesToDelete);
87     }
88 }