/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.tosca.utils; import fj.data.Either; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.collections.MapUtils; import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.tosca.CapabilityRequirementConverter; import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class ForwardingPathToscaUtil { public static final String FORWARDS_TO_TOSCA_NAME = "org.openecomp.relationships.ForwardsTo"; public static final String PROTOCOL = "protocol"; public static final String PORTS_RANGE = "target_range"; public static final String FORWARDER = "forwarder"; public static void addForwardingPaths(Service service, Map nodeTemplates, CapabilityRequirementConverter capabiltyRequirementConvertor, Map originComponents, ToscaOperationFacade toscaOperationFacade) { for (String forwardingPathName : service.getForwardingPaths().keySet()) { ToscaNodeTemplate forwardingPathNodeTemplate = new ToscaNodeTemplate(); final ForwardingPathDataDefinition path = service.getForwardingPaths().get(forwardingPathName); forwardingPathNodeTemplate.setType(path.getToscaResourceName()); if (Objects.nonNull(path.getDescription())) { forwardingPathNodeTemplate.setDescription(path.getDescription()); } Map props = new HashMap<>(); if (Objects.nonNull(path.getDestinationPortNumber())) { props.put(PORTS_RANGE, Collections.singletonList(path.getDestinationPortNumber())); } if (Objects.nonNull(path.getProtocol())) { props.put(PROTOCOL, path.getProtocol()); } if (MapUtils.isNotEmpty(props)) { forwardingPathNodeTemplate.setProperties(props); } final List pathElements = path.getPathElements().getListToscaDataDefinition(); forwardingPathNodeTemplate.setRequirements( convertPathElementsToRequirements(pathElements, service, capabiltyRequirementConvertor, originComponents, toscaOperationFacade)); nodeTemplates.put(path.getName(), forwardingPathNodeTemplate); } } private static List> convertPathElementsToRequirements( List pathElements, Service service, CapabilityRequirementConverter capabiltyRequirementConvertor, Map originComponents, ToscaOperationFacade toscaOperationFacade) { List> toscaRequirements = new ArrayList<>(); for (int i = 0; i <= pathElements.size() - 1; i++) { final ForwardingPathElementDataDefinition element = pathElements.get(i); toscaRequirements.add(handleSingleReq( fetchCPName(service, element.getFromNode(), element.getFromCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element.getFromNode()))); if (i == pathElements.size() - 1) { toscaRequirements.add(handleSingleReq( fetchCPName(service, element.getToNode(), element.getToCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element.getToNode()))); } } return toscaRequirements; } private static String fetchNodeName(Service service, String nodeId) { final Optional componentInstanceByName = service.getComponentInstanceByName(nodeId); if (componentInstanceByName.isPresent()) { return componentInstanceByName.get().getName(); } else { return ""; } } private static Map handleSingleReq(String fromCP, String fromNode) { Map toscaReqMap = new HashMap<>(); ToscaTemplateRequirement firstReq = new ToscaTemplateRequirement(); firstReq.setRelationship(FORWARDS_TO_TOSCA_NAME); //todo firstReq.setCapability(fromCP); firstReq.setNode(fromNode); toscaReqMap.put(FORWARDER, firstReq); return toscaReqMap; } /** * @todo handle errors. */ private static String fetchCPName(Service service, String nodeID, String cpName, CapabilityRequirementConverter capabiltyRequirementConvertor, Map originComponents, ToscaOperationFacade toscaOperationFacade) { Optional componentInstance = service.getComponentInstanceByName(nodeID); ComponentInstance componentInstanceVal = componentInstance.get(); String name = componentInstanceVal.getNormalizedName(); Component component = originComponents.get(componentInstanceVal.getComponentUid()); if (componentInstanceVal.getIsProxy()) { component = originComponents.get(componentInstanceVal.getSourceModelUid()); if (component == null) { component = toscaOperationFacade.getToscaFullElement(componentInstanceVal.getSourceModelUid()).left().value(); } } CapabilityDefinition capability = componentInstanceVal.getCapabilities().values().stream().flatMap(Collection::stream) .filter(capabilityDefinition -> capabilityDefinition.getName().equals(cpName)).findAny().get(); List path = capability.getPath(); List reducedPath = new ArrayList<>(path); reducedPath.remove(reducedPath.size() - 1); Either stringBooleanEither = capabiltyRequirementConvertor .buildSubstitutedName(originComponents, component, reducedPath, capability.getName(), null); return name + "." + stringBooleanEither.left().value(); } }