2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.helper;
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.common.utils.CommonUtil;
25 import org.openecomp.sdc.heat.datatypes.HeatBoolean;
26 import org.openecomp.sdc.heat.datatypes.model.Resource;
27 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
28 import org.openecomp.sdc.tosca.services.ToscaConstants;
29 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.PropertyRegexMatcher;
31 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
32 import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Optional;
42 public class ContrailTranslationHelper {
44 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
46 * Gets compute node type id.
48 * @param contrailServiceTemplateResource contrail service teamplte resource
49 * @param contrailServiceTemplateResourceId contrailservice template resource id
50 * @param contrailServiceTemplateTranslatedId contrail service tempalte resource translated id
51 * @return the compute node type id
53 public String getComputeNodeTypeId(Resource contrailServiceTemplateResource,
54 String contrailServiceTemplateResourceId,
55 String contrailServiceTemplateTranslatedId,
56 TranslationContext context) {
57 mdcDataDebugMessage.debugEntryMessage(null, null);
58 NameExtractor nodeTypeNameExtractor =
59 context.getNameExtractorImpl(ConfigConstants.CONTRAIL_COMPUTE_NODE_TYPE_IMPL_KEY);
61 mdcDataDebugMessage.debugExitMessage(null, null);
62 return nodeTypeNameExtractor
63 .extractNodeTypeName(contrailServiceTemplateResource, contrailServiceTemplateResourceId,
64 contrailServiceTemplateTranslatedId);
68 * Get property Regx matcher list.
70 * @return Regex exprission per contrail service template resource property, while contail compute
71 * type name is consider when setting the name value
73 public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
74 List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
76 .add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"),
79 .add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"),
81 return propertyRegexMatchers;
84 public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName,
85 String serviceInstanceTranslatedId) {
88 mdcDataDebugMessage.debugEntryMessage(null, null);
90 mdcDataDebugMessage.debugExitMessage(null, null);
91 return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
95 * Translate fn split function optional.
97 * @param propertyValue the property value
98 * @param listSize the list size
99 * @param includeBooleanValue the include boolean value
100 * @return the optional
102 public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue,
105 includeBooleanValue) {
106 List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
108 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
109 Map<String, Object> propMap = (Map) propertyValue;
110 Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
111 Object entity = entry.getValue();
112 String key = entry.getKey();
115 if (key.equals("Fn::Split") && entity instanceof List) {
116 tokenChar = (String) ((List) entity).get(0);
117 Object refParameter = ((List) entity).get(1);
119 for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
120 Map<String, List> tokenPropertyValue = new HashMap<>();
121 tokenPropertyValue.put("token", new ArrayList<>());
123 if (refParameter instanceof Map && ((Map) refParameter).get("Ref") != null) {
124 Map<String, String> stringWithToken = new HashMap<>();
125 ((Map) stringWithToken)
126 .put(ToscaFunctions.GET_INPUT.getDisplayName(), ((Map) refParameter).get("Ref"));
127 tokenPropertyValue.get("token").add(stringWithToken);
128 } else if (refParameter instanceof String) {
129 if (includeBooleanValue) {
130 StringBuffer booleanBuffer = new StringBuffer();
131 String[] booleanValueList = ((String) refParameter).split(tokenChar);
132 for (int i = 0; i < booleanValueList.length; i++) {
134 booleanBuffer.append(HeatBoolean.eval(booleanValueList[i]));
136 booleanBuffer.append(tokenChar);
137 booleanBuffer.append(HeatBoolean.eval(booleanValueList[i]));
140 tokenPropertyValue.get("token").add(booleanBuffer.toString());
142 tokenPropertyValue.get("token").add(refParameter);
145 tokenPropertyValue.get("token").add(tokenChar);
146 tokenPropertyValue.get("token").add(substringIndex);
147 tokenPropertyValueList.add(tokenPropertyValue);
150 return Optional.of(tokenPropertyValueList);
155 return Optional.empty();