Add lombok support to simple classes
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / ForwardingPathOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsontitan.operations;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
25 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
26 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
28 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
29 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
31 import org.openecomp.sdc.be.model.Service;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
34 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36
37 import java.util.*;
38
39 @org.springframework.stereotype.Component("forwarding-paths-operations")
40 public class ForwardingPathOperation extends BaseOperation {
41     private static final Logger log = Logger.getLogger(ForwardingPathOperation.class.getName());
42
43
44     public Either<Set<String>, StorageOperationStatus> deleteForwardingPath(Service service, Set<String> forwardingPathsToDelete) {
45         Either<Set<String>, StorageOperationStatus> result = null;
46         Either<GraphVertex, TitanOperationStatus> getComponentVertex;
47         StorageOperationStatus status = null;
48
49         if (result == null) {
50             getComponentVertex = titanDao.getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
51             if (getComponentVertex.isRight()) {
52                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
53             }
54         }
55         if (result == null) {
56
57             status = deleteToscaDataElements(service.getUniqueId(), EdgeLabelEnum.FORWARDING_PATH,new ArrayList<>(forwardingPathsToDelete));
58
59             if (status != StorageOperationStatus.OK) {
60                 result = Either.right(status);
61             }
62         }
63
64         if (result == null) {
65             result = Either.left(forwardingPathsToDelete);
66         }
67         return result;
68     }
69
70     public Either<ForwardingPathDataDefinition, StorageOperationStatus> addForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
71         return addOrUpdateForwardingPath(false, serviceId, currentPath);
72     }
73
74     public Either<ForwardingPathDataDefinition, StorageOperationStatus> updateForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
75         return addOrUpdateForwardingPath(true, serviceId, currentPath);
76     }
77
78     private Either<ForwardingPathDataDefinition, StorageOperationStatus> addOrUpdateForwardingPath(boolean isUpdateAction, String serviceId, ForwardingPathDataDefinition currentPath) {
79
80         StorageOperationStatus statusRes;
81         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
82
83         getToscaElementRes = titanDao.getVertexById(serviceId, JsonParseFlagEnum.NoParse);
84         if (getToscaElementRes.isRight()) {
85             TitanOperationStatus status = getToscaElementRes.right().value();
86             CommonUtility.addRecordToLog(log, CommonUtility.LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", serviceId, status);
87             statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
88             return Either.right(statusRes);
89         }
90         GraphVertex serviceVertex = getToscaElementRes.left().value();
91         if (!isUpdateAction){
92             currentPath.setUniqueId(UUID.randomUUID().toString());
93         }
94         statusRes = performUpdateToscaAction(isUpdateAction, serviceVertex, Arrays.asList(currentPath), JsonPresentationFields.FORWARDING_PATH);
95         {
96             if (!statusRes.equals(StorageOperationStatus.OK)) {
97                 log.error("Failed to find the parent capability of capability type {}. status is {}", serviceId, statusRes);
98                 return Either.right(statusRes);
99             }
100             return Either.left(currentPath);
101         }
102
103     }
104
105
106     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex, List<ForwardingPathDataDefinition> toscaDataList, JsonPresentationFields mapKeyField) {
107         if (isUpdate) {
108             return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);
109         } else {
110             return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);
111         }
112     }
113
114 }
115
116
117