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