Add feature to Support NSMF based TN slices
[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     private SliceTaskInfo<SliceProfileAdapter> anNFSliceTaskInfo = new SliceTaskInfo<>();
71
72     /**
73      * change T t to {@link Map}
74      *
75      * @param t input
76      * @param <T> Object
77      * @return {@link Map}
78      */
79     private <T> Map<String, Object> bean2Map(T t) {
80         Map<String, Object> resMap = new HashMap<>();
81         if (t == null) {
82             return resMap;
83         }
84
85         try {
86             Field[] fields = t.getClass().getDeclaredFields();
87             for (Field field : fields) {
88                 String name = field.getName();
89                 String key = name;
90                 if (name == null || "".equals(name) || "serialVersionUID".equalsIgnoreCase(name)) {
91                     continue;
92                 }
93                 JsonProperty annotation = field.getAnnotation(JsonProperty.class);
94                 if (annotation != null && !annotation.value().equals(JsonProperty.USE_DEFAULT_NAME)) {
95                     key = annotation.value();
96                 }
97
98                 Method method = t.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
99                 Object value = method.invoke(t);
100                 resMap.put(key, value);
101             }
102
103         } catch (Exception e) {
104             e.printStackTrace();
105         }
106         return resMap;
107     }
108
109     /**
110      * replace of slice profile
111      *
112      * @param paramMap params map
113      * @param header starts of key
114      * @return Map
115      */
116     private Map<String, Object> replaceHeader(Map<String, String> paramMap, String header) {
117         Map<String, Object> sliceProfileMap = new HashMap<>();
118         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
119             if (entry.getKey().startsWith(header)) {
120                 sliceProfileMap.put(entry.getKey().replaceFirst("^" + header, ""), entry.getValue());
121             }
122         }
123         return sliceProfileMap;
124     }
125 }
126