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