bfd4627b472ee1ffc397ff94a1a7053be62e9676
[so.git] / common / src / main / java / org / onap / so / beans / nsmf / SliceTaskParamsAdapter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
6  #
7  # Licensed under the Apache License, Version 2.0 (the "License")
8  # you may not use this file except in compliance with the License.
9  # You may obtain a copy of the License at
10  #
11  #       http://www.apache.org/licenses/LICENSE-2.0
12  #
13  # Unless required by applicable law or agreed to in writing, software
14  # distributed under the License is distributed on an "AS IS" BASIS,
15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  # See the License for the specific language governing permissions and
17  # limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.beans.nsmf;
21
22 import com.fasterxml.jackson.annotation.JsonProperty;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.JsonObject;
25 import lombok.*;
26 import org.onap.so.beans.nsmf.oof.TemplateInfo;
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Method;
30 import java.util.HashMap;
31 import java.io.Serializable;
32 import java.util.Map;
33
34
35 @Data
36 @NoArgsConstructor
37 @AllArgsConstructor
38 @ToString
39 @Builder
40 public class SliceTaskParamsAdapter implements Serializable {
41
42     private static final long serialVersionUID = -7785578865170503301L;
43
44     private String serviceId;
45
46     private String serviceName;
47
48     private String nstId;
49
50     private String nstName;
51
52     private Map<String, Object> serviceProfile = new HashMap<>();
53
54     private String suggestNsiId;
55
56     private String suggestNsiName;
57
58     private TemplateInfo NSTInfo = new TemplateInfo();
59
60     private SliceTaskInfo<SliceProfileAdapter> tnBHSliceTaskInfo = new SliceTaskInfo<>();
61
62     private SliceTaskInfo<SliceProfileAdapter> tnMHSliceTaskInfo = new SliceTaskInfo<>();
63
64     private SliceTaskInfo<SliceProfileAdapter> tnFHSliceTaskInfo = new SliceTaskInfo<>();
65
66     private SliceTaskInfo<SliceProfileAdapter> cnSliceTaskInfo = new SliceTaskInfo<>();
67
68     private SliceTaskInfo<SliceProfileAdapter> anSliceTaskInfo = new SliceTaskInfo<>();
69
70     @SuppressWarnings("unchecked")
71     public void convertFromJson(String jsonString) throws IOException {
72         ObjectMapper mapper = new ObjectMapper();
73         Map<String, String> paramMap = (Map<String, String>) mapper.readValue(jsonString, Map.class);
74
75         this.setServiceName(paramMap.get("ServiceName"));
76         this.setServiceId(paramMap.get("ServiceId"));
77         this.setNstId(paramMap.get("NSTId"));
78         this.setNstName(paramMap.get("NSTName"));
79         this.setSuggestNsiId(paramMap.get("suggestNSIId"));
80         this.setSuggestNsiName(paramMap.get("suggestNSIName"));
81
82         this.setServiceProfile(replaceHeader(paramMap, "ServiceProfile."));
83
84         SliceProfileAdapter tnBHSliceProfile = mapper.readValue(
85                 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.BH.")), SliceProfileAdapter.class);
86         this.tnBHSliceTaskInfo.setSliceProfile(tnBHSliceProfile);
87
88         SliceProfileAdapter tnMHSliceProfile = mapper.readValue(
89                 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.MH.")), SliceProfileAdapter.class);
90         this.tnMHSliceTaskInfo.setSliceProfile(tnMHSliceProfile);
91
92         SliceProfileAdapter tnFHSliceProfile = mapper.readValue(
93                 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.FH.")), SliceProfileAdapter.class);
94         this.tnFHSliceTaskInfo.setSliceProfile(tnFHSliceProfile);
95
96         SliceProfileAdapter cnSliceProfile = mapper.readValue(
97                 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.CN.")), SliceProfileAdapter.class);
98         this.cnSliceTaskInfo.setSliceProfile(cnSliceProfile);
99
100         SliceProfileAdapter anSliceProfile = mapper.readValue(
101                 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.AN.")), SliceProfileAdapter.class);
102         this.anSliceTaskInfo.setSliceProfile(anSliceProfile);
103
104         this.tnBHSliceTaskInfo.setSuggestNssiId(paramMap.get("TN.BH.SuggestNSSIId"));
105         this.tnBHSliceTaskInfo.setSuggestNssiName(paramMap.get("TN.BH.SuggestNSSIName"));
106         this.tnBHSliceTaskInfo.setProgress(paramMap.get("TN.BH.progress"));
107         this.tnBHSliceTaskInfo.setStatus(paramMap.get("TN.BH.status"));
108         this.tnBHSliceTaskInfo.setStatusDescription(paramMap.get("TN.BH.statusDescription"));
109         this.tnBHSliceTaskInfo.setScriptName(paramMap.get("TN.BH.ScriptName"));
110
111         this.tnMHSliceTaskInfo.setSuggestNssiId(paramMap.get("TN.MH.SuggestNSSIId"));
112         this.tnMHSliceTaskInfo.setSuggestNssiName(paramMap.get("TN.MH.SuggestNSSIName"));
113         this.tnMHSliceTaskInfo.setProgress(paramMap.get("TN.MH.progress"));
114         this.tnMHSliceTaskInfo.setStatus(paramMap.get("TN.MH.status"));
115         this.tnMHSliceTaskInfo.setStatusDescription(paramMap.get("TN.MH.statusDescription"));
116         this.tnMHSliceTaskInfo.setScriptName(paramMap.get("TN.MH.ScriptName"));
117
118         this.tnFHSliceTaskInfo.setSuggestNssiId(paramMap.get("TN.FH.SuggestNSSIId"));
119         this.tnFHSliceTaskInfo.setSuggestNssiName(paramMap.get("TN.FH.SuggestNSSIName"));
120         this.tnFHSliceTaskInfo.setProgress(paramMap.get("TN.FH.progress"));
121         this.tnFHSliceTaskInfo.setStatus(paramMap.get("TN.FH.status"));
122         this.tnFHSliceTaskInfo.setStatusDescription(paramMap.get("TN.FH.statusDescription"));
123         this.tnFHSliceTaskInfo.setScriptName(paramMap.get("TN.FH.ScriptName"));
124
125         this.cnSliceTaskInfo.setSuggestNssiId(paramMap.get("CN.SuggestNSSIId"));
126         this.cnSliceTaskInfo.setSuggestNssiName(paramMap.get("CN.SuggestNSSIName"));
127         this.cnSliceTaskInfo.setProgress(paramMap.get("CN.progress"));
128         this.cnSliceTaskInfo.setStatus(paramMap.get("CN.status"));
129         this.cnSliceTaskInfo.setStatusDescription(paramMap.get("CN.statusDescription"));
130         this.cnSliceTaskInfo.setScriptName(paramMap.get("CN.ScriptName"));
131
132         this.anSliceTaskInfo.setSuggestNssiId(paramMap.get("AN.SuggestNSSIId"));
133         this.anSliceTaskInfo.setSuggestNssiName(paramMap.get("AN.SuggestNSSIName"));
134         this.anSliceTaskInfo.setProgress(paramMap.get("AN.progress"));
135         this.anSliceTaskInfo.setStatus(paramMap.get("AN.status"));
136         this.anSliceTaskInfo.setStatusDescription(paramMap.get("AN.statusDescription"));
137         this.anSliceTaskInfo.setScriptName(paramMap.get("AN.ScriptName"));
138     }
139
140     public String convertToJson() {
141         JsonObject jsonObject = new JsonObject();
142         jsonObject.addProperty("ServiceId", serviceId);
143         jsonObject.addProperty("ServiceName", serviceName);
144         jsonObject.addProperty("NSTId", nstId);
145         jsonObject.addProperty("NSTName", nstName);
146         jsonObject.addProperty("suggestNSIId", suggestNsiId);
147         jsonObject.addProperty("suggestNSIName", suggestNsiName);
148
149         for (Map.Entry<String, Object> entry : serviceProfile.entrySet()) {
150             jsonObject.addProperty("ServiceProfile." + entry.getKey(), entry.getValue().toString());
151         }
152         Map<String, Object> sliceProfileAn = bean2Map(anSliceTaskInfo.getSliceProfile());
153         for (Map.Entry<String, Object> entry : sliceProfileAn.entrySet()) {
154             String value = entry.getValue() == null ? "" : entry.getValue().toString();
155             jsonObject.addProperty("SliceProfile.AN." + entry.getKey(), value);
156         }
157         Map<String, Object> sliceProfileCn = bean2Map(cnSliceTaskInfo.getSliceProfile());
158         for (Map.Entry<String, Object> entry : sliceProfileCn.entrySet()) {
159             String value = entry.getValue() == null ? "" : entry.getValue().toString();
160             jsonObject.addProperty("SliceProfile.CN." + entry.getKey(), value);
161         }
162         Map<String, Object> sliceProfileTnBH = bean2Map(tnBHSliceTaskInfo.getSliceProfile());
163         for (Map.Entry<String, Object> entry : sliceProfileTnBH.entrySet()) {
164             String value = entry.getValue() == null ? "" : entry.getValue().toString();
165             jsonObject.addProperty("SliceProfile.TN.BH." + entry.getKey(), value);
166         }
167         Map<String, Object> sliceProfileTnMH = bean2Map(tnMHSliceTaskInfo.getSliceProfile());
168         for (Map.Entry<String, Object> entry : sliceProfileTnMH.entrySet()) {
169             String value = entry.getValue() == null ? "" : entry.getValue().toString();
170             jsonObject.addProperty("SliceProfile.TN.MH." + entry.getKey(), value);
171         }
172         Map<String, Object> sliceProfileTnFH = bean2Map(tnFHSliceTaskInfo.getSliceProfile());
173         for (Map.Entry<String, Object> entry : sliceProfileTnFH.entrySet()) {
174             String value = entry.getValue() == null ? "" : entry.getValue().toString();
175             jsonObject.addProperty("SliceProfile.TN.FH." + entry.getKey(), value);
176         }
177         jsonObject.addProperty("TN.BH.SuggestNSSIId", tnBHSliceTaskInfo.getSuggestNssiId());
178         jsonObject.addProperty("TN.BH.SuggestNSSIName", tnBHSliceTaskInfo.getSuggestNssiName());
179         jsonObject.addProperty("TN.BH.progress", tnBHSliceTaskInfo.getProgress());
180         jsonObject.addProperty("TN.BH.status", tnBHSliceTaskInfo.getStatus());
181         jsonObject.addProperty("TN.BH.statusDescription", tnBHSliceTaskInfo.getStatusDescription());
182         jsonObject.addProperty("TN.BH.ScriptName", tnBHSliceTaskInfo.getScriptName());
183
184         jsonObject.addProperty("TN.MH.SuggestNSSIId", tnMHSliceTaskInfo.getSuggestNssiId());
185         jsonObject.addProperty("TN.MH.SuggestNSSIName", tnMHSliceTaskInfo.getSuggestNssiName());
186         jsonObject.addProperty("TN.MH.progress", tnMHSliceTaskInfo.getProgress());
187         jsonObject.addProperty("TN.MH.status", tnMHSliceTaskInfo.getStatus());
188         jsonObject.addProperty("TN.MH.statusDescription", tnMHSliceTaskInfo.getStatusDescription());
189         jsonObject.addProperty("TN.MH.ScriptName", tnMHSliceTaskInfo.getScriptName());
190
191         jsonObject.addProperty("TN.FH.SuggestNSSIId", tnFHSliceTaskInfo.getSuggestNssiId());
192         jsonObject.addProperty("TN.FH.SuggestNSSIName", tnFHSliceTaskInfo.getSuggestNssiName());
193         jsonObject.addProperty("TN.FH.progress", tnFHSliceTaskInfo.getProgress());
194         jsonObject.addProperty("TN.FH.status", tnFHSliceTaskInfo.getStatus());
195         jsonObject.addProperty("TN.FH.statusDescription", tnFHSliceTaskInfo.getStatusDescription());
196         jsonObject.addProperty("TN.FH.ScriptName", tnFHSliceTaskInfo.getScriptName());
197
198         jsonObject.addProperty("CN.SuggestNSSIId", cnSliceTaskInfo.getSuggestNssiId());
199         jsonObject.addProperty("CN.SuggestNSSIName", cnSliceTaskInfo.getSuggestNssiName());
200         jsonObject.addProperty("CN.progress", cnSliceTaskInfo.getProgress());
201         jsonObject.addProperty("CN.status", cnSliceTaskInfo.getStatus());
202         jsonObject.addProperty("CN.statusDescription", cnSliceTaskInfo.getStatusDescription());
203         jsonObject.addProperty("CN.ScriptName", cnSliceTaskInfo.getScriptName());
204
205         jsonObject.addProperty("AN.SuggestNSSIId", anSliceTaskInfo.getSuggestNssiId());
206         jsonObject.addProperty("AN.SuggestNSSIName", anSliceTaskInfo.getSuggestNssiName());
207         jsonObject.addProperty("AN.progress", anSliceTaskInfo.getProgress());
208         jsonObject.addProperty("AN.status", anSliceTaskInfo.getStatus());
209         jsonObject.addProperty("AN.statusDescription", anSliceTaskInfo.getStatusDescription());
210         jsonObject.addProperty("AN.ScriptName", anSliceTaskInfo.getScriptName());
211
212         return jsonObject.toString();
213     }
214
215     /**
216      * change T t to {@link Map}
217      *
218      * @param t input
219      * @param <T> Object
220      * @return {@link Map}
221      */
222     private <T> Map<String, Object> bean2Map(T t) {
223         Map<String, Object> resMap = new HashMap<>();
224         if (t == null) {
225             return resMap;
226         }
227
228         try {
229             Field[] fields = t.getClass().getDeclaredFields();
230             for (Field field : fields) {
231                 String name = field.getName();
232                 String key = name;
233                 if (name == null || "".equals(name) || "serialVersionUID".equalsIgnoreCase(name)) {
234                     continue;
235                 }
236                 JsonProperty annotation = field.getAnnotation(JsonProperty.class);
237                 if (annotation != null && !annotation.value().equals(JsonProperty.USE_DEFAULT_NAME)) {
238                     key = annotation.value();
239                 }
240
241                 Method method = t.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
242                 Object value = method.invoke(t);
243                 resMap.put(key, value);
244             }
245
246         } catch (Exception e) {
247             e.printStackTrace();
248         }
249         return resMap;
250     }
251
252     /**
253      * replace of slice profile
254      *
255      * @param paramMap params map
256      * @param header starts of key
257      * @return Map
258      */
259     private Map<String, Object> replaceHeader(Map<String, String> paramMap, String header) {
260         Map<String, Object> sliceProfileMap = new HashMap<>();
261         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
262             if (entry.getKey().startsWith(header)) {
263                 sliceProfileMap.put(entry.getKey().replaceFirst("^" + header, ""), entry.getValue());
264             }
265         }
266         return sliceProfileMap;
267     }
268 }