Sync Integ to Master
[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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Set;
42 import java.util.UUID;
43
44 @org.springframework.stereotype.Component("forwarding-paths-operations")
45 public class ForwardingPathOperation extends BaseOperation {
46     private static Logger logger = LoggerFactory.getLogger(ForwardingPathOperation.class.getName());
47
48
49     public Either<Set<String>, StorageOperationStatus> deleteForwardingPath(Service service, Set<String> forwardingPathsToDelete) {
50         Either<Set<String>, StorageOperationStatus> result = null;
51         Either<GraphVertex, TitanOperationStatus> getComponentVertex;
52         StorageOperationStatus status = null;
53
54         if (result == null) {
55             getComponentVertex = titanDao.getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
56             if (getComponentVertex.isRight()) {
57                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
58             }
59         }
60         if (result == null) {
61
62             status = deleteToscaDataElements(service.getUniqueId(), EdgeLabelEnum.FORWARDING_PATH,new ArrayList<>(forwardingPathsToDelete));
63
64             if (status != StorageOperationStatus.OK) {
65                 result = Either.right(status);
66             }
67         }
68
69         if (result == null) {
70             result = Either.left(forwardingPathsToDelete);
71         }
72         return result;
73     }
74
75     public Either<ForwardingPathDataDefinition, StorageOperationStatus> addForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
76         return addOrUpdateForwardingPath(false, serviceId, currentPath);
77     }
78
79     public Either<ForwardingPathDataDefinition, StorageOperationStatus> updateForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
80         return addOrUpdateForwardingPath(true, serviceId, currentPath);
81     }
82
83     private Either<ForwardingPathDataDefinition, StorageOperationStatus> addOrUpdateForwardingPath(boolean isUpdateAction, String serviceId, ForwardingPathDataDefinition currentPath) {
84
85         StorageOperationStatus statusRes;
86         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
87
88         getToscaElementRes = titanDao.getVertexById(serviceId, JsonParseFlagEnum.NoParse);
89         if (getToscaElementRes.isRight()) {
90             TitanOperationStatus status = getToscaElementRes.right().value();
91             CommonUtility.addRecordToLog(logger, CommonUtility.LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", serviceId, status);
92             statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
93             return Either.right(statusRes);
94         }
95         GraphVertex serviceVertex = getToscaElementRes.left().value();
96         if (!isUpdateAction){
97             currentPath.setUniqueId(UUID.randomUUID().toString());
98         }
99         statusRes = performUpdateToscaAction(isUpdateAction, serviceVertex, Arrays.asList(currentPath), JsonPresentationFields.FORWARDING_PATH);
100         {
101             if (!statusRes.equals(StorageOperationStatus.OK)) {
102                 logger.error("Failed to find the parent capability of capability type {}. status is {}", serviceId, statusRes);
103                 return Either.right(statusRes);
104             }
105             return Either.left(currentPath);
106         }
107
108     }
109
110
111     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex, List<ForwardingPathDataDefinition> toscaDataList, JsonPresentationFields mapKeyField) {
112         if (isUpdate) {
113             return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);
114         } else {
115             return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);
116         }
117     }
118
119 }
120
121
122