Fix the legacy policies
[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("properties").getAsJsonObject()
56                 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
57                 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
58                 .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
59
60         // update CDS recipe and payload information to schema
61         JsonArray actors = jsonSchema.get("properties").getAsJsonObject()
62                 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
63                 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor")
64                 .getAsJsonObject().get("anyOf").getAsJsonArray();
65
66         for (JsonElement actor : actors) {
67             if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) {
68                 actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject()
69                         .get("anyOf").getAsJsonArray()
70                         .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails()));
71             }
72         }
73
74         return jsonSchema;
75     }
76
77     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
78                                                    String[] enumArray) {
79         JsonObject property = new JsonObject();
80         property.addProperty("title", title);
81         property.addProperty("type", type);
82         property.addProperty("default", defaultValue);
83         property.addProperty("readOnly", readOnlyFlag);
84
85         if (enumArray != null) {
86             JsonArray jsonArray = new JsonArray();
87             property.add("enum", jsonArray);
88             for (String val : enumArray) {
89                 jsonArray.add(val);
90             }
91         }
92         return property;
93     }
94
95     private static JsonArray createVnfSchema(Service modelService) {
96         JsonArray vnfSchemaArray = new JsonArray();
97         JsonObject modelVnfs = modelService.getResourceByType("VF");
98
99         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
100             JsonObject vnfOneOfSchema = new JsonObject();
101             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
102             JsonObject properties = new JsonObject();
103             properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
104             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
105                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
106
107             vnfOneOfSchema.add("properties", properties);
108             vnfSchemaArray.add(vnfOneOfSchema);
109         }
110         return vnfSchemaArray;
111     }
112
113     private static JsonArray createVfModuleSchema(Service modelService) {
114         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
115         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
116
117         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
118             JsonObject vfModuleOneOfSchema = new JsonObject();
119             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
120             JsonObject properties = new JsonObject();
121             properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
122             properties.add("resourceID",
123                     createSchemaProperty("Resource ID", "string",
124                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
125                             "True", null));
126             properties.add("modelInvariantId",
127                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string",
128                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID")
129                                     .getAsString(),
130                             "True", null));
131             properties.add("modelVersionId",
132                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
133                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
134                             "True", null));
135             properties.add("modelName",
136                     createSchemaProperty("Model Name", "string",
137                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
138                             "True", null));
139             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
140                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
141                     "True", null));
142             properties
143                     .add("modelCustomizationId",
144                             createSchemaProperty("Customization ID", "string",
145                                     modelVfModules.get(entry.getKey()).getAsJsonObject()
146                                             .get("vfModuleModelCustomizationUUID").getAsString(), "True",
147                                     null));
148
149             vfModuleOneOfSchema.add("properties", properties);
150             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
151         }
152         return vfModuleOneOfSchemaArray;
153     }
154
155     private static JsonArray createAnyOfArray(Service modelJson) {
156         JsonArray targetOneOfStructure = new JsonArray();
157         targetOneOfStructure.addAll(createVnfSchema(modelJson));
158         targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
159         return targetOneOfStructure;
160     }
161
162     private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) {
163         JsonArray anyOfStructure = new JsonArray();
164         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF")));
165         anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF")));
166         return anyOfStructure;
167     }
168
169     private static JsonArray createAnyOfCdsRecipe(JsonObject jsonObject) {
170         JsonArray schemaArray = new JsonArray();
171         for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
172             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
173                     .getAsJsonObject("controllerProperties");
174
175             if (controllerProperties != null) {
176                 JsonObject workflows = controllerProperties.getAsJsonObject("workflows");
177                 for (Entry<String, JsonElement> workflowsEntry : workflows.entrySet()) {
178                     JsonObject obj = new JsonObject();
179                     obj.addProperty("title", workflowsEntry.getKey());
180                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
181                             controllerProperties));
182                     schemaArray.add(obj);
183                 }
184
185             }
186         }
187         return schemaArray;
188     }
189
190     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
191         JsonObject type = new JsonObject();
192         type.addProperty("title", "Payload (YAML)");
193         type.addProperty("type", "string");
194         type.addProperty("default", createDefaultStringForPayload(workFlow, controllerProperties));
195         type.addProperty("format", "textarea");
196         JsonObject properties = new JsonObject();
197         properties.add("type", type);
198         return properties;
199     }
200
201     private static String createDefaultStringForPayload(JsonObject workFlow, JsonObject controllerProperties) {
202         String artifactName = controllerProperties.get("sdnc_model_name").toString();
203         String artifactVersion = controllerProperties.get("sdnc_model_version").toString();
204         String data = workFlow.getAsJsonObject("inputs").toString();
205         StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n")
206                 .append("artifact_version : ").append(artifactVersion).append("\n")
207                 .append("mode : async").append("\n")
208                 .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'")
209                 .append("'");
210         return builder.toString();
211     }
212 }