Implement 'Update Service by importing Tosca Model'-story
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / 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 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
21
22 import fj.data.Either;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.UUID;
28 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
30 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
33 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
35 import org.openecomp.sdc.be.model.Service;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
38 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40
41 @org.springframework.stereotype.Component("forwarding-paths-operations")
42 public class ForwardingPathOperation extends BaseOperation {
43
44     private static final Logger log = Logger.getLogger(ForwardingPathOperation.class.getName());
45
46     public Either<Set<String>, StorageOperationStatus> deleteForwardingPath(Service service, Set<String> forwardingPathsToDelete) {
47         Either<Set<String>, StorageOperationStatus> result = null;
48         Either<GraphVertex, JanusGraphOperationStatus> getComponentVertex;
49         StorageOperationStatus status = null;
50         if (result == null) {
51             getComponentVertex = janusGraphDao.getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
52             if (getComponentVertex.isRight()) {
53                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getComponentVertex.right().value()));
54             }
55         }
56         if (result == null) {
57             status = deleteToscaDataElements(service.getUniqueId(), EdgeLabelEnum.FORWARDING_PATH, new ArrayList<>(forwardingPathsToDelete));
58             if (status != StorageOperationStatus.OK) {
59                 result = Either.right(status);
60             }
61         }
62         if (result == null) {
63             result = Either.left(forwardingPathsToDelete);
64         }
65         return result;
66     }
67
68     public Either<ForwardingPathDataDefinition, StorageOperationStatus> addForwardingPath(String serviceId,
69                                                                                           ForwardingPathDataDefinition currentPath) {
70         return addOrUpdateForwardingPath(false, serviceId, currentPath);
71     }
72
73     public Either<ForwardingPathDataDefinition, StorageOperationStatus> updateForwardingPath(String serviceId,
74                                                                                              ForwardingPathDataDefinition currentPath) {
75         return addOrUpdateForwardingPath(true, serviceId, currentPath);
76     }
77
78     private Either<ForwardingPathDataDefinition, StorageOperationStatus> addOrUpdateForwardingPath(boolean isUpdateAction, String serviceId,
79                                                                                                    ForwardingPathDataDefinition currentPath) {
80         StorageOperationStatus statusRes;
81         Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes;
82         getToscaElementRes = janusGraphDao.getVertexById(serviceId, JsonParseFlagEnum.NoParse);
83         if (getToscaElementRes.isRight()) {
84             JanusGraphOperationStatus status = getToscaElementRes.right().value();
85             CommonUtility
86                 .addRecordToLog(log, CommonUtility.LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ",
87                     serviceId, status);
88             statusRes = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
89             return Either.right(statusRes);
90         }
91         GraphVertex serviceVertex = getToscaElementRes.left().value();
92         if (!isUpdateAction) {
93             currentPath.setUniqueId(UUID.randomUUID().toString());
94         }
95         statusRes = performUpdateToscaAction(isUpdateAction, serviceVertex, Arrays.asList(currentPath), JsonPresentationFields.FORWARDING_PATH);
96         {
97             if (!statusRes.equals(StorageOperationStatus.OK)) {
98                 log.error("Failed to find the parent capability of capability type {}. status is {}", serviceId, statusRes);
99                 return Either.right(statusRes);
100             }
101             return Either.left(currentPath);
102         }
103     }
104
105     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex,
106                                                             List<ForwardingPathDataDefinition> toscaDataList, JsonPresentationFields mapKeyField) {
107         if (isUpdate) {
108             return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList,
109                 JsonPresentationFields.UNIQUE_ID);
110         } else {
111             return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList,
112                 JsonPresentationFields.UNIQUE_ID);
113         }
114     }
115 }