f50969ed68c3417336abff9704b78f1a7065151b
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data;
23
24 import java.io.File;
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;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
36
37 /**
38  * 
39  * @author Michael Dürre
40  *
41  */
42 public class DataContainer {
43
44         private final Release release;
45         private final Date created;
46         private final Map<ComponentName, ComponentData> components;
47         private final Map<ConfigName, ConfigData> configs;
48
49         public Release getRelease() {
50                 return this.release;
51         }
52
53         public boolean isCurrentRelease() {
54                 return this.release.equals(Release.CURRENT_RELEASE);
55         }
56
57         public Date getCreated() {
58                 return this.created;
59         }
60
61         public Map<ComponentName, ComponentData> getComponents() {
62                 return this.components;
63         }
64
65         public Map<ConfigName, ConfigData> getConfigs() {
66                 return this.configs;
67         }
68
69         public DataContainer() {
70                 this(Release.CURRENT_RELEASE);
71         }
72
73         public DataContainer(Release release) {
74                 this(release, new Date());
75         }
76
77         public DataContainer(Release release, Date dt) {
78                 this.release = release;
79                 this.created = dt;
80                 this.components = new HashMap<>();
81                 this.configs = new HashMap<>();
82         }
83
84         public void addComponent(ComponentName name, ComponentData data) {
85                 this.components.put(name, data);
86         }
87
88         public void addConfig(ConfigName name, ConfigData data) {
89                 this.configs.put(name, data);
90         }
91
92         public static DataContainer load(File file) throws Exception {
93                 if(!file.exists()) {
94                         throw new FileNotFoundException();
95                 }
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");
101
102                 String k;
103                 ComponentName compKey;
104                 JSONArray compData;
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));
111                 }
112                 ConfigName confKey;
113                 ConfigData confData;
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);
120                 }
121                 return c;
122         }
123
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());
132                 }
133                 for (Entry<ConfigName, ConfigData> entry : this.configs.entrySet()) {
134                         confsJson.put(entry.getKey().getValue(), entry.getValue().toString());
135                 }
136                 o.put("components", compsJson);
137                 o.put("configs", confsJson);
138                 return o.toString();
139         }
140
141 }