2 * ============LICENSE_START=======================================================
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
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.translator.services.heattotosca.impl.resourcetranslation;
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;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
47 import java.util.Optional;
49 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
51 protected static Logger logger =
52 (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
55 public void translate(TranslateTo translateTo) {
58 mdcDataDebugMessage.debugEntryMessage(null, null);
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(),
70 linkToPolicyNodeTemplate(translateTo);
72 mdcDataDebugMessage.debugExitMessage(null, null);
75 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
78 mdcDataDebugMessage.debugEntryMessage(null, null);
80 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
81 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
84 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
85 NodeTemplate policyNodeTemplate = DataModelUtil
86 .getNodeTemplate(translateTo.getServiceTemplate(),
87 (String) attachedResourceId.getTranslatedId());
89 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
90 createRequirementAssignment(translateTo.getTranslatedId()));
93 mdcDataDebugMessage.debugExitMessage(null, null);
96 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
99 mdcDataDebugMessage.debugEntryMessage(null, null);
101 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
102 if (propertyValue != null) {
103 mdcDataDebugMessage.debugExitMessage(null, null);
104 return extractNetworkPolicyId(propertyValue, translateTo);
106 mdcDataDebugMessage.debugExitMessage(null, null);
111 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
112 TranslateTo translateTo) {
115 mdcDataDebugMessage.debugEntryMessage(null, null);
117 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
119 if (propertyValue instanceof List) {
120 for (Object value : (List) propertyValue) {
121 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
124 AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
125 if (resourceId != null) {
126 attachedResourceIdList.add(resourceId);
130 mdcDataDebugMessage.debugExitMessage(null, null);
131 return attachedResourceIdList;
134 private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
137 mdcDataDebugMessage.debugEntryMessage(null, null);
139 Optional<String> translatedPolicyResourceId;
140 String policyResourceId = extractResourceId(propertyValue, translateTo);
141 if (policyResourceId == null) {
142 mdcDataDebugMessage.debugExitMessage(null, null);
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);
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);
166 AttachedResourceId attachedResourceId =
167 new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
168 ReferenceType.GET_ATTR);
169 mdcDataDebugMessage.debugExitMessage(null, null);
170 return attachedResourceId;
173 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
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);
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.");
191 } else if (((Map) propertyValue).containsKey("get_resource")) {
192 value = ((Map) propertyValue).get("get_resource");
193 if (value instanceof String) {
194 return (String) value;
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.");
202 Collection<Object> valCollection = ((Map) propertyValue).values();
203 for (Object entryValue : valCollection) {
204 String ret = extractResourceId(entryValue, translateTo);
211 } else if (propertyValue instanceof List) {
212 for (Object prop : (List) propertyValue) {
213 String ret = extractResourceId(prop, translateTo);
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 "
226 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
229 mdcDataDebugMessage.debugEntryMessage(null, null);
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);