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