2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.tosca.utils;
23 import fj.data.Either;
24 import org.apache.commons.collections.MapUtils;
25 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
27 import org.openecomp.sdc.be.model.CapabilityDefinition;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.Service;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.tosca.CapabilityRequirementConverter;
33 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
34 import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
37 import java.util.Collection;
41 * @since November 19, 2017
44 public class ForwardingPathToscaUtil {
45 public static final String FORWARDS_TO_TOSCA_NAME =
46 "org.openecomp.relationships.ForwardsTo";
47 public static final String PROTOCOL = "protocol";
48 public static final String PORTS_RANGE = "target_range";
49 public static final String FORWARDER = "forwarder";
51 public static void addForwardingPaths(Service service, Map<String, ToscaNodeTemplate>
52 nodeTemplates, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
53 for (String forwardingPathName : service.getForwardingPaths().keySet()) {
54 ToscaNodeTemplate forwardingPathNodeTemplate =
55 new ToscaNodeTemplate();
56 final ForwardingPathDataDefinition path =
57 service.getForwardingPaths().get(forwardingPathName);
58 forwardingPathNodeTemplate.setType(path.getToscaResourceName());
60 if (Objects.nonNull(path.getDescription())) {
61 forwardingPathNodeTemplate.setDescription(path
64 Map<String, Object> props = new HashMap<>();
65 if (Objects.nonNull(path.getDestinationPortNumber())) {
66 props.put(PORTS_RANGE, Collections.singletonList(path.getDestinationPortNumber()));
68 if (Objects.nonNull(path.getProtocol())) {
69 props.put(PROTOCOL, path.getProtocol());
71 if (MapUtils.isNotEmpty(props)) {
72 forwardingPathNodeTemplate.setProperties(props);
75 final List<ForwardingPathElementDataDefinition> pathElements =
76 path.getPathElements()
77 .getListToscaDataDefinition();
78 forwardingPathNodeTemplate.setRequirements(convertPathElementsToRequirements(pathElements,
79 service, capabiltyRequirementConvertor, originComponents, toscaOperationFacade));
81 nodeTemplates.put(path.getName(), forwardingPathNodeTemplate);
86 private static List<Map<String, ToscaTemplateRequirement>> convertPathElementsToRequirements(
87 List<ForwardingPathElementDataDefinition> pathElements, Service service, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
88 List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
89 for (int i = 0; i <= pathElements.size() -1 ; i++) {
90 final ForwardingPathElementDataDefinition element = pathElements.get(i);
91 toscaRequirements.add(handleSingleReq(fetchCPName(service, element.getFromNode(), element.getFromCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element.getFromNode())));
92 if ( i == pathElements.size() -1) {
93 toscaRequirements.add(handleSingleReq(fetchCPName(service, element.getToNode(), element.getToCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element
97 return toscaRequirements;
101 private static String fetchNodeName(Service service, String nodeId) {
102 if (service.getComponentInstanceByName(nodeId).isPresent()) {
103 return service.getComponentInstanceByName(nodeId).get().getName();
110 private static Map<String, ToscaTemplateRequirement> handleSingleReq(
111 String fromCP, String fromNode) {
112 Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
113 ToscaTemplateRequirement firstReq = new ToscaTemplateRequirement();
114 firstReq.setRelationship(FORWARDS_TO_TOSCA_NAME); //todo
115 firstReq.setCapability(fromCP);
116 firstReq.setNode(fromNode);
117 toscaReqMap.put(FORWARDER, firstReq);
123 * @todo handle errors.
125 private static String fetchCPName(Service service, String nodeID, String cpName, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
126 Optional<ComponentInstance> componentInstance = service.getComponentInstanceByName(nodeID);
127 ComponentInstance componentInstanceVal = componentInstance.get();
128 String name = componentInstanceVal.getNormalizedName();
129 Component component = originComponents.get(componentInstanceVal.getComponentUid());
130 if(componentInstanceVal.getIsProxy()){
131 component = originComponents.get(componentInstanceVal.getSourceModelUid());
132 if (component == null) {
133 component = toscaOperationFacade.getToscaFullElement(componentInstanceVal.getSourceModelUid()).left().value();
137 CapabilityDefinition capability = componentInstanceVal.getCapabilities().values().stream().flatMap(Collection::stream)
138 .filter(capabilityDefinition -> capabilityDefinition.getName().equals(cpName)).findAny().get();
139 List<String> path = capability.getPath();
140 List<String> reducedPath = new ArrayList<>(path);
141 reducedPath.remove(reducedPath.size() - 1);
142 Either<String, Boolean> stringBooleanEither = capabiltyRequirementConvertor.buildSubstitutedName(originComponents, component, reducedPath, capability.getName(), null);
143 return name + "." + stringBooleanEither.left().value();