Implement 'Update Service by importing Tosca Model'-story
[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.enums.ResourceTypeEnum;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.InterfaceDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
36
37 @org.springframework.stereotype.Component("interfaces-operation")
38 public class InterfaceOperation extends BaseOperation {
39
40     public Either<List<InterfaceDefinition>, StorageOperationStatus> addInterfaces(final Component component,
41                                                                                    List<InterfaceDefinition> interfaceDefinitions) {
42         final String componentId = component.getUniqueId();
43         if (isVfc(component)) {
44             return addOrUpdateInterfaces(componentId, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfaceDefinitions,
45                 false);
46         } else {
47             return addOrUpdateInterfaces(componentId, VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceDefinitions, false);
48         }
49     }
50
51     private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(final String componentId,
52                                                                                             final VertexTypeEnum vertexTypeEnum,
53                                                                                             final EdgeLabelEnum edgeLabelEnum,
54                                                                                             final List<InterfaceDefinition> interfaceDefinitions,
55                                                                                             final boolean isUpdateAction) {
56         final List<ToscaDataDefinition> interfaceDataDefinitions = interfaceDefinitions.stream()
57             .map(InterfaceDataDefinition::new)
58             .collect(Collectors.toList());
59         final StorageOperationStatus status = performUpdateToscaAction(isUpdateAction, componentId, interfaceDataDefinitions, edgeLabelEnum,
60             vertexTypeEnum);
61         if (!status.equals(StorageOperationStatus.OK)) {
62             return Either.right(status);
63         }
64         return Either.left(interfaceDefinitions);
65     }
66
67     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, String componentId, List<ToscaDataDefinition> toscaDataList,
68                                                             EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
69         if (isUpdate) {
70             return updateToscaDataOfToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
71         } else {
72             return addToscaDataToToscaElement(componentId, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
73         }
74     }
75
76     public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(final Component component,
77                                                                                       final List<InterfaceDefinition> interfaceDefinitions) {
78         final String componentId = component.getUniqueId();
79         if (isVfc(component)) {
80             return addOrUpdateInterfaces(componentId, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfaceDefinitions,
81                 true);
82         } else {
83             return addOrUpdateInterfaces(componentId, VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceDefinitions, true);
84         }
85     }
86
87     public Either<String, StorageOperationStatus> deleteInterface(final Component component, final String interfacesToDelete) {
88         final String componentId = component.getUniqueId();
89         if (isVfc(component)) {
90             return deleteInterface(componentId, interfacesToDelete, EdgeLabelEnum.INTERFACE_ARTIFACTS);
91         } else {
92             return deleteInterface(componentId, interfacesToDelete, EdgeLabelEnum.INTERFACE);
93         }
94     }
95
96     private boolean isVfc(Component component) {
97         return component instanceof Resource && ((Resource) component).getResourceType() == ResourceTypeEnum.VFC;
98     }
99
100     private Either<String, StorageOperationStatus> deleteInterface(final String componentId, final String interfacesToDelete,
101                                                                    final EdgeLabelEnum edgeLabel) {
102         StorageOperationStatus statusRes = deleteToscaDataElements(componentId, edgeLabel, Collections.singletonList(interfacesToDelete));
103         if (!statusRes.equals(StorageOperationStatus.OK)) {
104             return Either.right(statusRes);
105         }
106         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> interfaceEither = getDataFromGraph(componentId, edgeLabel);
107         if (interfaceEither.isRight()) {
108             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(interfaceEither.right().value()));
109         }
110         final Map<String, InterfaceDataDefinition> interfaceDataDefinitionMap = interfaceEither.left().value();
111         if (MapUtils.isEmpty(interfaceDataDefinitionMap)) {
112             statusRes = removeToscaData(componentId, edgeLabel);
113             if (!statusRes.equals(StorageOperationStatus.OK)) {
114                 return Either.right(statusRes);
115             }
116         }
117         return Either.left(interfacesToDelete);
118     }
119 }