[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / ResourceTranslationContrailV2VirtualNetworkImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.impl;
22
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;
41
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Optional;
47
48 public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase {
49
50   protected static Logger logger =
51       LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class);
52
53   @Override
54   public void translate(TranslateTo translateTo) {
55
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(),
68           nodeTemplate);
69     }
70     linkToPolicyNodeTemplate(translateTo);
71   }
72
73   private void linkToPolicyNodeTemplate(TranslateTo translateTo) {
74     List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo);
75     if (CollectionUtils.isEmpty(networkPolicyIdList)) {
76       return;
77     }
78     for (AttachedResourceId attachedResourceId : networkPolicyIdList) {
79       NodeTemplate policyNodeTemplate = DataModelUtil
80           .getNodeTemplate(translateTo.getServiceTemplate(),
81               (String) attachedResourceId.getTranslatedId());
82       DataModelUtil
83           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
84               createRequirementAssignment(translateTo.getTranslatedId()));
85     }
86   }
87
88   private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) {
89
90     Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs");
91     if (propertyValue != null) {
92       return extractNetworkPolicyId(propertyValue, translateTo);
93     } else {
94       return null;
95     }
96   }
97
98   private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue,
99                                                           TranslateTo translateTo) {
100     List<AttachedResourceId> attachedResourceIdList = new ArrayList<>();
101
102     if (propertyValue instanceof List) {
103       for (Object value : (List) propertyValue) {
104         attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo));
105       }
106     } else {
107       AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo);
108       if (resourceId != null) {
109         attachedResourceIdList.add(resourceId);
110       }
111     }
112     return attachedResourceIdList;
113   }
114
115   private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) {
116
117     Optional<String> translatedPolicyResourceId;
118     String policyResourceId = extractResourceId(propertyValue, translateTo);
119     if (policyResourceId == null) {
120       return null;
121     }
122
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())) {
128       return null;
129     }
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.");
139       return null;
140     }
141     return
142         new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId,
143             ResourceReferenceType.GET_ATTR);
144   }
145
146   private String extractResourceId(Object propertyValue, TranslateTo translateTo) {
147
148     Object value;
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);
156             } else {
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.");
162             }
163           }
164         }
165       } else if (((Map) propertyValue).containsKey("get_resource")) {
166         value = ((Map) propertyValue).get("get_resource");
167         if (value instanceof String) {
168           return (String) value;
169         } else {
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.");
174         }
175       } else {
176         Collection<Object> valCollection = ((Map) propertyValue).values();
177         for (Object entryValue : valCollection) {
178           String ret = extractResourceId(entryValue, translateTo);
179           if (ret != null) {
180             return ret;
181           }
182
183         }
184       }
185     } else if (propertyValue instanceof List) {
186       for (Object prop : (List) propertyValue) {
187         String ret = extractResourceId(prop, translateTo);
188         if (ret != null) {
189           return ret;
190         }
191       }
192     }
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.");
197     return null;
198   }
199
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());
205     return requirement;
206   }
207
208
209 }