[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 / ResourceTranslationContrailAttachPolicyImpl.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.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
26 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
27 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
28 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
29 import org.openecomp.sdc.tosca.services.DataModelUtil;
30 import org.openecomp.sdc.tosca.services.ToscaConstants;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
35 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import java.util.Optional;
40
41 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
42   protected static Logger logger =
43       LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
44
45   @Override
46   protected void translate(TranslateTo translateTo) {
47     String heatFileName = translateTo.getHeatFileName();
48     String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
49     if (translatedNetworkResourceId == null) {
50       return;
51     }
52
53     NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
54     if (policyNodeTemplate != null) {
55       DataModelUtil
56           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
57               createRequirementAssignment(translatedNetworkResourceId));
58     }
59   }
60
61   private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
62                                                        String heatFileName) {
63     Optional<AttachedResourceId> attachedPolicyResourceId =
64         extractAttachedResourceIdHandleMissing(translateTo, "policy");
65     NodeTemplate policyNodeTemplate = new NodeTemplate();
66     Optional<String> policyResourceId =
67         HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId.get());
68     if (policyResourceId.isPresent()) {
69       policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
70     } else {
71       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
72           + translateTo.getResource().getType()
73           + "' include 'policy' property without 'get_attr' of 'fq_name'/'get_resource'"
74           +   " function, therefore this resource will be ignored in TOSCA translation.");
75     }
76     return policyNodeTemplate;
77   }
78
79   private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
80                                              String policyResourceId) {
81     Resource policyResource = HeatToToscaUtil
82         .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
83     Optional<String> translatedPolicyResourceId =
84         ResourceTranslationFactory.getInstance(policyResource)
85             .translateResource(heatFileName, translateTo.getServiceTemplate(),
86                 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
87                 translateTo.getContext());
88     if (!translatedPolicyResourceId.isPresent()) {
89       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
90           + translateTo.getResource().getType()
91           + "' include unsupported policy resource, therefore this "
92           +  "resource will be ignored in TOSCA translation. ");
93       return null;
94     }
95     return DataModelUtil
96         .getNodeTemplate(translateTo.getServiceTemplate(),
97                 translatedPolicyResourceId.get());
98   }
99
100   private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
101     Optional<AttachedResourceId> attachedNetworkResourceId =
102         extractAttachedResourceIdHandleMissing(translateTo, "network");
103
104     String translatedNetworkResourceId = null;
105     if (attachedNetworkResourceId.get().isGetResource()) {
106       translatedNetworkResourceId = (String) attachedNetworkResourceId.get().getTranslatedId();
107     } else {
108       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
109           + translateTo.getResource().getType()
110           + "' include 'network' property without 'get_resource' function, "
111           +   "therefore this resource will be ignored in TOSCA translation.");
112     }
113     return translatedNetworkResourceId;
114   }
115
116   private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
117     RequirementAssignment requirement = new RequirementAssignment();
118     requirement.setCapability(ToscaCapabilityType.ATTACHMENT.getDisplayName());
119     requirement.setNode(translatedNetworkResourceId);
120     requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO.getDisplayName());
121     return requirement;
122   }
123
124   private Optional<AttachedResourceId> extractAttachedResourceIdHandleMissing(
125       TranslateTo translateTo, String propertyName) {
126     Optional<AttachedResourceId> attachedResourceId =
127         HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
128
129     if (!attachedResourceId.isPresent()) {
130       throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
131     }
132     return attachedResourceId;
133   }
134
135
136   @Override
137   protected String generateTranslatedId(TranslateTo translateTo) {
138     return extractAttachedResourceIdHandleMissing(translateTo, "network").get().getEntityId()
139         .toString();
140   }
141 }