2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
21 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
22 import org.openecomp.sdc.heat.datatypes.model.Resource;
23 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
27 import org.openecomp.sdc.tosca.services.DataModelUtil;
28 import org.openecomp.sdc.tosca.services.ToscaConstants;
29 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.ReferenceType;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
32 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
33 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
34 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
38 import static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment;
39 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.*;
41 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
43 private static final String FQ_NAME = "fq_name";
44 protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
47 public void translate(TranslateTo translateTo) {
48 NodeTemplate nodeTemplate = new NodeTemplate();
49 nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK);
50 nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
51 .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),
52 translateTo.getResourceId(), translateTo.getResource().getProperties(),
53 nodeTemplate.getProperties(), translateTo.getHeatFileName(),
54 translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
55 nodeTemplate, translateTo.getContext()));
56 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
58 linkToPolicyNodeTemplate(translateTo);
61 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
62 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
63 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
66 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
67 NodeTemplate policyNodeTemplate = DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(),
68 (String) attachedResourceId.getTranslatedId());
69 DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
70 createAttachmentRequirementAssignment(translateTo.getTranslatedId()));
74 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
75 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
76 if (propertyValue != null) {
77 return extractNetworkPolicyId(propertyValue, translateTo);
79 return Collections.emptyList();
82 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
83 TranslateTo translateTo) {
84 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
85 if (propertyValue instanceof List) {
86 for (Object value : (List) propertyValue) {
87 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
90 AttachedResourceId resourceId = parseNetworkPolicyId(propertyValue, translateTo);
91 if (resourceId != null) {
92 attachedResourceIdList.add(resourceId);
95 return attachedResourceIdList;
98 private AttachedResourceId parseNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
99 Optional<String> translatedPolicyResourceId;
100 String policyResourceId = extractResourceId(propertyValue, translateTo);
101 if (policyResourceId == null) {
105 Resource policyResource = HeatToToscaUtil.getResource(translateTo.getHeatOrchestrationTemplate(),
106 policyResourceId, translateTo.getHeatFileName());
107 if (!policyResource.getType().equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE
108 .getHeatResource())) {
111 translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
112 .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
113 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
114 translateTo.getContext());
115 if (!translatedPolicyResourceId.isPresent()) {
116 logger.warn(LOG_INVALID_NETWORK_POLICY_REFS_RESOURCE,
117 translateTo.getResourceId(), translateTo.getResource().getType());
120 return new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId, ReferenceType.GET_ATTR);
123 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
124 if (propertyValue instanceof Map) {
125 return extractResourceIdFromMapProperty((Map) propertyValue, translateTo);
126 } else if (propertyValue instanceof List) {
127 String resourceId = extractResourceIdFromListProperty((List) propertyValue, translateTo);
128 if (resourceId != null) {
132 logger.warn(LOG_INVALID_PROPERTY_VALUE_FORMAT, translateTo.getResourceId(),
133 translateTo.getResource().getType());
137 private String extractResourceIdFromMapProperty(Map propertyValue, TranslateTo translateTo) {
139 String resourceId = null;
140 if (propertyValue.containsKey(ResourceReferenceFunctions.GET_ATTR.getFunction())) {
141 value = propertyValue.get(ResourceReferenceFunctions.GET_ATTR.getFunction());
142 if (value instanceof List && extractResourceIdFromGetAttrList(translateTo, (List) value)) {
143 resourceId = (String) ((List) value).get(0);
145 } else if (propertyValue.containsKey(ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
146 resourceId = extractResourceIdFromGetResource(propertyValue, translateTo);
148 resourceId = extractResourceIdFromPropertyValues(propertyValue, translateTo);
153 private boolean extractResourceIdFromGetAttrList(TranslateTo translateTo, List<Object> value) {
154 if (value.size() == 2 && FQ_NAME.equals(value.get(1))) {
155 if (value.get(0) instanceof String) {
158 logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME, translateTo.getResourceId(),
159 translateTo.getResource().getType());
165 private String extractResourceIdFromPropertyValues(Map propertyValue, TranslateTo translateTo) {
166 Collection<Object> valCollection = propertyValue.values();
167 for (Object entryValue : valCollection) {
168 String ret = extractResourceId(entryValue, translateTo);
176 private String extractResourceIdFromListProperty(List<Object> propertyValue, TranslateTo translateTo) {
177 for (Object prop : propertyValue) {
178 String resourceId = extractResourceId(prop, translateTo);
179 if (resourceId != null) {
186 private String extractResourceIdFromGetResource(Map propertyValue, TranslateTo translateTo) {
188 value = propertyValue.get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
189 if (value instanceof String) {
190 return (String) value;
192 logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE, translateTo.getResourceId(),
193 translateTo.getResource().getType());