2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.openecomp.sdc.be.model.jsontitan.operations;
 
  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;
 
  38 import java.util.ArrayList;
 
  39 import java.util.Arrays;
 
  40 import java.util.List;
 
  42 import java.util.UUID;
 
  44 @org.springframework.stereotype.Component("forwarding-paths-operations")
 
  45 public class ForwardingPathOperation extends BaseOperation {
 
  46     private static Logger logger = LoggerFactory.getLogger(ForwardingPathOperation.class.getName());
 
  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;
 
  55             getComponentVertex = titanDao.getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
 
  56             if (getComponentVertex.isRight()) {
 
  57                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentVertex.right().value()));
 
  62             status = deleteToscaDataElements(service.getUniqueId(), EdgeLabelEnum.FORWARDING_PATH,new ArrayList<>(forwardingPathsToDelete));
 
  64             if (status != StorageOperationStatus.OK) {
 
  65                 result = Either.right(status);
 
  70             result = Either.left(forwardingPathsToDelete);
 
  75     public Either<ForwardingPathDataDefinition, StorageOperationStatus> addForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
 
  76         return addOrUpdateForwardingPath(false, serviceId, currentPath);
 
  79     public Either<ForwardingPathDataDefinition, StorageOperationStatus> updateForwardingPath(String serviceId, ForwardingPathDataDefinition currentPath) {
 
  80         return addOrUpdateForwardingPath(true, serviceId, currentPath);
 
  83     private Either<ForwardingPathDataDefinition, StorageOperationStatus> addOrUpdateForwardingPath(boolean isUpdateAction, String serviceId, ForwardingPathDataDefinition currentPath) {
 
  85         StorageOperationStatus statusRes;
 
  86         Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
 
  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);
 
  95         GraphVertex serviceVertex = getToscaElementRes.left().value();
 
  97             currentPath.setUniqueId(UUID.randomUUID().toString());
 
  99         statusRes = performUpdateToscaAction(isUpdateAction, serviceVertex, Arrays.asList(currentPath), JsonPresentationFields.FORWARDING_PATH);
 
 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);
 
 105             return Either.left(currentPath);
 
 111     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, GraphVertex graphVertex, List<ForwardingPathDataDefinition> toscaDataList, JsonPresentationFields mapKeyField) {
 
 113             return updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);
 
 115             return addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, VertexTypeEnum.FORWARDING_PATH, toscaDataList, JsonPresentationFields.UNIQUE_ID);