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.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;
25 import java.io.IOException;
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
30 * Modifies a CBAM VNFD to fit ONAP
32 public class CbamVnfdBuilder {
35 * @param cbamVnfdContent the original CBAM VNFD
36 * @return the modified content CBAM VNFD
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"));
53 if (interfaces.has("Healable")) {
54 addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
56 JsonNode jsonNodeTree = new ObjectMapper().readTree(new GsonBuilder().setPrettyPrinting().create().toJson(root));
57 return new YAMLMapper().writeValueAsString(jsonNodeTree);
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");
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");
81 private JsonArray addChildArray(JsonObject root, String name) {
83 return root.get(name).getAsJsonArray();
85 JsonArray child = new JsonArray();
86 root.add(name, child);
91 private JsonObject addChild(JsonObject root, String name) {
93 return root.get(name).getAsJsonObject();
95 JsonObject child = new JsonObject();
96 root.add(name, child);