ec2ad27c4cf586963192b6d500a15c9abfb5896a
[vfc/nfvo/driver/vnfm/svnfm.git] /
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.google.gson.*;
20 import java.io.StringReader;
21 import org.yaml.snakeyaml.Yaml;
22
23 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
24
25 /**
26  * Modifies a CBAM VNFD to fit ONAP
27  */
28 public class CbamVnfdBuilder {
29
30     /**
31      * @param cbamVnfdContent the original CBAM VNFD
32      * @return the modified content CBAM VNFD
33      */
34     public String build(String cbamVnfdContent) {
35         JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfdContent)).getAsJsonObject();
36         JsonObject substitutionMappings = child(child(root, "topology_template"), "substitution_mappings");
37         JsonObject extensions = addChild(addChild(addChild(addChild(addChild(substitutionMappings, "capabilities"), "vnf"), "properties"), "modifiable_attributes"), "extensions");
38         JsonObject onapCsarId = addChild(extensions, "onapCsarId");
39         onapCsarId.add("default", new JsonPrimitive("kuku"));
40         JsonObject externalVnfmId = addChild(extensions, "externalVnfmId");
41         externalVnfmId.add("default", new JsonPrimitive("kuku"));
42         JsonObject vimId = addChild(extensions, "vimId");
43         vimId.add("default", new JsonPrimitive("kuku"));
44         JsonObject interfaces = child(substitutionMappings, "interfaces");
45         JsonObject basic = addChild(interfaces, "Basic");
46         addOperationParams(addChild(basic, "instantiate"));
47         addOperationParams(addChild(basic, "terminate"));
48         if (interfaces.has("Scalable")) {
49             addOperationParams(addChild(child(interfaces, "Scalable"), "scale"));
50         }
51         if (interfaces.has("Healable")) {
52             addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
53         }
54         return new Yaml().dump(new Yaml().load(new StringReader(new Gson().toJson(root))));
55     }
56
57     private void addOperationParams(JsonObject operation) {
58         JsonObject inputs = addChild(operation, "inputs");
59         JsonObject extensions = addChild(inputs, "extensions");
60         JsonArray preActions = addChildArray(extensions, "pre_actions");
61         preActions.add(addAction("javascript/cbam.pre.collectConnectionPoints.js"));
62         JsonArray postActions = addChildArray(extensions, "post_actions");
63         postActions.add(addAction("javascript/cbam.post.collectConnectionPoints.js"));
64         JsonObject additionalParameters = addChild(inputs, "additional_parameters");
65         additionalParameters.addProperty("jobId", "kuku");
66     }
67
68     private JsonElement addAction(String jsAction) {
69         JsonObject action = new JsonObject();
70         action.addProperty("javascript", jsAction);
71         JsonArray myInclude = new JsonArray();
72         myInclude.add("javascript/cbam.collectConnectionPoints.js");
73         action.add("include", myInclude);
74         action.addProperty("output", "operation_result");
75         return action;
76     }
77
78     private JsonArray addChildArray(JsonObject root, String name) {
79         if (root.has(name)) {
80             return root.get(name).getAsJsonArray();
81         } else {
82             JsonArray child = new JsonArray();
83             root.add(name, child);
84             return child;
85         }
86     }
87
88     private JsonObject addChild(JsonObject root, String name) {
89         if (root.has(name)) {
90             return root.get(name).getAsJsonObject();
91         } else {
92             JsonObject child = new JsonObject();
93             root.add(name, child);
94             return child;
95         }
96     }
97 }