5d44ba1555d93286ce01c3eee2a9f758e7934447
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceTranslationContrailV2VirtualNetworkImpl.java
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.translator.services.heattotosca.impl.resourcetranslation;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
30 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.ReferenceType;
37 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
40 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
41
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Optional;
47
48 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
49
50   protected static Logger logger =
51       (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
52
53   @Override
54   public void translate(TranslateTo translateTo) {
55     NodeTemplate nodeTemplate = new NodeTemplate();
56     nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK);
57     nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
58         .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo.
59             getResourceId(),translateTo.getResource().getProperties(),
60             nodeTemplate.getProperties(), translateTo.getHeatFileName(),
61             translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
62             nodeTemplate, translateTo.getContext()));
63     DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
64             nodeTemplate);
65     linkToPolicyNodeTemplate(translateTo);
66   }
67
68   private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
69     List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
70     if (CollectionUtils.isEmpty(networkPolicyIdList)) {
71       return;
72     }
73     for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
74       NodeTemplate policyNodeTemplate = DataModelUtil
75           .getNodeTemplate(translateTo.getServiceTemplate(),
76               (String) attachedResourceId.getTranslatedId());
77       DataModelUtil
78           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
79               createRequirementAssignment(translateTo.getTranslatedId()));
80     }
81   }
82
83   private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
84     Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
85     if (propertyValue != null) {
86       return extractNetworkPolicyId(propertyValue, translateTo);
87     } else {
88       return null;
89     }
90   }
91
92   private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
93                                                           TranslateTo translateTo) {
94     List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
95
96     if (propertyValue instanceof List) {
97       for (Object value : (List) propertyValue) {
98         attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
99       }
100     } else {
101       AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
102       if (resourceId != null) {
103         attachedResourceIdList.add(resourceId);
104       }
105     }
106     return attachedResourceIdList;
107   }
108
109   private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
110     Optional<String> translatedPolicyResourceId;
111     String policyResourceId = extractResourceId(propertyValue, translateTo);
112     if (policyResourceId == null) {
113       return null;
114     }
115
116     Resource policyResource = HeatToToscaUtil
117         .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId,
118             translateTo.getHeatFileName());
119     if (!policyResource.getType()
120         .equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE.getHeatResource())) {
121       return null;
122     }
123     translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
124         .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
125             translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
126             translateTo.getContext());
127     if (!translatedPolicyResourceId.isPresent()) {
128       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
129           + translateTo.getResource().getType()
130           + "' property network_policy_refs is referenced to an unsupported resource the "
131           + "connection will be ignored in TOSCA translation.");
132       return null;
133     }
134     AttachedResourceId attachedResourceId =
135         new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
136             ReferenceType.GET_ATTR);
137     return attachedResourceId;
138   }
139
140   private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
141
142     Object value;
143     if (propertyValue instanceof Map) {
144       if (((Map) propertyValue).containsKey("get_attr")) {
145         value = ((Map) propertyValue).get("get_attr");
146         if (value instanceof List) {
147           if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) {
148             if (((List) value).get(0) instanceof String) {
149               return (String) ((List) value).get(0);
150             } else {
151               logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
152                   + translateTo.getResource().getType()
153                   + "' has property with invalid format of 'get_attr' function with 'fq_name' "
154                   + "value, therefore this property will be ignored in TOSCA translation.");
155             }
156           }
157         }
158       } else if (((Map) propertyValue).containsKey("get_resource")) {
159         value = ((Map) propertyValue).get("get_resource");
160         if (value instanceof String) {
161           return (String) value;
162         } else {
163           logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
164               + translateTo.getResource().getType()
165               + "' has property invalid format of 'get_resource' function, therefore this property"
166               + " will be ignored in TOSCA translation.");
167         }
168       } else {
169         Collection<Object> valCollection = ((Map) propertyValue).values();
170         for (Object entryValue : valCollection) {
171           String ret = extractResourceId(entryValue, translateTo);
172           if (ret != null) {
173             return ret;
174           }
175
176         }
177       }
178     } else if (propertyValue instanceof List) {
179       for (Object prop : (List) propertyValue) {
180         String ret = extractResourceId(prop, translateTo);
181         if (ret != null) {
182           return ret;
183         }
184       }
185     }
186     logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
187         + translateTo.getResource().getType()
188         + "' invalid format of property value, therefore this resource will be ignored in TOSCA "
189         + "translation.");
190     return null;
191   }
192
193   private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
194     RequirementAssignment requirement = new RequirementAssignment();
195     requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
196     requirement.setNode(translatedNetworkResourceId);
197     requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
198     return requirement;
199   }
200
201
202 }