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