Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / utils / ForwardingPathToscaUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.tosca.utils;
22
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;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Objects;
43 import java.util.Optional;
44
45 /**
46  * @author KATYR
47  * @since November 19, 2017
48  */
49
50 public class ForwardingPathToscaUtil {
51     public static final String FORWARDS_TO_TOSCA_NAME =
52             "org.openecomp.relationships.ForwardsTo";
53     public static final String PROTOCOL = "protocol";
54     public static final String PORTS_RANGE = "target_range";
55     public static final String FORWARDER = "forwarder";
56
57     public static void addForwardingPaths(Service service, Map<String, ToscaNodeTemplate>
58             nodeTemplates, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
59         for (String forwardingPathName : service.getForwardingPaths().keySet()) {
60             ToscaNodeTemplate forwardingPathNodeTemplate =
61                     new ToscaNodeTemplate();
62             final ForwardingPathDataDefinition path =
63                     service.getForwardingPaths().get(forwardingPathName);
64             forwardingPathNodeTemplate.setType(path.getToscaResourceName());
65
66             if (Objects.nonNull(path.getDescription())) {
67                 forwardingPathNodeTemplate.setDescription(path
68                         .getDescription());
69             }
70             Map<String, Object> props = new HashMap<>();
71             if (Objects.nonNull(path.getDestinationPortNumber())) {
72                 props.put(PORTS_RANGE, Collections.singletonList(path.getDestinationPortNumber()));
73             }
74             if (Objects.nonNull(path.getProtocol())) {
75                 props.put(PROTOCOL, path.getProtocol());
76             }
77             if (MapUtils.isNotEmpty(props)) {
78                 forwardingPathNodeTemplate.setProperties(props);
79             }
80
81             final List<ForwardingPathElementDataDefinition> pathElements =
82                     path.getPathElements()
83                             .getListToscaDataDefinition();
84             forwardingPathNodeTemplate.setRequirements(convertPathElementsToRequirements(pathElements,
85                     service, capabiltyRequirementConvertor, originComponents, toscaOperationFacade));
86
87             nodeTemplates.put(path.getName(), forwardingPathNodeTemplate);
88         }
89
90     }
91
92     private static List<Map<String, ToscaTemplateRequirement>> convertPathElementsToRequirements(
93             List<ForwardingPathElementDataDefinition> pathElements, Service service, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
94         List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
95         for (int i = 0; i <= pathElements.size() -1 ; i++) {
96                 final ForwardingPathElementDataDefinition element = pathElements.get(i);
97                 toscaRequirements.add(handleSingleReq(fetchCPName(service, element.getFromNode(), element.getFromCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element.getFromNode())));
98                 if ( i == pathElements.size() -1) {
99                     toscaRequirements.add(handleSingleReq(fetchCPName(service, element.getToNode(), element.getToCP(), capabiltyRequirementConvertor, originComponents, toscaOperationFacade), fetchNodeName(service, element
100                             .getToNode())));
101                 }
102        }
103         return toscaRequirements;
104
105     }
106
107     private static String fetchNodeName(Service service, String nodeId) {
108         if (service.getComponentInstanceByName(nodeId).isPresent()) {
109             return service.getComponentInstanceByName(nodeId).get().getName();
110         } else {
111             return "";
112         }
113     }
114
115
116     private static Map<String, ToscaTemplateRequirement> handleSingleReq(
117             String fromCP, String fromNode) {
118         Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
119         ToscaTemplateRequirement firstReq = new ToscaTemplateRequirement();
120         firstReq.setRelationship(FORWARDS_TO_TOSCA_NAME); //todo
121         firstReq.setCapability(fromCP);
122         firstReq.setNode(fromNode);
123         toscaReqMap.put(FORWARDER, firstReq);
124
125         return toscaReqMap;
126     }
127
128     /**
129      * @todo handle errors.
130      */
131     private static String fetchCPName(Service service, String nodeID, String cpName, CapabilityRequirementConverter capabiltyRequirementConvertor, Map<String, Component> originComponents, ToscaOperationFacade toscaOperationFacade) {
132         Optional<ComponentInstance> componentInstance = service.getComponentInstanceByName(nodeID);
133         ComponentInstance componentInstanceVal = componentInstance.get();
134         String name = componentInstanceVal.getNormalizedName();
135         Component component = originComponents.get(componentInstanceVal.getComponentUid());
136         if(componentInstanceVal.getIsProxy()){
137             component = originComponents.get(componentInstanceVal.getSourceModelUid());
138             if (component == null) {
139                 component = toscaOperationFacade.getToscaFullElement(componentInstanceVal.getSourceModelUid()).left().value();
140             }
141
142         }
143         CapabilityDefinition capability = componentInstanceVal.getCapabilities().values().stream().flatMap(Collection::stream)
144                 .filter(capabilityDefinition -> capabilityDefinition.getName().equals(cpName)).findAny().get();
145         List<String> path = capability.getPath();
146         List<String> reducedPath = new ArrayList<>(path);
147         reducedPath.remove(reducedPath.size() - 1);
148         Either<String, Boolean> stringBooleanEither = capabiltyRequirementConvertor.buildSubstitutedName(originComponents, component, reducedPath, capability.getName(), null);
149         return name + "." + stringBooleanEither.left().value();
150     }
151 }