Fix the CDS calls
[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.onap.clamp.clds.tosca.JsonEditorSchemaConstants;
33 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
34 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
35 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataExecutor;
36 import org.onap.clamp.loop.service.Service;
37 import org.onap.clamp.tosca.DictionaryElement;
38 import org.onap.clamp.tosca.DictionaryService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {
44
45     @Autowired
46     private ToscaMetadataExecutor toscaMetadataExecutor;
47
48     @Autowired
49     private DictionaryService dictionaryService;
50
51     /**
52      * This method is used to start the processing of the metadata field.
53      *
54      * @param toscaElementProperty The property metadata as Json Object
55      * @return The jsonObject structure that must be added to the json schema
56      */
57     public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel) {
58         if (dictionaryService != null) {
59             return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService, serviceModel,
60                     toscaMetadataExecutor);
61         }
62         else {
63             return null;
64         }
65     }
66
67     private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
68                                                           DictionaryService dictionaryService, Service serviceModel,
69                                                           ToscaMetadataExecutor toscaMetadataExecutor) {
70         JsonObject childObject = new JsonObject();
71         if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
72                 && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
73             ((LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA)).forEach((key,
74                                                                                                        value) -> {
75                 if (key.equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
76                     if (((String) value).contains(ToscaSchemaConstants.DICTIONARY)) {
77                         processDictionaryElements((String) value, childObject, dictionaryService);
78                     }
79                     if (((String) value).contains("ClampExecution:")) {
80                         executeClampProcess(((String) value).replaceAll("ClampExecution:", ""), childObject,
81                                 serviceModel, toscaMetadataExecutor);
82                     }
83                 }
84             });
85         }
86         return childObject;
87     }
88
89     private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel,
90                                             ToscaMetadataExecutor toscaMetadataExecutor) {
91         toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel);
92     }
93
94     private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
95                                                   DictionaryService dictionaryService) {
96         String[] dictionaryKeyArray =
97                 dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
98                         dictionaryReference.length()).split("#");
99         if (dictionaryKeyArray.length > 1) {
100             // We support only one # as of now.
101             List<DictionaryElement> dictionaryElements = null;
102             if (dictionaryKeyArray.length == 2) {
103                 dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
104                         .getDictionaryElements());
105                 JsonArray subDictionaryNames = new JsonArray();
106                 new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
107                         .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));
108
109                 JsonArray jsonArray = new JsonArray();
110
111                 Optional.of(dictionaryElements).get().stream().forEach(c -> {
112                     JsonObject jsonObject = new JsonObject();
113                     jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
114                     if (c.getType() != null
115                             && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
116                         jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
117
118                     }
119                     jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
120                     jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
121                     jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
122                     jsonArray.add(jsonObject);
123                 });
124
125                 JsonObject filterObject = new JsonObject();
126                 filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
127
128                 childObject.addProperty(JsonEditorSchemaConstants.TYPE,
129                         JsonEditorSchemaConstants.TYPE_QBLDR);
130                 // TO invoke validation on such parameters
131                 childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
132                 childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
133
134             }
135         }
136         else {
137             List<DictionaryElement> dictionaryElements =
138                     new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements());
139             JsonArray dictionaryNames = new JsonArray();
140             JsonArray dictionaryFullNames = new JsonArray();
141             dictionaryElements.stream().forEach(c -> {
142                 // Json type will be translated before Policy creation
143                 if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
144                     dictionaryFullNames.add(c.getName());
145                 }
146                 dictionaryNames.add(c.getShortName());
147             });
148
149             if (dictionaryFullNames.size() > 0) {
150                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
151                 // Add Enum titles for generated translated values during JSON instance
152                 // generation
153                 JsonObject enumTitles = new JsonObject();
154                 enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
155                 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
156             }
157             else {
158                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
159             }
160         }
161     }
162
163     private static String getJsonType(String toscaType) {
164         String jsonType = null;
165         if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
166             jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
167         }
168         else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
169             jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
170         }
171         else {
172             jsonType = JsonEditorSchemaConstants.TYPE_STRING;
173         }
174         return jsonType;
175     }
176
177 }