4aec8860923f285e65ef31e67a160e1b09ca2d1e
[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.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;
34
35 @org.springframework.stereotype.Component("interfaces-operation")
36 public class InterfaceOperation extends BaseOperation {
37
38     public Either<List<InterfaceDefinition>, StorageOperationStatus> addInterfaces(String componentId, List<InterfaceDefinition> interfaceDefinitions) {
39         return addOrUpdateInterfaces(false, componentId, interfaceDefinitions);
40     }
41
42     public Either<List<InterfaceDefinition>, StorageOperationStatus> updateInterfaces(String componentId, List<InterfaceDefinition> interfaceDefinitions) {
43         return addOrUpdateInterfaces(true, componentId, interfaceDefinitions);
44     }
45
46     private Either<List<InterfaceDefinition>, StorageOperationStatus> addOrUpdateInterfaces(boolean isUpdateAction, String componentId, List<InterfaceDefinition> interfaceDefinitions) {
47
48         StorageOperationStatus statusRes;
49         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
50
51         getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
52         if (getToscaElementRes.isRight()) {
53             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value()));
54         }
55         GraphVertex componentVertex = getToscaElementRes.left().value();
56
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);
61         }
62         return Either.left(interfaceDefinitions);
63     }
64
65     public Either<String, StorageOperationStatus> deleteInterface(String componentId, String interfacesToDelete) {
66         StorageOperationStatus statusRes;
67         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
68
69         getToscaElementRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
70         if (getToscaElementRes.isRight()) {
71             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getToscaElementRes.right().value()));
72         }
73
74         statusRes = deleteToscaDataElements(componentId, EdgeLabelEnum.INTERFACE, Collections.singletonList(interfacesToDelete));
75         if (statusRes != StorageOperationStatus.OK) {
76             return Either.right(statusRes);
77         }
78
79         return Either.left(interfacesToDelete);
80     }
81
82     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
83         List<ToscaDataDefinition> toscaDataList, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
84         if (isUpdate) {
85             return updateToscaDataOfToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
86         } else {
87             return addToscaDataToToscaElement(graphVertex, edgeLabel, vertexLabel, toscaDataList, JsonPresentationFields.UNIQUE_ID);
88         }
89     }
90
91 }