Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicyRepresentationBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.policy.operational;
26
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonSyntaxException;
31 import java.io.IOException;
32 import java.util.Map.Entry;
33 import org.onap.clamp.clds.util.JsonUtils;
34 import org.onap.clamp.clds.util.ResourceFileUtil;
35 import org.onap.clamp.loop.service.Service;
36
37 public class OperationalPolicyRepresentationBuilder {
38
39     /**
40      * This method generates the operational policy json representation that will be
41      * used by ui for rendering. It uses the model (VF and VFModule) defined in the
42      * loop object to do so, so it's dynamic. It also uses the operational policy
43      * schema template defined in the resource folder.
44      *
45      * @param modelJson The loop model json
46      * @return The json representation
47      * @throws JsonSyntaxException If the schema template cannot be parsed
48      * @throws IOException         In case of issue when opening the schema template
49      */
50     public static JsonObject generateOperationalPolicySchema(Service modelJson)
51             throws JsonSyntaxException, IOException {
52         JsonObject jsonSchema = JsonUtils.GSON.fromJson(
53                 ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
54                 JsonObject.class);
55         jsonSchema.get("schema").getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject()
56                 .get("configurationsJson").getAsJsonObject().get("properties").getAsJsonObject()
57                 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
58                 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
59                 .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
60
61         // update CDS recipe and payload information to schema
62         JsonArray actors = jsonSchema.get("schema").getAsJsonObject().get("items").getAsJsonObject().get("properties")
63                 .getAsJsonObject().get("configurationsJson").getAsJsonObject().get("properties").getAsJsonObject()
64                 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
65                 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
66                 .getAsJsonObject().get("anyOf").getAsJsonArray();
67
68         for (JsonElement actor : actors) {
69             if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
70                 actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject()
71                         .get("anyOf").getAsJsonArray()
72                         .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails()));
73             }
74         }
75
76         return jsonSchema;
77     }
78
79     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
80                                                    String[] enumArray) {
81         JsonObject property = new JsonObject();
82         property.addProperty("title", title);
83         property.addProperty("type", type);
84         property.addProperty("default", defaultValue);
85         property.addProperty("readOnly", readOnlyFlag);
86
87         if (enumArray != null) {
88             JsonArray jsonArray = new JsonArray();
89             property.add("enum", jsonArray);
90             for (String val : enumArray) {
91                 jsonArray.add(val);
92             }
93         }
94         return property;
95     }
96
97     private static JsonArray createVnfSchema(Service modelService) {
98         JsonArray vnfSchemaArray = new JsonArray();
99         JsonObject modelVnfs = modelService.getResourceByType("VF");
100
101         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
102             JsonObject vnfOneOfSchema = new JsonObject();
103             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
104             JsonObject properties = new JsonObject();
105             properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
106             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
107                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
108
109             vnfOneOfSchema.add("properties", properties);
110             vnfSchemaArray.add(vnfOneOfSchema);
111         }
112         return vnfSchemaArray;
113     }
114
115     private static JsonArray createVfModuleSchema(Service modelService) {
116         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
117         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
118
119         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
120             JsonObject vfModuleOneOfSchema = new JsonObject();
121             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
122             JsonObject properties = new JsonObject();
123             properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
124             properties.add("resourceID",
125                     createSchemaProperty("Resource ID", "string",
126                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
127                             "True", null));
128             properties.add("modelInvariantId",
129                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
130                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
131                                     .getAsString(),
132                             "True", null));
133             properties.add("modelVersionId",
134                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
135                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
136                             "True", null));
137             properties.add("modelName",
138                     createSchemaProperty("Model Name", "string",
139                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
140                             "True", null));
141             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
142                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
143                     "True", null));
144             properties
145                     .add("modelCustomizationId",
146                             createSchemaProperty("Customization ID", "string",
147                                     modelVfModules.get(entry.getKey()).getAsJsonObject()
148                                             .get("vfModuleModelCustomizationUUID").getAsString(), "True",
149                                     null));
150
151             vfModuleOneOfSchema.add("properties", properties);
152             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
153         }
154         return vfModuleOneOfSchemaArray;
155     }
156
157     private static JsonArray createAnyOfArray(Service modelJson) {
158         JsonArray targetOneOfStructure = new JsonArray();
159         targetOneOfStructure.addAll(createVnfSchema(modelJson));
160         targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
161         return targetOneOfStructure;
162     }
163
164     private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) {
165         JsonArray anyOfStructure = new JsonArray();
166         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF")));
167         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF")));
168         return anyOfStructure;
169     }
170
171     private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
172         JsonArray schemaArray = new JsonArray();
173         for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
174             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
175                     .getAsJsonObject("controllerProperties");
176
177             if (controllerProperties != null) {
178                 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
179                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
180                     JsonObject obj = new JsonObject();
181                     obj.addProperty("title", workflowsEntry.getKey());
182                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
183                             controllerProperties));
184                     schemaArray.add(obj);
185                 }
186
187             }
188         }
189         return schemaArray;
190     }
191
192     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
193         JsonObject type = new JsonObject();
194         type.addProperty("title", "Payload (YAML)");
195         type.addProperty("type", "string");
196         type.addProperty("default", createDefaultStringForPayload(workFlow, controllerProperties));
197         type.addProperty("format", "textarea");
198         JsonObject properties = new JsonObject();
199         properties.add("type", type);
200         return properties;
201     }
202
203     private static String createDefaultStringForPayload(JsonObject workFlow, JsonObject controllerProperties) {
204         String artifactName = controllerProperties.get("sdnc_model_name").toString();
205         String artifactVersion = controllerProperties.get("sdnc_model_version").toString();
206         String data = workFlow.getAsJsonObject("inputs").toString();
207         StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n")
208                 .append("artifact_version : ").append(artifactVersion).append("\n")
209                 .append("mode : async").append("\n")
210                 .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'")
211                 .append("'");
212         return builder.toString();
213     }
214 }