Updating Nokia driver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / CbamVnfdBuilder.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
18
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
22 import com.google.gson.*;
23 import org.yaml.snakeyaml.Yaml;
24
25 import java.io.IOException;
26
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
28
29 /**
30  * Modifies a CBAM VNFD to fit ONAP
31  */
32 public class CbamVnfdBuilder {
33
34     /**
35      * @param cbamVnfdContent the original CBAM VNFD
36      * @return the modified content CBAM VNFD
37      */
38     public String build(String cbamVnfdContent) throws IOException {
39         JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfdContent)).getAsJsonObject();
40         JsonObject substitution_mappings = child(child(root, "topology_template"), "substitution_mappings");
41         JsonObject extensions = addChild(addChild(addChild(addChild(addChild(substitution_mappings, "capabilities"), "vnf"), "properties"), "modifiable_attributes"), "extensions");
42         JsonObject onapCsarId = addChild(extensions, "onapCsarId");
43         onapCsarId.add("default", new JsonPrimitive("kuku"));
44         JsonObject vimId = addChild(extensions, "vimId");
45         vimId.add("default", new JsonPrimitive("kuku"));
46         JsonObject interfaces = child(substitution_mappings, "interfaces");
47         JsonObject basic = addChild(interfaces, "Basic");
48         addOperationParams(addChild(basic, "instantiate"));
49         addOperationParams(addChild(basic, "terminate"));
50         if (interfaces.has("Scalable")) {
51             addOperationParams(addChild(child(interfaces, "Scalable"), "scale"));
52         }
53         if (interfaces.has("Healable")) {
54             addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
55         }
56         JsonNode jsonNodeTree = new ObjectMapper().readTree(new GsonBuilder().setPrettyPrinting().create().toJson(root));
57         return new YAMLMapper().writeValueAsString(jsonNodeTree);
58     }
59
60     private void addOperationParams(JsonObject operation) {
61         JsonObject inputs = addChild(operation, "inputs");
62         JsonObject extensions = addChild(inputs, "extensions");
63         JsonArray pre_actions = addChildArray(extensions, "pre_actions");
64         pre_actions.add(addAction("javascript/cbam.pre.collectConnectionPoints.js"));
65         JsonArray post_actions = addChildArray(extensions, "post_actions");
66         post_actions.add(addAction("javascript/cbam.post.collectConnectionPoints.js"));
67         JsonObject additional_parameters = addChild(inputs, "additional_parameters");
68         additional_parameters.addProperty("jobId", "kuku");
69     }
70
71     private JsonElement addAction(String jsAction) {
72         JsonObject action = new JsonObject();
73         action.addProperty("javascript", jsAction);
74         JsonArray myInclude = new JsonArray();
75         myInclude.add("javascript/cbam.collectConnectionPoints.js");
76         action.add("include", myInclude);
77         action.addProperty("output", "operation_result");
78         return action;
79     }
80
81     private JsonArray addChildArray(JsonObject root, String name) {
82         if (root.has(name)) {
83             return root.get(name).getAsJsonArray();
84         } else {
85             JsonArray child = new JsonArray();
86             root.add(name, child);
87             return child;
88         }
89     }
90
91     private JsonObject addChild(JsonObject root, String name) {
92         if (root.has(name)) {
93             return root.get(name).getAsJsonObject();
94         } else {
95             JsonObject child = new JsonObject();
96             root.add(name, child);
97             return child;
98         }
99     }
100 }