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