1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* Copyright 2016-2017, Nokia Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.google.gson.*;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
/**
* Modifies a CBAM VNFD to fit ONAP
*/
public class CbamVnfdBuilder {
/**
* @param cbamVnfdContent the original CBAM VNFD
* @return the modified content CBAM VNFD
*/
public String build(String cbamVnfdContent) throws IOException {
JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfdContent)).getAsJsonObject();
JsonObject substitutionMappings = child(child(root, "topology_template"), "substitution_mappings");
JsonObject extensions = addChild(addChild(addChild(addChild(addChild(substitutionMappings, "capabilities"), "vnf"), "properties"), "modifiable_attributes"), "extensions");
JsonObject onapCsarId = addChild(extensions, "onapCsarId");
onapCsarId.add("default", new JsonPrimitive("kuku"));
JsonObject vimId = addChild(extensions, "vimId");
vimId.add("default", new JsonPrimitive("kuku"));
JsonObject interfaces = child(substitutionMappings, "interfaces");
JsonObject basic = addChild(interfaces, "Basic");
addOperationParams(addChild(basic, "instantiate"));
addOperationParams(addChild(basic, "terminate"));
if (interfaces.has("Scalable")) {
addOperationParams(addChild(child(interfaces, "Scalable"), "scale"));
}
if (interfaces.has("Healable")) {
addOperationParams(addChild(child(interfaces, "Healable"), "heal"));
}
JsonNode jsonNodeTree = new ObjectMapper().readTree(new GsonBuilder().setPrettyPrinting().create().toJson(root));
return new YAMLMapper().writeValueAsString(jsonNodeTree);
}
private void addOperationParams(JsonObject operation) {
JsonObject inputs = addChild(operation, "inputs");
JsonObject extensions = addChild(inputs, "extensions");
JsonArray preActions = addChildArray(extensions, "pre_actions");
preActions.add(addAction("javascript/cbam.pre.collectConnectionPoints.js"));
JsonArray postActions = addChildArray(extensions, "post_actions");
postActions.add(addAction("javascript/cbam.post.collectConnectionPoints.js"));
JsonObject additionalParameters = addChild(inputs, "additional_parameters");
additionalParameters.addProperty("jobId", "kuku");
}
private JsonElement addAction(String jsAction) {
JsonObject action = new JsonObject();
action.addProperty("javascript", jsAction);
JsonArray myInclude = new JsonArray();
myInclude.add("javascript/cbam.collectConnectionPoints.js");
action.add("include", myInclude);
action.addProperty("output", "operation_result");
return action;
}
private JsonArray addChildArray(JsonObject root, String name) {
if (root.has(name)) {
return root.get(name).getAsJsonArray();
} else {
JsonArray child = new JsonArray();
root.add(name, child);
return child;
}
}
private JsonObject addChild(JsonObject root, String name) {
if (root.has(name)) {
return root.get(name).getAsJsonObject();
} else {
JsonObject child = new JsonObject();
root.add(name, child);
return child;
}
}
}
|