cf9e010b3ec256b7d778f83affa75a8d4ae40fb4
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / execution / cds / ToscaMetadataCdsProcess.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.execution.cds;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import java.util.Map;
30 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
31 import org.onap.clamp.loop.service.Service;
32 import org.onap.clamp.tosca.DictionaryService;
33 import org.springframework.beans.factory.annotation.Autowired;
34
35 /**
36  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
37  */
38 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
39
40     @Autowired
41     private DictionaryService dictionaryService;
42
43     @Override
44     public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
45         switch (parameters) {
46             case "payload":
47                 childObject.add("anyOf", generatePayload(serviceModel));
48                 break;
49             case "operation":
50                 childObject.add("enum", generateOperation(serviceModel));
51                 break;
52         }
53     }
54
55     private static JsonArray generatePayload(Service serviceModel) {
56         JsonArray schemaAnyOf = new JsonArray();
57         schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
58         schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
59         return schemaAnyOf;
60     }
61
62     private static JsonArray generateOperation(Service serviceModel) {
63         JsonArray schemaEnum = new JsonArray();
64         schemaEnum.addAll(generateOperationPerResource("VF", serviceModel));
65         schemaEnum.addAll(generateOperationPerResource("PNF", serviceModel));
66         return schemaEnum;
67     }
68
69     private static JsonArray generateOperationPerResource(String resourceName, Service serviceModel) {
70         JsonArray schemaEnum = new JsonArray();
71         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
72                 .entrySet()) {
73             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
74                     .getAsJsonObject("controllerProperties");
75             if (controllerProperties != null) {
76                 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
77                     schemaEnum.add(workflowsEntry);
78                 }
79             }
80         }
81         return schemaEnum;
82     }
83
84     private static JsonArray generatePayloadPerResource(String resourceName, Service serviceModel) {
85         JsonArray schemaAnyOf = new JsonArray();
86         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
87                 .entrySet()) {
88             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
89                     .getAsJsonObject("controllerProperties");
90             if (controllerProperties != null) {
91                 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
92                         .entrySet()) {
93                     JsonObject obj = new JsonObject();
94                     obj.addProperty("title", workflowsEntry.getKey());
95                     obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
96                             controllerProperties));
97                     schemaAnyOf.add(obj);
98                 }
99             }
100         }
101         return schemaAnyOf;
102     }
103
104     private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
105         JsonObject payloadResult = new JsonObject();
106
107         payloadResult.addProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString());
108         payloadResult.addProperty("artifact_version", controllerProperties.get("sdnc_model_version").getAsString());
109         payloadResult.addProperty("mode", "async");
110         payloadResult.add("data", workFlow.getAsJsonObject("inputs"));
111         return payloadResult;
112     }
113 }