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.heat.datatypes.HeatBoolean;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
27 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
28 import org.openecomp.sdc.translator.datatypes.heattotosca.PropertyRegexMatcher;
29 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
30 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
31 import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Optional;
40 public class ContrailTranslationHelper {
42 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
44 * Gets compute node type id.
46 * @param contrailServiceTemplateResource contrail service teamplte resource
47 * @param contrailServiceTemplateResourceId contrailservice template resource id
48 * @param contrailServiceTemplateTranslatedId contrail service tempalte resource translated id
49 * @return the compute node type id
51 public String getComputeNodeTypeId(Resource contrailServiceTemplateResource,
52 String contrailServiceTemplateResourceId,
53 String contrailServiceTemplateTranslatedId,
54 TranslationContext context) {
55 mdcDataDebugMessage.debugEntryMessage(null, null);
56 NameExtractor nodeTypeNameExtractor =
57 context.getNameExtractorImpl(ConfigConstants.CONTRAIL_COMPUTE_NODE_TYPE_IMPL_KEY);
59 mdcDataDebugMessage.debugExitMessage(null, null);
60 return nodeTypeNameExtractor
61 .extractNodeTypeName(contrailServiceTemplateResource, contrailServiceTemplateResourceId,
62 contrailServiceTemplateTranslatedId);
66 * Get property Regx matcher list.
68 * @return Regex exprission per contrail service template resource property, while contail compute
69 * type name is consider when setting the name value
71 public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
72 List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
74 .add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"),
77 .add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"),
79 return propertyRegexMatchers;
82 public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName,
83 String serviceInstanceTranslatedId) {
86 mdcDataDebugMessage.debugEntryMessage(null, null);
88 mdcDataDebugMessage.debugExitMessage(null, null);
89 return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
93 * Translate fn split function optional.
95 * @param propertyValue the property value
96 * @param listSize the list size
97 * @param includeBooleanValue the include boolean value
98 * @return the optional
100 public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue,
103 includeBooleanValue) {
104 List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
106 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
107 Map<String, Object> propMap = (Map) propertyValue;
108 Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
109 Object entity = entry.getValue();
110 String key = entry.getKey();
113 if (key.equals("Fn::Split") && entity instanceof List) {
114 tokenChar = (String) ((List) entity).get(0);
115 Object refParameter = ((List) entity).get(1);
117 for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
118 Map<String, List> tokenPropertyValue = new HashMap<>();
119 tokenPropertyValue.put("token", new ArrayList<>());
121 if (refParameter instanceof Map && ((Map) refParameter).get("Ref") != null) {
122 Map<String, String> stringWithToken = new HashMap<>();
123 ((Map) stringWithToken)
124 .put(ToscaFunctions.GET_INPUT.getDisplayName(), ((Map) refParameter).get("Ref"));
125 tokenPropertyValue.get("token").add(stringWithToken);
126 } else if (refParameter instanceof String) {
127 if (includeBooleanValue) {
128 StringBuilder booleanBuilder = new StringBuilder();
129 String[] booleanValueList = ((String) refParameter).split(tokenChar);
130 for (int i = 0; i < booleanValueList.length; i++) {
132 booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
134 booleanBuilder.append(tokenChar);
135 booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
138 tokenPropertyValue.get("token").add(booleanBuilder.toString());
140 tokenPropertyValue.get("token").add(refParameter);
143 tokenPropertyValue.get("token").add(tokenChar);
144 tokenPropertyValue.get("token").add(substringIndex);
145 tokenPropertyValueList.add(tokenPropertyValue);
148 return Optional.of(tokenPropertyValueList);
153 return Optional.empty();