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;
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.tosca.datatypes.ToscaCapabilityType;
27 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
29 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
30 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
31 import org.openecomp.sdc.tosca.services.DataModelUtil;
32 import org.openecomp.sdc.tosca.services.ToscaConstants;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.ResourceReferenceType;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
38 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
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 LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
54 public void translate(TranslateTo translateTo) {
56 NodeTemplate nodeTemplate = new NodeTemplate();
57 nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK.getDisplayName());
58 nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
59 .getToscaPropertiesSimpleConversion(translateTo.getResource().getProperties(),
60 nodeTemplate.getProperties(), translateTo.getHeatFileName(),
61 translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
62 nodeTemplate, translateTo.getContext()));
63 Optional<String> resourceTranslatedId = getResourceTranslatedId(translateTo.getHeatFileName(),
64 translateTo.getHeatOrchestrationTemplate(), translateTo.getResourceId(),
65 translateTo.getContext());
66 if (resourceTranslatedId.isPresent()) {
67 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), resourceTranslatedId.get(),
70 linkToPolicyNodeTemplate(translateTo);
73 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
74 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
75 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
78 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
79 NodeTemplate policyNodeTemplate = DataModelUtil
80 .getNodeTemplate(translateTo.getServiceTemplate(),
81 (String) attachedResourceId.getTranslatedId());
83 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
84 createRequirementAssignment(translateTo.getTranslatedId()));
88 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
90 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
91 if (propertyValue != null) {
92 return extractNetworkPolicyId(propertyValue, translateTo);
98 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
99 TranslateTo translateTo) {
100 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
102 if (propertyValue instanceof List) {
103 for (Object value : (List) propertyValue) {
104 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
107 AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
108 if (resourceId != null) {
109 attachedResourceIdList.add(resourceId);
112 return attachedResourceIdList;
115 private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
117 Optional<String> translatedPolicyResourceId;
118 String policyResourceId = extractResourceId(propertyValue, translateTo);
119 if (policyResourceId == null) {
123 Resource policyResource = HeatToToscaUtil
124 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId,
125 translateTo.getHeatFileName());
126 if (!policyResource.getType()
127 .equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE.getHeatResource())) {
130 translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
131 .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
132 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
133 translateTo.getContext());
134 if (!translatedPolicyResourceId.isPresent()) {
135 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
136 + translateTo.getResource().getType()
137 + "' property network_policy_refs is referenced to an "
138 + "unsupported resource the connection will be ignored in TOSCA translation.");
142 new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
143 ResourceReferenceType.GET_ATTR);
146 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
149 if (propertyValue instanceof Map) {
150 if (((Map) propertyValue).containsKey("get_attr")) {
151 value = ((Map) propertyValue).get("get_attr");
152 if (value instanceof List) {
153 if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) {
154 if (((List) value).get(0) instanceof String) {
155 return (String) ((List) value).get(0);
157 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
158 + translateTo.getResource().getType()
159 + "' has property with invalid format of 'get_attr' function "
160 + "with 'fq_name' value, therefore this property"
161 + " will be ignored in TOSCA translation.");
165 } else if (((Map) propertyValue).containsKey("get_resource")) {
166 value = ((Map) propertyValue).get("get_resource");
167 if (value instanceof String) {
168 return (String) value;
170 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
171 + translateTo.getResource().getType()
172 + "' has property invalid format of 'get_resource' function, therefore"
173 + " this property will be ignored in TOSCA translation.");
176 Collection<Object> valCollection = ((Map) propertyValue).values();
177 for (Object entryValue : valCollection) {
178 String ret = extractResourceId(entryValue, translateTo);
185 } else if (propertyValue instanceof List) {
186 for (Object prop : (List) propertyValue) {
187 String ret = extractResourceId(prop, translateTo);
193 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
194 + translateTo.getResource().getType()
195 + "' invalid format of property value, therefore "
196 + "this resource will be ignored in TOSCA translation.");
200 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
201 RequirementAssignment requirement = new RequirementAssignment();
202 requirement.setCapability(ToscaCapabilityType.ATTACHMENT.getDisplayName());
203 requirement.setNode(translatedNetworkResourceId);
204 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO.getDisplayName());