[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / 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.resourcetranslation;
22
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
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.model.NodeTemplate;
36 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
37 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
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
55
56     mdcDataDebugMessage.debugEntryMessage(null, null);
57
58     String heatFileName = translateTo.getHeatFileName();
59     String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
60     if (translatedNetworkResourceId == null) {
61       mdcDataDebugMessage.debugExitMessage(null, null);
62       return;
63     }
64
65     NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
66     if (policyNodeTemplate != null) {
67       DataModelUtil
68           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
69               createRequirementAssignment(translatedNetworkResourceId));
70     }
71
72     mdcDataDebugMessage.debugExitMessage(null, null);
73   }
74
75   @Override
76   protected String generateTranslatedId(TranslateTo translateTo) {
77     return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
78         .toString();
79   }
80
81   @Override
82   protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
83       TranslateTo translateTo) {
84     return Optional.empty();
85   }
86
87   private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
88                                                        String heatFileName) {
89
90
91     mdcDataDebugMessage.debugEntryMessage(null, null);
92
93     AttachedResourceId attachedPolicyResourceId =
94         extractAttachedResourceIdHandleMissing(translateTo, "policy");
95     NodeTemplate policyNodeTemplate = new NodeTemplate();
96     Optional<String> policyResourceId =
97         HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
98     if (policyResourceId.isPresent()) {
99       policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
100     } else {
101       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
102           + translateTo.getResource().getType()
103           + "' include 'policy' property without 'get_attr' of 'fq_name'/'get_resource' function,"
104           + " therefore this resource will be ignored in TOSCA translation.");
105     }
106
107     mdcDataDebugMessage.debugExitMessage(null, null);
108     return policyNodeTemplate;
109   }
110
111   private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
112                                              String policyResourceId) {
113
114
115     mdcDataDebugMessage.debugEntryMessage(null, null);
116
117     Resource policyResource = HeatToToscaUtil
118         .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
119     Optional<String> translatedPolicyResourceId =
120         ResourceTranslationFactory.getInstance(policyResource)
121             .translateResource(heatFileName, translateTo.getServiceTemplate(),
122                 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
123                 translateTo.getContext());
124     if (!translatedPolicyResourceId.isPresent()) {
125       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
126           + translateTo.getResource().getType()
127           + "' include unsupported policy resource, therefore this resource will be ignored in "
128           + "TOSCA translation. ");
129
130       mdcDataDebugMessage.debugExitMessage(null, null);
131       return null;
132     }
133
134     mdcDataDebugMessage.debugExitMessage(null, null);
135     return DataModelUtil
136         .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
137   }
138
139   private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
140
141
142     mdcDataDebugMessage.debugEntryMessage(null, null);
143
144     AttachedResourceId attachedNetworkResourceId =
145         extractAttachedResourceIdHandleMissing(translateTo, "network");
146
147     String translatedNetworkResourceId = null;
148     if (attachedNetworkResourceId.isGetResource()) {
149       translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
150     } else {
151       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
152           + translateTo.getResource().getType()
153           + "' include 'network' property without 'get_resource' function, therefore this "
154           + "resource will be ignored in TOSCA translation.");
155     }
156
157     mdcDataDebugMessage.debugExitMessage(null, null);
158     return translatedNetworkResourceId;
159   }
160
161   private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
162
163
164     mdcDataDebugMessage.debugEntryMessage(null, null);
165
166     RequirementAssignment requirement = new RequirementAssignment();
167     requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
168     requirement.setNode(translatedNetworkResourceId);
169     requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
170     mdcDataDebugMessage.debugExitMessage(null, null);
171     return requirement;
172   }
173
174   private AttachedResourceId extractAttachedResourceIdHandleMissing(
175       TranslateTo translateTo, String propertyName) {
176
177
178     mdcDataDebugMessage.debugEntryMessage(null, null);
179
180     Optional<AttachedResourceId> attachedResourceId =
181         HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
182
183     if (!attachedResourceId.isPresent()) {
184       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
185           LoggerTragetServiceName.CREATE_REQUIREMENT_ASSIGNMENT, ErrorLevel.ERROR.name(),
186           LoggerErrorCode.DATA_ERROR.getErrorCode(),
187           LoggerErrorDescription.MISSING_MANDATORY_PROPERTY);
188       throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
189     }
190
191     mdcDataDebugMessage.debugExitMessage(null, null);
192     return attachedResourceId.get();
193   }
194 }