aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/main/java/org/onap/policy/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java
blob: 4e55263fbc55aa13441be7b09e90ed920fc329e7 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 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.onap.policy.clamp.clds.tosca.update.parser.metadata;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import org.onap.policy.clamp.clds.tosca.JsonEditorSchemaConstants;
import org.onap.policy.clamp.clds.tosca.ToscaSchemaConstants;
import org.onap.policy.clamp.clds.tosca.update.elements.ToscaElementProperty;
import org.onap.policy.clamp.clds.tosca.update.execution.ToscaMetadataExecutor;
import org.onap.policy.clamp.loop.service.Service;
import org.onap.policy.clamp.tosca.DictionaryElement;
import org.onap.policy.clamp.tosca.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {

    @Autowired
    private ToscaMetadataExecutor toscaMetadataExecutor;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * This method is used to start the processing of the metadata field.
     *
     * @param toscaElementProperty The property metadata as Json Object
     * @return The jsonObject structure that must be added to the json schema
     */
    public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel) {
        if (dictionaryService != null) {
            return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService, serviceModel,
                    toscaMetadataExecutor);
        } else {
            return null;
        }
    }

    private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
                                                          DictionaryService dictionaryService, Service serviceModel,
                                                          ToscaMetadataExecutor toscaMetadataExecutor) {
        JsonObject childObject = new JsonObject();
        if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
                && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
            ((LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA)).forEach((key,
                                                                                                       value) -> {
                if (key.equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
                    String[] multipleValues = ((String) value).split(",");
                    for (String multipleValue : multipleValues) {
                        if (multipleValue.contains(ToscaSchemaConstants.DICTIONARY)) {
                            processDictionaryElements(multipleValue, childObject, dictionaryService);
                        }
                        if (multipleValue.contains("ClampExecution:")) {
                            executeClampProcess(multipleValue.replaceFirst("ClampExecution:", ""), childObject,
                                    serviceModel, toscaMetadataExecutor);
                        }
                    }

                }
            });
        }
        return childObject;
    }

    private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel,
                                            ToscaMetadataExecutor toscaMetadataExecutor) {
        toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel);
    }

    /**
     * For dictionary with multiple levels (defined by #).
     *
     * @param dictionaryKeyArray the array containing the different elements
     * @param childObject        the structure getting the new entries
     * @param dictionaryService  the dictionary service bean
     */
    private static void processComplexDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
                                                         DictionaryService dictionaryService) {
        // We support only one # as of now.
        List<DictionaryElement> dictionaryElements = null;
        if (dictionaryKeyArray.length == 2) {
            dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
                    .getDictionaryElements());
            JsonArray subDictionaryNames = new JsonArray();
            new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
                    .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));

            JsonArray jsonArray = new JsonArray();

            Optional.of(dictionaryElements).get().forEach(c -> {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
                if (c.getType() != null
                        && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
                    jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);

                }
                jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
                jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
                jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
                jsonArray.add(jsonObject);
            });

            JsonObject filterObject = new JsonObject();
            filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);

            childObject.addProperty(JsonEditorSchemaConstants.TYPE,
                    JsonEditorSchemaConstants.TYPE_QBLDR);
            // TO invoke validation on such parameters
            childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
            childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);

        }
    }

    /**
     * For dictionary with single entry.
     *
     * @param dictionaryKeyArray the array containing the different elements
     * @param childObject        the structure getting the new entries
     * @param dictionaryService  the dictionary service bean
     */
    private static void processSimpleDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
                                                        DictionaryService dictionaryService) {
        JsonArray dictionaryNames = new JsonArray();
        JsonArray dictionaryFullNames = new JsonArray();
        dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements().forEach(c -> {
            // Json type will be translated before Policy creation
            if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
                dictionaryFullNames.add(c.getName());
            }
            dictionaryNames.add(c.getShortName());
        });

        if (dictionaryFullNames.size() > 0) {
            if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
                childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryFullNames);
            } else {
                childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
            }
            // Add Enum titles for generated translated values during JSON instance
            // generation
            JsonObject enumTitles = new JsonObject();
            enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
            if (childObject.get(JsonEditorSchemaConstants.OPTIONS) != null) {
                childObject.get(JsonEditorSchemaConstants.OPTIONS).getAsJsonArray().add(enumTitles);
            } else {
                childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
            }

        } else {
            if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
                childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryNames);
            } else {
                childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
            }
        }
    }

    private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
                                                  DictionaryService dictionaryService) {
        String[] dictionaryKeyArray =
                dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
                        dictionaryReference.length()).split("#");
        if (dictionaryKeyArray.length > 1) {
            processComplexDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
        } else {
            processSimpleDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
        }
    }

    private static String getJsonType(String toscaType) {
        String jsonType = null;
        if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
            jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
        } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
            jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
        } else {
            jsonType = JsonEditorSchemaConstants.TYPE_STRING;
        }
        return jsonType;
    }

}