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.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;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.List;
46 import java.util.Optional;
48 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
50 protected static Logger logger =
51 (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
54 public void translate(TranslateTo translateTo) {
57 mdcDataDebugMessage.debugEntryMessage(null, null);
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(),
69 linkToPolicyNodeTemplate(translateTo);
71 mdcDataDebugMessage.debugExitMessage(null, null);
74 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
77 mdcDataDebugMessage.debugEntryMessage(null, null);
79 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
80 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
83 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
84 NodeTemplate policyNodeTemplate = DataModelUtil
85 .getNodeTemplate(translateTo.getServiceTemplate(),
86 (String) attachedResourceId.getTranslatedId());
88 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
89 createRequirementAssignment(translateTo.getTranslatedId()));
92 mdcDataDebugMessage.debugExitMessage(null, null);
95 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
98 mdcDataDebugMessage.debugEntryMessage(null, null);
100 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
101 if (propertyValue != null) {
102 mdcDataDebugMessage.debugExitMessage(null, null);
103 return extractNetworkPolicyId(propertyValue, translateTo);
105 mdcDataDebugMessage.debugExitMessage(null, null);
110 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
111 TranslateTo translateTo) {
114 mdcDataDebugMessage.debugEntryMessage(null, null);
116 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
118 if (propertyValue instanceof List) {
119 for (Object value : (List) propertyValue) {
120 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
123 AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
124 if (resourceId != null) {
125 attachedResourceIdList.add(resourceId);
129 mdcDataDebugMessage.debugExitMessage(null, null);
130 return attachedResourceIdList;
133 private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
136 mdcDataDebugMessage.debugEntryMessage(null, null);
138 Optional<String> translatedPolicyResourceId;
139 String policyResourceId = extractResourceId(propertyValue, translateTo);
140 if (policyResourceId == null) {
141 mdcDataDebugMessage.debugExitMessage(null, null);
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);
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);
165 AttachedResourceId attachedResourceId =
166 new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
167 ReferenceType.GET_ATTR);
168 mdcDataDebugMessage.debugExitMessage(null, null);
169 return attachedResourceId;
172 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
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);
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.");
190 } else if (((Map) propertyValue).containsKey("get_resource")) {
191 value = ((Map) propertyValue).get("get_resource");
192 if (value instanceof String) {
193 return (String) value;
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.");
201 Collection<Object> valCollection = ((Map) propertyValue).values();
202 for (Object entryValue : valCollection) {
203 String ret = extractResourceId(entryValue, translateTo);
210 } else if (propertyValue instanceof List) {
211 for (Object prop : (List) propertyValue) {
212 String ret = extractResourceId(prop, translateTo);
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 "
225 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
228 mdcDataDebugMessage.debugEntryMessage(null, null);
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);