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.onap.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.onap.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) {
55 NodeTemplate nodeTemplate = new NodeTemplate();
56 nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK);
57 nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
58 .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo.
59 getResourceId(),translateTo.getResource().getProperties(),
60 nodeTemplate.getProperties(), translateTo.getHeatFileName(),
61 translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
62 nodeTemplate, translateTo.getContext()));
63 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
65 linkToPolicyNodeTemplate(translateTo);
68 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
69 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
70 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
73 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
74 NodeTemplate policyNodeTemplate = DataModelUtil
75 .getNodeTemplate(translateTo.getServiceTemplate(),
76 (String) attachedResourceId.getTranslatedId());
78 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
79 createRequirementAssignment(translateTo.getTranslatedId()));
83 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
84 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
85 if (propertyValue != null) {
86 return extractNetworkPolicyId(propertyValue, translateTo);
92 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
93 TranslateTo translateTo) {
94 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
96 if (propertyValue instanceof List) {
97 for (Object value : (List) propertyValue) {
98 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
101 AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
102 if (resourceId != null) {
103 attachedResourceIdList.add(resourceId);
106 return attachedResourceIdList;
109 private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
110 Optional<String> translatedPolicyResourceId;
111 String policyResourceId = extractResourceId(propertyValue, translateTo);
112 if (policyResourceId == null) {
116 Resource policyResource = HeatToToscaUtil
117 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId,
118 translateTo.getHeatFileName());
119 if (!policyResource.getType()
120 .equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE.getHeatResource())) {
123 translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
124 .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
125 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
126 translateTo.getContext());
127 if (!translatedPolicyResourceId.isPresent()) {
128 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
129 + translateTo.getResource().getType()
130 + "' property network_policy_refs is referenced to an unsupported resource the "
131 + "connection will be ignored in TOSCA translation.");
134 AttachedResourceId attachedResourceId =
135 new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
136 ReferenceType.GET_ATTR);
137 return attachedResourceId;
140 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
143 if (propertyValue instanceof Map) {
144 if (((Map) propertyValue).containsKey("get_attr")) {
145 value = ((Map) propertyValue).get("get_attr");
146 if (value instanceof List) {
147 if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) {
148 if (((List) value).get(0) instanceof String) {
149 return (String) ((List) value).get(0);
151 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
152 + translateTo.getResource().getType()
153 + "' has property with invalid format of 'get_attr' function with 'fq_name' "
154 + "value, therefore this property will be ignored in TOSCA translation.");
158 } else if (((Map) propertyValue).containsKey("get_resource")) {
159 value = ((Map) propertyValue).get("get_resource");
160 if (value instanceof String) {
161 return (String) value;
163 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
164 + translateTo.getResource().getType()
165 + "' has property invalid format of 'get_resource' function, therefore this property"
166 + " will be ignored in TOSCA translation.");
169 Collection<Object> valCollection = ((Map) propertyValue).values();
170 for (Object entryValue : valCollection) {
171 String ret = extractResourceId(entryValue, translateTo);
178 } else if (propertyValue instanceof List) {
179 for (Object prop : (List) propertyValue) {
180 String ret = extractResourceId(prop, translateTo);
186 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
187 + translateTo.getResource().getType()
188 + "' invalid format of property value, therefore this resource will be ignored in TOSCA "
193 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
194 RequirementAssignment requirement = new RequirementAssignment();
195 requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
196 requirement.setNode(translatedNetworkResourceId);
197 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);