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 vimId = addChild(extensions, "vimId");
 
  41         vimId.add("default", new JsonPrimitive("kuku"));
 
  42         JsonObject interfaces = child(substitutionMappings, "interfaces");
 
  43         JsonObject basic = addChild(interfaces, "Basic");
 
  44         addOperationParams(addChild(basic, "instantiate"));
 
  45         addOperationParams(addChild(basic, "terminate"));
 
  46         if (interfaces.has("Scalable")) {
 
  47             addOperationParams(addChild(child(interfaces, "Scalable"), "scale"));
 
  49         if (interfaces.has("Healable")) {
 
  50             addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
 
  52         return new Yaml().dump(new Yaml().load(new StringReader(new Gson().toJson(root))));
 
  55     private void addOperationParams(JsonObject operation) {
 
  56         JsonObject inputs = addChild(operation, "inputs");
 
  57         JsonObject extensions = addChild(inputs, "extensions");
 
  58         JsonArray preActions = addChildArray(extensions, "pre_actions");
 
  59         preActions.add(addAction("javascript/cbam.pre.collectConnectionPoints.js"));
 
  60         JsonArray postActions = addChildArray(extensions, "post_actions");
 
  61         postActions.add(addAction("javascript/cbam.post.collectConnectionPoints.js"));
 
  62         JsonObject additionalParameters = addChild(inputs, "additional_parameters");
 
  63         additionalParameters.addProperty("jobId", "kuku");
 
  66     private JsonElement addAction(String jsAction) {
 
  67         JsonObject action = new JsonObject();
 
  68         action.addProperty("javascript", jsAction);
 
  69         JsonArray myInclude = new JsonArray();
 
  70         myInclude.add("javascript/cbam.collectConnectionPoints.js");
 
  71         action.add("include", myInclude);
 
  72         action.addProperty("output", "operation_result");
 
  76     private JsonArray addChildArray(JsonObject root, String name) {
 
  78             return root.get(name).getAsJsonArray();
 
  80             JsonArray child = new JsonArray();
 
  81             root.add(name, child);
 
  86     private JsonObject addChild(JsonObject root, String name) {
 
  88             return root.get(name).getAsJsonObject();
 
  90             JsonObject child = new JsonObject();
 
  91             root.add(name, child);