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 static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment;
20 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_NETWORK_POLICY_REFS_RESOURCE;
21 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME;
22 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE;
23 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_VALUE_FORMAT;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.Optional;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
34 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
35 import org.openecomp.sdc.heat.datatypes.model.Resource;
36 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
40 import org.openecomp.sdc.tosca.services.DataModelUtil;
41 import org.openecomp.sdc.tosca.services.ToscaConstants;
42 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
43 import org.openecomp.sdc.translator.datatypes.heattotosca.ReferenceType;
44 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
45 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
46 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
47 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
49 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
51 private static final String FQ_NAME = "fq_name";
52 protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
55 public void translate(TranslateTo translateTo) {
56 NodeTemplate nodeTemplate = new NodeTemplate();
57 nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK);
58 nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
59 .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),
60 translateTo.getResourceId(), translateTo.getResource().getProperties(),
61 nodeTemplate.getProperties(), translateTo.getHeatFileName(),
62 translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
63 nodeTemplate, translateTo.getContext()));
64 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
66 linkToPolicyNodeTemplate(translateTo);
69 private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
70 List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
71 if (CollectionUtils.isEmpty(networkPolicyIdList)) {
74 for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
75 NodeTemplate policyNodeTemplate = DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(),
76 (String) attachedResourceId.getTranslatedId());
77 DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
78 createAttachmentRequirementAssignment(translateTo.getTranslatedId()));
82 private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
83 Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
84 if (propertyValue != null) {
85 return extractNetworkPolicyId(propertyValue, translateTo);
87 return Collections.emptyList();
90 private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
91 TranslateTo translateTo) {
92 List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
93 if (propertyValue instanceof List) {
94 for (Object value : (List) propertyValue) {
95 attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
98 AttachedResourceId resourceId = parseNetworkPolicyId(propertyValue, translateTo);
99 if (resourceId != null) {
100 attachedResourceIdList.add(resourceId);
103 return attachedResourceIdList;
106 private AttachedResourceId parseNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
107 Optional<String> translatedPolicyResourceId;
108 String policyResourceId = extractResourceId(propertyValue, translateTo);
109 if (policyResourceId == null) {
113 Resource policyResource = HeatToToscaUtil.getResource(translateTo.getHeatOrchestrationTemplate(),
114 policyResourceId, translateTo.getHeatFileName());
115 if (!policyResource.getType().equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE
116 .getHeatResource())) {
119 translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource)
120 .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(),
121 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
122 translateTo.getContext());
123 if (!translatedPolicyResourceId.isPresent()) {
124 logger.warn(LOG_INVALID_NETWORK_POLICY_REFS_RESOURCE,
125 translateTo.getResourceId(), translateTo.getResource().getType());
128 return new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId, ReferenceType.GET_ATTR);
131 private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
132 if (propertyValue instanceof Map) {
133 return extractResourceIdFromMapProperty((Map) propertyValue, translateTo);
134 } else if (propertyValue instanceof List) {
135 String resourceId = extractResourceIdFromListProperty((List) propertyValue, translateTo);
136 if (resourceId != null) {
140 logger.warn(LOG_INVALID_PROPERTY_VALUE_FORMAT, translateTo.getResourceId(),
141 translateTo.getResource().getType());
145 private String extractResourceIdFromMapProperty(Map propertyValue, TranslateTo translateTo) {
147 String resourceId = null;
148 if (propertyValue.containsKey(ResourceReferenceFunctions.GET_ATTR.getFunction())) {
149 value = propertyValue.get(ResourceReferenceFunctions.GET_ATTR.getFunction());
150 if (value instanceof List && extractResourceIdFromGetAttrList(translateTo, (List) value)) {
151 resourceId = (String) ((List) value).get(0);
153 } else if (propertyValue.containsKey(ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
154 resourceId = extractResourceIdFromGetResource(propertyValue, translateTo);
156 resourceId = extractResourceIdFromPropertyValues(propertyValue, translateTo);
161 private boolean extractResourceIdFromGetAttrList(TranslateTo translateTo, List<Object> value) {
162 if (value.size() == 2 && FQ_NAME.equals(value.get(1))) {
163 if (value.get(0) instanceof String) {
166 logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME, translateTo.getResourceId(),
167 translateTo.getResource().getType());
173 private String extractResourceIdFromPropertyValues(Map propertyValue, TranslateTo translateTo) {
174 Collection<Object> valCollection = propertyValue.values();
175 for (Object entryValue : valCollection) {
176 String ret = extractResourceId(entryValue, translateTo);
184 private String extractResourceIdFromListProperty(List<Object> propertyValue, TranslateTo translateTo) {
185 for (Object prop : propertyValue) {
186 String resourceId = extractResourceId(prop, translateTo);
187 if (resourceId != null) {
194 private String extractResourceIdFromGetResource(Map propertyValue, TranslateTo translateTo) {
196 value = propertyValue.get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
197 if (value instanceof String) {
198 return (String) value;
200 logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE, translateTo.getResourceId(),
201 translateTo.getResource().getType());