re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceTranslationContrailV2VirtualNetworkImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
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;
35
36 import java.util.*;
37
38 import static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment;
39 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.*;
40
41 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
42
43     private static final String FQ_NAME = "fq_name";
44     protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
45
46     @Override
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(),
57                 nodeTemplate);
58         linkToPolicyNodeTemplate(translateTo);
59     }
60
61     private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
62         List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
63         if (CollectionUtils.isEmpty(networkPolicyIdList)) {
64             return;
65         }
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()));
71         }
72     }
73
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);
78         }
79         return Collections.emptyList();
80     }
81
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));
88             }
89         } else {
90             AttachedResourceId resourceId = parseNetworkPolicyId(propertyValue, translateTo);
91             if (resourceId != null) {
92                 attachedResourceIdList.add(resourceId);
93             }
94         }
95         return attachedResourceIdList;
96     }
97
98     private AttachedResourceId parseNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
99         Optional<String> translatedPolicyResourceId;
100         String policyResourceId = extractResourceId(propertyValue, translateTo);
101         if (policyResourceId == null) {
102             return null;
103         }
104
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())) {
109             return null;
110         }
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());
118             return null;
119         }
120         return new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId, ReferenceType.GET_ATTR);
121     }
122
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) {
129                 return resourceId;
130             }
131         }
132         logger.warn(LOG_INVALID_PROPERTY_VALUE_FORMAT, translateTo.getResourceId(),
133                 translateTo.getResource().getType());
134         return null;
135     }
136
137     private String extractResourceIdFromMapProperty(Map propertyValue, TranslateTo translateTo) {
138         Object value;
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);
144             }
145         } else if (propertyValue.containsKey(ResourceReferenceFunctions.GET_RESOURCE.getFunction())) {
146             resourceId = extractResourceIdFromGetResource(propertyValue, translateTo);
147         } else {
148             resourceId = extractResourceIdFromPropertyValues(propertyValue, translateTo);
149         }
150         return resourceId;
151     }
152
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) {
156                 return true;
157             } else {
158                 logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME, translateTo.getResourceId(),
159                         translateTo.getResource().getType());
160             }
161         }
162         return false;
163     }
164
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);
169             if (ret != null) {
170                 return ret;
171             }
172         }
173         return null;
174     }
175
176     private String extractResourceIdFromListProperty(List<Object> propertyValue, TranslateTo translateTo) {
177         for (Object prop : propertyValue) {
178             String resourceId = extractResourceId(prop, translateTo);
179             if (resourceId != null) {
180                 return resourceId;
181             }
182         }
183         return null;
184     }
185
186     private String extractResourceIdFromGetResource(Map propertyValue, TranslateTo translateTo) {
187         Object value;
188         value = propertyValue.get(ResourceReferenceFunctions.GET_RESOURCE.getFunction());
189         if (value instanceof String) {
190             return (String) value;
191         } else {
192             logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE, translateTo.getResourceId(),
193                     translateTo.getResource().getType());
194         }
195         return null;
196     }
197 }