Merge "correct link"
[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  * ================================================================================
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.policy.operational;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonSyntaxException;
30
31 import java.io.IOException;
32 import java.util.Map.Entry;
33
34 import org.onap.clamp.clds.util.JsonUtils;
35 import org.onap.clamp.clds.util.ResourceFileUtil;
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(JsonObject 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         return jsonSchema;
61     }
62
63     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
64             String[] enumArray) {
65         JsonObject property = new JsonObject();
66         property.addProperty("title", title);
67         property.addProperty("type", type);
68         property.addProperty("default", defaultValue);
69         property.addProperty("readOnly", readOnlyFlag);
70
71         if (enumArray != null) {
72             JsonArray jsonArray = new JsonArray();
73             property.add("enum", jsonArray);
74             for (String val : enumArray) {
75                 jsonArray.add(val);
76             }
77         }
78         return property;
79     }
80
81     private static JsonArray createVnfSchema(JsonObject modelJson) {
82         JsonArray vnfSchemaArray = new JsonArray();
83         JsonObject modelVnfs = modelJson.get("resourceDetails").getAsJsonObject().get("VF").getAsJsonObject();
84
85         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
86             JsonObject vnfOneOfSchema = new JsonObject();
87             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
88             JsonObject properties = new JsonObject();
89             properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
90             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
91                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
92
93             vnfOneOfSchema.add("properties", properties);
94             vnfSchemaArray.add(vnfOneOfSchema);
95         }
96         return vnfSchemaArray;
97     }
98
99     private static JsonArray createVfModuleSchema(JsonObject modelJson) {
100         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
101         JsonObject modelVfModules = modelJson.get("resourceDetails").getAsJsonObject().get("VFModule")
102                 .getAsJsonObject();
103
104         for (Entry<String, JsonElement> entry : modelVfModules.entrySet()) {
105             JsonObject vfModuleOneOfSchema = new JsonObject();
106             vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey());
107             JsonObject properties = new JsonObject();
108             properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null));
109             properties.add("resourceID",
110                     createSchemaProperty("Resource ID", "string",
111                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
112                             "True", null));
113             properties.add("modelInvariantId",
114                     createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", modelVfModules
115                             .get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID").getAsString(),
116                             "True", null));
117             properties.add("modelVersionId",
118                     createSchemaProperty("Model Version Id (ModelUUID)", "string",
119                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(),
120                             "True", null));
121             properties.add("modelName",
122                     createSchemaProperty("Model Name", "string",
123                             modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(),
124                             "True", null));
125             properties.add("modelVersion", createSchemaProperty("Model Version", "string",
126                     modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(),
127                     "True", null));
128             properties
129                     .add("modelCustomizationId",
130                             createSchemaProperty("Customization ID", "string", modelVfModules.get(entry.getKey())
131                                     .getAsJsonObject().get("vfModuleModelCustomizationUUID").getAsString(), "True",
132                                     null));
133
134             vfModuleOneOfSchema.add("properties", properties);
135             vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema);
136         }
137         return vfModuleOneOfSchemaArray;
138     }
139
140     private static JsonArray createAnyOfArray(JsonObject modelJson) {
141         JsonArray targetOneOfStructure = new JsonArray();
142         targetOneOfStructure.addAll(createVnfSchema(modelJson));
143         targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
144         return targetOneOfStructure;
145     }
146 }