2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.beans.nsmf;
22 import com.fasterxml.jackson.annotation.JsonProperty;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.JsonObject;
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;
40 public class SliceTaskParamsAdapter implements Serializable {
42 private static final long serialVersionUID = -7785578865170503301L;
44 private String serviceId;
46 private String serviceName;
50 private String nstName;
52 private Map<String, Object> serviceProfile = new HashMap<>();
54 private String suggestNsiId;
56 private String suggestNsiName;
58 private TemplateInfo NSTInfo = new TemplateInfo();
60 private SliceTaskInfo<SliceProfileAdapter> tnBHSliceTaskInfo = new SliceTaskInfo<>();
62 private SliceTaskInfo<SliceProfileAdapter> tnMHSliceTaskInfo = new SliceTaskInfo<>();
64 private SliceTaskInfo<SliceProfileAdapter> tnFHSliceTaskInfo = new SliceTaskInfo<>();
66 private SliceTaskInfo<SliceProfileAdapter> cnSliceTaskInfo = new SliceTaskInfo<>();
68 private SliceTaskInfo<SliceProfileAdapter> anSliceTaskInfo = new SliceTaskInfo<>();
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);
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"));
82 this.setServiceProfile(replaceHeader(paramMap, "ServiceProfile."));
84 SliceProfileAdapter tnBHSliceProfile = mapper.readValue(
85 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.BH.")), SliceProfileAdapter.class);
86 this.tnBHSliceTaskInfo.setSliceProfile(tnBHSliceProfile);
88 SliceProfileAdapter tnMHSliceProfile = mapper.readValue(
89 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.MH.")), SliceProfileAdapter.class);
90 this.tnMHSliceTaskInfo.setSliceProfile(tnMHSliceProfile);
92 SliceProfileAdapter tnFHSliceProfile = mapper.readValue(
93 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.TN.FH.")), SliceProfileAdapter.class);
94 this.tnFHSliceTaskInfo.setSliceProfile(tnFHSliceProfile);
96 SliceProfileAdapter cnSliceProfile = mapper.readValue(
97 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.CN.")), SliceProfileAdapter.class);
98 this.cnSliceTaskInfo.setSliceProfile(cnSliceProfile);
100 SliceProfileAdapter anSliceProfile = mapper.readValue(
101 mapper.writeValueAsString(replaceHeader(paramMap, "SliceProfile.AN.")), SliceProfileAdapter.class);
102 this.anSliceTaskInfo.setSliceProfile(anSliceProfile);
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"));
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"));
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"));
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"));
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"));
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);
149 for (Map.Entry<String, Object> entry : serviceProfile.entrySet()) {
150 jsonObject.addProperty("ServiceProfile." + entry.getKey(), entry.getValue().toString());
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);
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);
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);
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);
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);
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());
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());
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());
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());
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());
212 return jsonObject.toString();
216 * change T t to {@link Map}
220 * @return {@link Map}
222 private <T> Map<String, Object> bean2Map(T t) {
223 Map<String, Object> resMap = new HashMap<>();
229 Field[] fields = t.getClass().getDeclaredFields();
230 for (Field field : fields) {
231 String name = field.getName();
233 if (name == null || "".equals(name) || "serialVersionUID".equalsIgnoreCase(name)) {
236 JsonProperty annotation = field.getAnnotation(JsonProperty.class);
237 if (annotation != null && !annotation.value().equals(JsonProperty.USE_DEFAULT_NAME)) {
238 key = annotation.value();
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);
246 } catch (Exception e) {
253 * replace of slice profile
255 * @param paramMap params map
256 * @param header starts of key
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());
266 return sliceProfileMap;