2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
19 import com.google.gson.*;
20 import java.io.StringReader;
21 import org.yaml.snakeyaml.Yaml;
23 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
26 * Modifies a CBAM VNFD to fit ONAP
28 public class CbamVnfdBuilder {
31 * @param cbamVnfdContent the original CBAM VNFD
32 * @return the modified content CBAM VNFD
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"));
51 if (interfaces.has("Healable")) {
52 addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
54 return new Yaml().dump(new Yaml().load(new StringReader(new Gson().toJson(root))));
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");
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");
78 private JsonArray addChildArray(JsonObject root, String name) {
80 return root.get(name).getAsJsonArray();
82 JsonArray child = new JsonArray();
83 root.add(name, child);
88 private JsonObject addChild(JsonObject root, String name) {
90 return root.get(name).getAsJsonObject();
92 JsonObject child = new JsonObject();
93 root.add(name, child);