d194a70e97423fdfb073074aced8aa1d65027d41
[sdc.git] /
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 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;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39
40 public class ContrailTranslationHelper {
41
42   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
43   /**
44    * Gets compute node type id.
45    *
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
50    */
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);
58
59     mdcDataDebugMessage.debugExitMessage(null, null);
60     return nodeTypeNameExtractor
61         .extractNodeTypeName(contrailServiceTemplateResource, contrailServiceTemplateResourceId,
62             contrailServiceTemplateTranslatedId);
63   }
64
65   /**
66    * Get property Regx matcher list.
67    *
68    * @return Regex exprission per contrail service template resource property, while contail compute
69    *         type name is consider when setting the name value
70    */
71   public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
72     List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
73     propertyRegexMatchers
74         .add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"),
75             "_image_name"));
76     propertyRegexMatchers
77         .add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"),
78             "_flavor_name"));
79     return propertyRegexMatchers;
80   }
81
82   public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName,
83                                                                String serviceInstanceTranslatedId) {
84
85
86     mdcDataDebugMessage.debugEntryMessage(null, null);
87
88     mdcDataDebugMessage.debugExitMessage(null, null);
89     return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
90   }
91
92   /**
93    * Translate fn split function optional.
94    *
95    * @param propertyValue       the property value
96    * @param listSize            the list size
97    * @param includeBooleanValue the include boolean value
98    * @return the optional
99    */
100   public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue,
101                                                                     int listSize,
102                                                                     boolean
103                                                                         includeBooleanValue) {
104     List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
105
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();
111       String tokenChar;
112
113       if (key.equals("Fn::Split") && entity instanceof List) {
114         tokenChar = (String) ((List) entity).get(0);
115         Object refParameter = ((List) entity).get(1);
116
117         for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
118           Map<String, List> tokenPropertyValue = new HashMap<>();
119           tokenPropertyValue.put("token", new ArrayList<>());
120
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++) {
131                 if (i == 0) {
132                   booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
133                 } else {
134                   booleanBuilder.append(tokenChar);
135                   booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
136                 }
137               }
138               tokenPropertyValue.get("token").add(booleanBuilder.toString());
139             } else {
140               tokenPropertyValue.get("token").add(refParameter);
141             }
142           }
143           tokenPropertyValue.get("token").add(tokenChar);
144           tokenPropertyValue.get("token").add(substringIndex);
145           tokenPropertyValueList.add(tokenPropertyValue);
146         }
147
148         return Optional.of(tokenPropertyValueList);
149
150       }
151     }
152
153     return Optional.empty();
154   }
155 }