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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Optional;
30 import org.openecomp.core.utilities.file.FileUtils;
31 import org.openecomp.sdc.heat.datatypes.HeatBoolean;
32 import org.openecomp.sdc.heat.datatypes.model.Resource;
33 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.PropertyRegexMatcher;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
36 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
37 import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;
39 public class ContrailTranslationHelper {
41 * Gets compute node type id.
43 * @param contrailServiceTemplateResource contrail service teamplte resource
44 * @param contrailServiceTemplateResourceId contrailservice template resource id
45 * @param contrailServiceTemplateTranslatedId contrail service tempalte resource translated id
46 * @return the compute node type id
48 public String getComputeNodeTypeId(Resource contrailServiceTemplateResource,
49 String contrailServiceTemplateResourceId,
50 String contrailServiceTemplateTranslatedId,
51 TranslationContext context) {
52 NameExtractor nodeTypeNameExtractor =
53 context.getNameExtractorImpl(ConfigConstants.CONTRAIL_COMPUTE_NODE_TYPE_IMPL_KEY);
54 return nodeTypeNameExtractor
55 .extractNodeTypeName(contrailServiceTemplateResource, contrailServiceTemplateResourceId,
56 contrailServiceTemplateTranslatedId);
60 * Get property Regx matcher list.
62 * @return Regex exprission per contrail service template resource property, while contail compute
63 * type name is consider when setting the name value
65 public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
66 List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
68 .add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"),
71 .add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"),
73 return propertyRegexMatchers;
76 public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName,
77 String serviceInstanceTranslatedId) {
78 return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
82 * Translate fn split function optional.
84 * @param propertyValue the property value
85 * @param listSize the list size
86 * @param includeBooleanValue the include boolean value
87 * @return the optional
89 public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue,
92 includeBooleanValue) {
93 List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
95 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
96 Map<String, Object> propMap = (Map) propertyValue;
97 Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
98 Object entity = entry.getValue();
99 String key = entry.getKey();
102 if (key.equals("Fn::Split") && entity instanceof List) {
103 tokenChar = (String) ((List) entity).get(0);
104 Object refParameter = ((List) entity).get(1);
106 for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
107 Map<String, List> tokenPropertyValue = new HashMap<>();
108 tokenPropertyValue.put("token", new ArrayList<>());
110 if (refParameter instanceof Map && ((Map) refParameter).get("Ref") != null) {
111 Map<String, String> stringWithToken = new HashMap<>();
112 ((Map) stringWithToken)
113 .put(ToscaFunctions.GET_INPUT.getFunctionName(), ((Map) refParameter).get("Ref"));
114 tokenPropertyValue.get("token").add(stringWithToken);
115 } else if (refParameter instanceof String) {
116 if (includeBooleanValue) {
117 StringBuilder booleanBuilder = new StringBuilder();
118 String[] booleanValueList = ((String) refParameter).split(tokenChar);
119 for (int i = 0; i < booleanValueList.length; i++) {
121 booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
123 booleanBuilder.append(tokenChar);
124 booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
127 tokenPropertyValue.get("token").add(booleanBuilder.toString());
129 tokenPropertyValue.get("token").add(refParameter);
132 tokenPropertyValue.get("token").add(tokenChar);
133 tokenPropertyValue.get("token").add(substringIndex);
134 tokenPropertyValueList.add(tokenPropertyValue);
137 return Optional.of(tokenPropertyValueList);
142 return Optional.empty();