f7f247a05b05d3f17ce2fba7cb8471a842fb00bb
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / OnapAbstractVnfdBuilder.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import java.util.Map;
25 import java.util.NoSuchElementException;
26 import java.util.Set;
27 import java.util.regex.Pattern;
28 import org.slf4j.Logger;
29 import org.yaml.snakeyaml.Yaml;
30
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
33 import static org.slf4j.LoggerFactory.getLogger;
34
35 /**
36  * Transforms a CBAM package into an ONAP package
37  */
38 public class OnapAbstractVnfdBuilder {
39     public static final String DESCRIPTION = "description";
40     public static final String PROPERTIES = "properties";
41     public static final String REQUIREMENTS = "requirements";
42     private static Logger logger = getLogger(OnapAbstractVnfdBuilder.class);
43
44     @VisibleForTesting
45     static String indent(String content, int prefixSize) {
46         StringBuilder sb = new StringBuilder();
47         for (int i = 0; i < prefixSize; i++) {
48             sb.append("  ");
49         }
50         Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
51         return pattern.matcher(content).replaceAll(sb.toString() + "$1");
52     }
53
54     protected static String getRequirement(JsonArray requirements, String key) {
55         for (int i = 0; i < requirements.size(); i++) {
56             JsonElement requirement = requirements.get(i);
57             Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
58             String s = next.getKey();
59             if (key.equals(s)) {
60                 return next.getValue().getAsString();
61             }
62         }
63         return null;
64     }
65
66     private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
67         for (Map.Entry<String, JsonElement> node : nodes) {
68             if (name.equals(node.getKey())) {
69                 return node.getValue();
70             }
71         }
72         throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
73     }
74 }