10ccf0f5607130e687c668ab4d45e87e1a3ffec6
[sdc.git] /
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
56
57     mdcDataDebugMessage.debugEntryMessage(null, null);
58
59     NodeTemplate nodeTemplate = new NodeTemplate();
60     nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK);
61     nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
62         .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo.
63             getResourceId(),translateTo.getResource().getProperties(),
64             nodeTemplate.getProperties(), translateTo.getHeatFileName(),
65             translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
66             nodeTemplate, translateTo.getContext()));
67     DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
68             nodeTemplate);
69     linkToPolicyNodeTemplate(translateTo);
70
71     mdcDataDebugMessage.debugExitMessage(null, null);
72   }
73
74   private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
75
76
77     mdcDataDebugMessage.debugEntryMessage(null, null);
78
79     List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
80     if (CollectionUtils.isEmpty(networkPolicyIdList)) {
81       return;
82     }
83     for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
84       NodeTemplate policyNodeTemplate = DataModelUtil
85           .getNodeTemplate(translateTo.getServiceTemplate(),
86               (String) attachedResourceId.getTranslatedId());
87       DataModelUtil
88           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
89               createRequirementAssignment(translateTo.getTranslatedId()));
90     }
91
92     mdcDataDebugMessage.debugExitMessage(null, null);
93   }
94
95   private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
96
97
98     mdcDataDebugMessage.debugEntryMessage(null, null);
99
100     Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
101     if (propertyValue != null) {
102       mdcDataDebugMessage.debugExitMessage(null, null);
103       return extractNetworkPolicyId(propertyValue, translateTo);
104     } else {
105       mdcDataDebugMessage.debugExitMessage(null, null);
106       return null;
107     }
108   }
109
110   private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
111                                                           TranslateTo translateTo) {
112
113
114     mdcDataDebugMessage.debugEntryMessage(null, null);
115
116     List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
117
118     if (propertyValue instanceof List) {
119       for (Object value : (List) propertyValue) {
120         attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
121       }
122     } else {
123       AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
124       if (resourceId != null) {
125         attachedResourceIdList.add(resourceId);
126       }
127     }
128
129     mdcDataDebugMessage.debugExitMessage(null, null);
130     return attachedResourceIdList;
131   }
132
133   private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
134
135
136     mdcDataDebugMessage.debugEntryMessage(null, null);
137
138     Optional<String> translatedPolicyResourceId;
139     String policyResourceId = extractResourceId(propertyValue, translateTo);
140     if (policyResourceId == null) {
141       mdcDataDebugMessage.debugExitMessage(null, null);
142       return null;
143     }
144
145     Resource policyResource = HeatToToscaUtil
146         .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId,
147             translateTo.getHeatFileName());
148     if (!policyResource.getType()
149         .equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE.getHeatResource())) {
150       mdcDataDebugMessage.debugExitMessage(null, null);
151       return null;
152     }
153     translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
154         .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
155             translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
156             translateTo.getContext());
157     if (!translatedPolicyResourceId.isPresent()) {
158       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
159           + translateTo.getResource().getType()
160           + "' property network_policy_refs is referenced to an unsupported resource the "
161           + "connection will be ignored in TOSCA translation.");
162       mdcDataDebugMessage.debugExitMessage(null, null);
163       return null;
164     }
165     AttachedResourceId attachedResourceId =
166         new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
167             ReferenceType.GET_ATTR);
168     mdcDataDebugMessage.debugExitMessage(null, null);
169     return attachedResourceId;
170   }
171
172   private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
173
174     Object value;
175     if (propertyValue instanceof Map) {
176       if (((Map) propertyValue).containsKey("get_attr")) {
177         value = ((Map) propertyValue).get("get_attr");
178         if (value instanceof List) {
179           if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) {
180             if (((List) value).get(0) instanceof String) {
181               return (String) ((List) value).get(0);
182             } else {
183               logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
184                   + translateTo.getResource().getType()
185                   + "' has property with invalid format of 'get_attr' function with 'fq_name' "
186                   + "value, therefore this property will be ignored in TOSCA translation.");
187             }
188           }
189         }
190       } else if (((Map) propertyValue).containsKey("get_resource")) {
191         value = ((Map) propertyValue).get("get_resource");
192         if (value instanceof String) {
193           return (String) value;
194         } else {
195           logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
196               + translateTo.getResource().getType()
197               + "' has property invalid format of 'get_resource' function, therefore this property"
198               + " will be ignored in TOSCA translation.");
199         }
200       } else {
201         Collection<Object> valCollection = ((Map) propertyValue).values();
202         for (Object entryValue : valCollection) {
203           String ret = extractResourceId(entryValue, translateTo);
204           if (ret != null) {
205             return ret;
206           }
207
208         }
209       }
210     } else if (propertyValue instanceof List) {
211       for (Object prop : (List) propertyValue) {
212         String ret = extractResourceId(prop, translateTo);
213         if (ret != null) {
214           return ret;
215         }
216       }
217     }
218     logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
219         + translateTo.getResource().getType()
220         + "' invalid format of property value, therefore this resource will be ignored in TOSCA "
221         + "translation.");
222     return null;
223   }
224
225   private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
226
227
228     mdcDataDebugMessage.debugEntryMessage(null, null);
229
230     RequirementAssignment requirement = new RequirementAssignment();
231     requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
232     requirement.setNode(translatedNetworkResourceId);
233     requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
234     mdcDataDebugMessage.debugExitMessage(null, null);
235     return requirement;
236   }
237
238
239 }