re base code
[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 org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
21 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
22 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
23 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
24 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
25 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
26 import org.openecomp.sdc.be.model.InterfaceDefinition;
27 import org.openecomp.sdc.be.model.Resource;
28 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
30 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32
33 import java.util.*;
34
35 @org.springframework.stereotype.Component("interfaces-operation")
36 public class InterfaceOperation extends BaseOperation {
37
38     private static final Logger logger = Logger.getLogger(InterfaceOperation.class.getName());
39
40
41     public Either<Set<String>, StorageOperationStatus> deleteInterface(Resource resource,
42         Set<String> interfacesToDelete) {
43         Either<Set<String>, StorageOperationStatus> result = null;
44         Either<GraphVertex, TitanOperationStatus> getComponentVertex;
45         StorageOperationStatus status = null;
46
47         if (result == null) {
48             getComponentVertex = titanDao.getVertexById(resource.getUniqueId(), JsonParseFlagEnum.NoParse);
49             if (getComponentVertex.isRight()) {
50                 result = Either
51                     .right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
52             }
53         }
54         if (result == null) {
55
56             status = deleteToscaDataElements(resource.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
57                 new ArrayList<>(interfacesToDelete));
58
59             if (status != StorageOperationStatus.OK) {
60                 result = Either.right(status);
61             }
62         }
63
64         if (result == null) {
65             result = Either.left(interfacesToDelete);
66         }
67         return result;
68     }
69
70     public Either<InterfaceDefinition, StorageOperationStatus> addInterface(String resourceId,
71         InterfaceDefinition interfaceDefinition) {
72         return addOrUpdateInterface(false, resourceId, interfaceDefinition);
73     }
74
75     public Either<InterfaceDefinition, StorageOperationStatus> updateInterface(String resourceId,
76         InterfaceDefinition interfaceDefinition) {
77         return addOrUpdateInterface(true, resourceId, interfaceDefinition);
78     }
79
80     private Either<InterfaceDefinition, StorageOperationStatus> addOrUpdateInterface(
81         boolean isUpdateAction, String resourceId, InterfaceDefinition interfaceDefinition) {
82
83         StorageOperationStatus statusRes;
84         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
85
86         getToscaElementRes = titanDao.getVertexById(resourceId, JsonParseFlagEnum.NoParse);
87         if (getToscaElementRes.isRight()) {
88             TitanOperationStatus status = getToscaElementRes.right().value();
89             CommonUtility.addRecordToLog(logger, CommonUtility.LogLevelEnum.DEBUG,
90                 "Failed to get tosca element {} upon adding the properties. Status is {}. ", resourceId, status);
91             statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
92             return Either.right(statusRes);
93         }
94         GraphVertex resourceVertex = getToscaElementRes.left().value();
95         if (!isUpdateAction) {
96             interfaceDefinition.setUniqueId(UUID.randomUUID().toString());
97         }
98         statusRes = performUpdateToscaAction(isUpdateAction, resourceVertex, Arrays.asList(interfaceDefinition),
99             JsonPresentationFields.INTERFACE);
100         if (!statusRes.equals(StorageOperationStatus.OK)) {
101             logger.error("Failed to find the parent capability of capability type {}. status is {}", resourceId,
102                 statusRes);
103             return Either.right(statusRes);
104         }
105         return Either.left(interfaceDefinition);
106     }
107
108     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
109         List<InterfaceDefinition> toscaDataList, JsonPresentationFields mapKeyField) {
110         if (isUpdate) {
111             return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.INTERFACE_ARTIFACTS,
112                 VertexTypeEnum.INTERFACE_ARTIFACTS, toscaDataList, JsonPresentationFields.UNIQUE_ID);
113         } else {
114             return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.INTERFACE_ARTIFACTS,
115                 VertexTypeEnum.INTERFACE_ARTIFACTS, toscaDataList, JsonPresentationFields.UNIQUE_ID);
116         }
117     }
118
119
120 }
121
122