Merge "Create Service object"
[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 import org.onap.clamp.loop.service.Service;
37
38 public class OperationalPolicyRepresentationBuilder {
39
40     /**
41      * This method generates the operational policy json representation that will be
42      * used by ui for rendering. It uses the model (VF and VFModule) defined in the
43      * loop object to do so, so it's dynamic. It also uses the operational policy
44      * schema template defined in the resource folder.
45      * 
46      * @param modelJson The loop model json
47      * @return The json representation
48      * @throws JsonSyntaxException If the schema template cannot be parsed
49      * @throws IOException         In case of issue when opening the schema template
50      */
51     public static JsonObject generateOperationalPolicySchema(Service modelJson)
52             throws JsonSyntaxException, IOException {
53         JsonObject jsonSchema = JsonUtils.GSON.fromJson(
54                 ResourceFileUtil.getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"),
55                 JsonObject.class);
56         jsonSchema.get("schema").getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject()
57                 .get("configurationsJson").getAsJsonObject().get("properties").getAsJsonObject()
58                 .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies")
59                 .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target")
60                 .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson));
61         return jsonSchema;
62     }
63
64     private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag,
65             String[] enumArray) {
66         JsonObject property = new JsonObject();
67         property.addProperty("title", title);
68         property.addProperty("type", type);
69         property.addProperty("default", defaultValue);
70         property.addProperty("readOnly", readOnlyFlag);
71
72         if (enumArray != null) {
73             JsonArray jsonArray = new JsonArray();
74             property.add("enum", jsonArray);
75             for (String val : enumArray) {
76                 jsonArray.add(val);
77             }
78         }
79         return property;
80     }
81
82     private static JsonArray createVnfSchema(Service modelService) {
83         JsonArray vnfSchemaArray = new JsonArray();
84         JsonObject modelVnfs = modelService.getResourceByType("VF");
85
86         for (Entry<String, JsonElement> entry : modelVnfs.entrySet()) {
87             JsonObject vnfOneOfSchema = new JsonObject();
88             vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey());
89             JsonObject properties = new JsonObject();
90             properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null));
91             properties.add("resourceID", createSchemaProperty("Resource ID", "string",
92                     modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null));
93
94             vnfOneOfSchema.add("properties", properties);
95             vnfSchemaArray.add(vnfOneOfSchema);
96         }
97         return vnfSchemaArray;
98     }
99
100     private static JsonArray createVfModuleSchema(Service modelService) {
101         JsonArray vfModuleOneOfSchemaArray = new JsonArray();
102         JsonObject modelVfModules = modelService.getResourceByType("VFModule");
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(Service modelJson) {
141         JsonArray targetOneOfStructure = new JsonArray();
142         targetOneOfStructure.addAll(createVnfSchema(modelJson));
143         targetOneOfStructure.addAll(createVfModuleSchema(modelJson));
144         return targetOneOfStructure;
145     }
146 }