2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data;
25 import java.io.FileNotFoundException;
26 import java.nio.file.Files;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.HashMap;
31 import java.util.Map.Entry;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
39 * @author Michael Dürre
42 public class DataContainer {
44 private final Release release;
45 private final Date created;
46 private final Map<ComponentName, ComponentData> components;
47 private final Map<ConfigName, ConfigData> configs;
49 public Release getRelease() {
53 public boolean isCurrentRelease() {
54 return this.release.equals(Release.CURRENT_RELEASE);
57 public Date getCreated() {
61 public Map<ComponentName, ComponentData> getComponents() {
62 return this.components;
65 public Map<ConfigName, ConfigData> getConfigs() {
69 public DataContainer() {
70 this(Release.CURRENT_RELEASE);
73 public DataContainer(Release release) {
74 this(release, new Date());
77 public DataContainer(Release release, Date dt) {
78 this.release = release;
80 this.components = new HashMap<>();
81 this.configs = new HashMap<>();
84 public void addComponent(ComponentName name, ComponentData data) {
85 this.components.put(name, data);
88 public void addConfig(ConfigName name, ConfigData data) {
89 this.configs.put(name, data);
92 public static DataContainer load(File file) throws Exception {
94 throw new FileNotFoundException();
96 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
97 JSONObject o = new JSONObject(new String(Files.readAllBytes(file.toPath())));
98 DataContainer c = new DataContainer(Release.getValueOf(o.getString("release")),
99 format.parse(o.getString("created")));
100 JSONObject comps = o.getJSONObject("components");
103 ComponentName compKey;
105 for (Object key : comps.keySet()) {
106 k = String.valueOf(key);
107 // check component if exists
108 compKey = ComponentName.getValueOf(k);
109 compData = comps.getJSONArray(k);
110 c.addComponent(compKey, new ComponentData(compKey, compData));
114 JSONObject confs = o.getJSONObject("configs");
115 for (Object key : confs.keySet()) {
116 k = String.valueOf(key);
117 confKey = ConfigName.getValueOf(k);
118 confData = new ConfigData(confs.getString(k));
119 c.addConfig(confKey, confData);
124 public String toJSON() {
125 JSONObject o = new JSONObject();
126 o.put("release", this.release.getValue());
127 o.put("created", NetconfTimeStampImpl.getConverter().getTimeStampAsNetconfString(this.created));
128 JSONObject compsJson = new JSONObject();
129 JSONObject confsJson = new JSONObject();
130 for (Entry<ComponentName, ComponentData> entry : this.components.entrySet()) {
131 compsJson.put(entry.getKey().getValue(), entry.getValue().toJsonArray());
133 for (Entry<ConfigName, ConfigData> entry : this.configs.entrySet()) {
134 confsJson.put(entry.getKey().getValue(), entry.getValue().toString());
136 o.put("components", compsJson);
137 o.put("configs", confsJson);