Support functions in TOSCA Simple Profile in YAML
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / helper / ContrailTranslationHelper.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.helper;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29
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;
38
39 public class ContrailTranslationHelper {
40   /**
41    * Gets compute node type id.
42    *
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
47    */
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);
57   }
58
59   /**
60    * Get property Regx matcher list.
61    *
62    * @return Regex exprission per contrail service template resource property, while contail compute
63    *         type name is consider when setting the name value
64    */
65   public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
66     List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
67     propertyRegexMatchers
68         .add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"),
69             "_image_name"));
70     propertyRegexMatchers
71         .add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"),
72             "_flavor_name"));
73     return propertyRegexMatchers;
74   }
75
76   public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName,
77                                                                String serviceInstanceTranslatedId) {
78     return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
79   }
80
81   /**
82    * Translate fn split function optional.
83    *
84    * @param propertyValue       the property value
85    * @param listSize            the list size
86    * @param includeBooleanValue the include boolean value
87    * @return the optional
88    */
89   public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue,
90                                                                     int listSize,
91                                                                     boolean
92                                                                         includeBooleanValue) {
93     List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
94
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();
100       String tokenChar;
101
102       if (key.equals("Fn::Split") && entity instanceof List) {
103         tokenChar = (String) ((List) entity).get(0);
104         Object refParameter = ((List) entity).get(1);
105
106         for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
107           Map<String, List> tokenPropertyValue = new HashMap<>();
108           tokenPropertyValue.put("token", new ArrayList<>());
109
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++) {
120                 if (i == 0) {
121                   booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
122                 } else {
123                   booleanBuilder.append(tokenChar);
124                   booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
125                 }
126               }
127               tokenPropertyValue.get("token").add(booleanBuilder.toString());
128             } else {
129               tokenPropertyValue.get("token").add(refParameter);
130             }
131           }
132           tokenPropertyValue.get("token").add(tokenChar);
133           tokenPropertyValue.get("token").add(substringIndex);
134           tokenPropertyValueList.add(tokenPropertyValue);
135         }
136
137         return Optional.of(tokenPropertyValueList);
138
139       }
140     }
141
142     return Optional.empty();
143   }
144 }