summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/ContrailTranslationHelper.java
blob: c36d8c66fa003dd72eecd1ea500398530fed0145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.translator.services.heattotosca.helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.sdc.heat.datatypes.HeatBoolean;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
import org.openecomp.sdc.translator.datatypes.heattotosca.PropertyRegexMatcher;
import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
import org.openecomp.sdc.translator.services.heattotosca.NameExtractor;

public class ContrailTranslationHelper {

    /**
     * Gets compute node type id.
     *
     * @param contrailServiceTemplateResource     contrail service teamplte resource
     * @param contrailServiceTemplateResourceId   contrailservice template resource id
     * @param contrailServiceTemplateTranslatedId contrail service tempalte resource translated id
     * @return the compute node type id
     */
    public String getComputeNodeTypeId(Resource contrailServiceTemplateResource, String contrailServiceTemplateResourceId,
                                       String contrailServiceTemplateTranslatedId, TranslationContext context) {
        NameExtractor nodeTypeNameExtractor = context.getNameExtractorImpl(ConfigConstants.CONTRAIL_COMPUTE_NODE_TYPE_IMPL_KEY);
        return nodeTypeNameExtractor
            .extractNodeTypeName(contrailServiceTemplateResource, contrailServiceTemplateResourceId, contrailServiceTemplateTranslatedId);
    }

    /**
     * Get property Regx matcher list.
     *
     * @return Regex exprission per contrail service template resource property, while contail compute type name is consider when setting the name
     * value
     */
    public List<PropertyRegexMatcher> getPropertyRegexMatchersForComputeNodeType() {
        List<PropertyRegexMatcher> propertyRegexMatchers = new ArrayList<>();
        propertyRegexMatchers.add(new PropertyRegexMatcher("image_name", Collections.singletonList(".+_image_name$"), "_image_name"));
        propertyRegexMatchers.add(new PropertyRegexMatcher("flavor", Collections.singletonList(".+_flavor_name$"), "_flavor_name"));
        return propertyRegexMatchers;
    }

    public String getSubstitutionContrailServiceTemplateMetadata(String heatFileName, String serviceInstanceTranslatedId) {
        return FileUtils.getFileWithoutExtention(heatFileName) + "_" + serviceInstanceTranslatedId;
    }

    /**
     * Translate fn split function optional.
     *
     * @param propertyValue       the property value
     * @param listSize            the list size
     * @param includeBooleanValue the include boolean value
     * @return the optional
     */
    public Optional<List<Map<String, List>>> translateFnSplitFunction(Object propertyValue, int listSize, boolean includeBooleanValue) {
        List<Map<String, List>> tokenPropertyValueList = new ArrayList<>();
        if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
            Map<String, Object> propMap = (Map) propertyValue;
            Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
            Object entity = entry.getValue();
            String key = entry.getKey();
            String tokenChar;
            if (key.equals("Fn::Split") && entity instanceof List) {
                tokenChar = (String) ((List) entity).get(0);
                Object refParameter = ((List) entity).get(1);
                for (int substringIndex = 0; substringIndex < listSize; substringIndex++) {
                    Map<String, List> tokenPropertyValue = new HashMap<>();
                    tokenPropertyValue.put("token", new ArrayList<>());
                    if (refParameter instanceof Map && ((Map) refParameter).get("Ref") != null) {
                        Map<String, String> stringWithToken = new HashMap<>();
                        ((Map) stringWithToken).put(ToscaFunctions.GET_INPUT.getFunctionName(), ((Map) refParameter).get("Ref"));
                        tokenPropertyValue.get("token").add(stringWithToken);
                    } else if (refParameter instanceof String) {
                        if (includeBooleanValue) {
                            StringBuilder booleanBuilder = new StringBuilder();
                            String[] booleanValueList = ((String) refParameter).split(tokenChar);
                            for (int i = 0; i < booleanValueList.length; i++) {
                                if (i == 0) {
                                    booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
                                } else {
                                    booleanBuilder.append(tokenChar);
                                    booleanBuilder.append(HeatBoolean.eval(booleanValueList[i]));
                                }
                            }
                            tokenPropertyValue.get("token").add(booleanBuilder.toString());
                        } else {
                            tokenPropertyValue.get("token").add(refParameter);
                        }
                    }
                    tokenPropertyValue.get("token").add(tokenChar);
                    tokenPropertyValue.get("token").add(substringIndex);
                    tokenPropertyValueList.add(tokenPropertyValue);
                }
                return Optional.of(tokenPropertyValueList);
            }
        }
        return Optional.empty();
    }
}