Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / parser / metadata / ToscaMetadataParserWithDictionarySupport.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update.parser.metadata;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Optional;
32 import org.json.JSONArray;
33 import org.onap.clamp.clds.tosca.JsonEditorSchemaConstants;
34 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
35 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
36 import org.onap.clamp.tosca.DictionaryElement;
37 import org.onap.clamp.tosca.DictionaryService;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {
43
44     @Autowired
45     private DictionaryService dictionaryService;
46
47     /**
48      * This method is used to start the processing of the metadata field.
49      *
50      * @param toscaElementProperty The property metadata as Json Object
51      * @return The jsonObject structure that must be added to the json schema
52      */
53     public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty) {
54         if (dictionaryService != null) {
55             return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService);
56         }
57         else {
58             return null;
59         }
60     }
61
62     private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
63                                                           DictionaryService dictionaryService) {
64         JsonObject childObject = new JsonObject();
65         if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
66                 && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
67             LinkedHashMap<String, Object> metadataMap =
68                     (LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA);
69             if (metadataMap != null) {
70                 metadataMap.entrySet().stream().forEach(constraint -> {
71                     if (constraint.getKey()
72                             .equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
73                         JSONArray validValuesArray = new JSONArray();
74                         if (constraint.getValue() instanceof ArrayList<?>) {
75                             boolean processDictionary = ((ArrayList<?>) constraint.getValue())
76                                     .stream().anyMatch(value -> (value instanceof String
77                                             && ((String) value).contains(ToscaSchemaConstants.DICTIONARY)));
78                             if (processDictionary) {
79                                 ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
80                                     if ((value instanceof String && ((String) value)
81                                             .contains(ToscaSchemaConstants.DICTIONARY))) {
82                                         processDictionaryElements((String) value, childObject, dictionaryService);
83                                     }
84
85                                 });
86                             }
87                         }
88                     }
89                 });
90             }
91         }
92         return childObject;
93     }
94
95     private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
96                                                   DictionaryService dictionaryService) {
97         String[] dictionaryKeyArray =
98                 dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
99                         dictionaryReference.length()).split("#");
100         if (dictionaryKeyArray.length > 1) {
101             // We support only one # as of now.
102             List<DictionaryElement> dictionaryElements = null;
103             if (dictionaryKeyArray.length == 2) {
104                 dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
105                         .getDictionaryElements());
106                 JsonArray subDictionaryNames = new JsonArray();
107                 new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
108                         .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));
109
110                 JsonArray jsonArray = new JsonArray();
111
112                 Optional.of(dictionaryElements).get().stream().forEach(c -> {
113                     JsonObject jsonObject = new JsonObject();
114                     jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
115                     if (c.getType() != null
116                             && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
117                         jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
118
119                     }
120                     jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
121                     jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
122                     jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
123                     jsonArray.add(jsonObject);
124                 });
125
126                 JsonObject filterObject = new JsonObject();
127                 filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
128
129                 childObject.addProperty(JsonEditorSchemaConstants.TYPE,
130                         JsonEditorSchemaConstants.TYPE_QBLDR);
131                 // TO invoke validation on such parameters
132                 childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
133                 childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
134
135             }
136         }
137         else {
138             List<DictionaryElement> dictionaryElements =
139                     new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements());
140             JsonArray dictionaryNames = new JsonArray();
141             JsonArray dictionaryFullNames = new JsonArray();
142             dictionaryElements.stream().forEach(c -> {
143                 // Json type will be translated before Policy creation
144                 if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
145                     dictionaryFullNames.add(c.getName());
146                 }
147                 dictionaryNames.add(c.getShortName());
148             });
149
150             if (dictionaryFullNames.size() > 0) {
151                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
152                 // Add Enum titles for generated translated values during JSON instance
153                 // generation
154                 JsonObject enumTitles = new JsonObject();
155                 enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
156                 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
157             }
158             else {
159                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
160             }
161         }
162     }
163
164     private static String getJsonType(String toscaType) {
165         String jsonType = null;
166         if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
167             jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
168         }
169         else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
170             jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
171         }
172         else {
173             jsonType = JsonEditorSchemaConstants.TYPE_STRING;
174         }
175         return jsonType;
176     }
177
178 }