40164d756c568253a103abc34f49b30dbdc4195e
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 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.yang.mapper;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.json.JSONObject;
28 import org.junit.Test;
29 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.OdlHelloMessageCapabilitiesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.MeasurementKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata15m.entity.PerformanceDataBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.Data;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.pmdata._15m.list.output.DataBuilder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class TestYangGenSalMapping {
45
46     // Create mapper for serialization and deserialization
47     DataProviderYangToolsMapper mapper = new DataProviderYangToolsMapper();
48
49     @Test
50     public void test1() throws IOException {
51
52         // Create test object
53         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
54         netconfNodeBuilder.setConnectedMessage("ConnMessage");
55
56         LoginPasswordBuilder loginPasswordBuilder = new LoginPasswordBuilder();
57         loginPasswordBuilder.setUsername("myTestUsername");
58         loginPasswordBuilder.setPassword("myTestPassword");
59         netconfNodeBuilder.setCredentials(loginPasswordBuilder.build());
60
61         OdlHelloMessageCapabilitiesBuilder odlHelloMessageCapabilitiesBuilder =
62                 new OdlHelloMessageCapabilitiesBuilder();
63         List<Uri> uriList = new ArrayList<>();
64         uriList.add(new Uri("test.uri"));
65         odlHelloMessageCapabilitiesBuilder.setCapability(uriList);
66         netconfNodeBuilder.setOdlHelloMessageCapabilities(odlHelloMessageCapabilitiesBuilder.build());
67
68         NetconfNode netconfNode = netconfNodeBuilder.build();
69         out(netconfNode.toString());
70
71         // Map Object to JSON String
72         String res = mapper.writeValueAsString(netconfNode);
73         JSONObject json = new JSONObject(res); // Convert text to object
74         out(json.toString(4)); // Print it with specified indentation
75
76         // Map to JSON String to Object
77         NetconfNode generatedNode = mapper.readValue(res.getBytes(), NetconfNode.class);
78         out(generatedNode.toString()); // Print it with specified indentation
79         // Compare result
80         //TODO - Guilin
81         //out("Equal?  "+netconfNode.equals(generatedNode));
82     }
83
84     @Test
85     public void test3() throws IOException {
86
87         PerformanceDataBuilder performanceBuilder = new PerformanceDataBuilder();
88         performanceBuilder.setEs(99);
89         DataBuilder pmData15MinutesBuilder = new DataBuilder();
90         pmData15MinutesBuilder.setLayerProtocolName("fdsaf");
91         pmData15MinutesBuilder.setTimeStamp(new DateAndTime("2017-03-01T09:15:00.0Z"));
92         pmData15MinutesBuilder.setPerformanceData(performanceBuilder.build());
93
94         // Map Object to JSON String
95         String res = mapper.writeValueAsString(pmData15MinutesBuilder.build());
96         JSONObject json = new JSONObject(res); // Convert text to object
97         out(json.toString(4)); // Print it with specified indentation
98
99         // Map to JSON String to Object
100         Data generatedNode = mapper.readValue(res.getBytes(), Data.class);
101         out(generatedNode.toString()); // Print it with specified indentation
102     }
103
104     @Test
105     public void test4() throws IOException {
106      // @formatter:off
107      String jsonString = "{\n"
108                 + "\"node-name\": \"Sim2230\",\n"
109                 + "\"uuid-interface\": \"LP-MWPS-TTP-RADIO\",\n"
110                 + "\"layer-protocol-name\": \"MWPS\",\n"
111                 + "\"radio-signal-id\": \"Test8\",\n"
112                 + "\"time-stamp\": \"2017-03-01T09:15:00.0Z\",\n"
113                 + "\"granularity-period\": \"Period15Min\",\n"
114                 + "\"scanner-id\": \"PM_RADIO_15M_4\",\n"
115                 + "\"performance-data\": {\n"
116                 +     "\"unavailability\": 0,\n"
117                 +     "\"tx-level-max\": 3,\n"
118                 +     "\"tx-level-avg\": 3,\n"
119                 +     "\"rx-level-min\": -44,\n"
120                 +     "\"rx-level-max\": -45,\n"
121                 +     "\"rx-level-avg\": -44,\n"
122                 +     "\"time2-states\": 0,\n"
123                 +     "\"time4-states-s\": 0,\n"
124                 +     "\"time4-states\": 0,\n"
125                 +     "\"time8-states\": -1,\n"
126                 +     "\"time16-states-s\": -1,\n"
127                 +     "\"time16-states\": 0,\n"
128                 +     "\"time32-states\": -1,\n"
129                 +     "\"time64-states\": 900,\n"
130                 +     "\"time128-states\": -1,\n"
131                 +     "\"time256-states\": -1,\n"
132                 +     "\"time512-states\": -1,\n"
133                 +     "\"time512-states-l\": -1,\n"
134                 +     "\"time1024-states\": -1,\n"
135                 +     "\"time1024-states-l\": -1,\n"
136                 +     "\"time8192-states-l\": -1,\n"
137                 +     "\"time8192-states\": -1,\n"
138                 +     "\"time2048-states\": -1,\n"
139                 +     "\"snir-min\": -99,\n"
140                 +     "\"snir-max\": -99,\n"
141                 +     "\"snir-avg\": -99,\n"
142                 +     "\"xpd-min\": -99,\n"
143                 +     "\"xpd-max\": -99,\n"
144                 +     "\"xpd-avg\": -99,\n"
145                 +     "\"rf-temp-min\": -99,\n"
146                 +     "\"rf-temp-max\": -99,\n"
147                 +     "\"rf-temp-avg\": -99,\n"
148                 +     "\"defect-blocks-sum\": -1,\n"
149                 +     "\"time-period\": 900,\n"
150                 +     "\"cses\": 0,\n"
151                 +     "\"time4096-states-l\": -1,\n"
152                 +     "\"tx-level-min\": 3,\n"
153                 +     "\"es\": 0,\n"
154                 +     "\"time2048-states-l\": -1,\n"
155                 +     "\"time4096-states\": -1,\n"
156                 +     "\"ses\": 0\n"
157                 + "},\n"
158                 + "\"suspect-interval-flag\": false\n"
159                 + "}\n"
160                 + "}";
161         // @formatter:on
162         // Map to JSON String to Object
163         Data generatedNode = mapper.readValue(jsonString.getBytes(), Data.class);
164         out(generatedNode.toString()); // Print it with specified indentation
165     }
166
167     @Test
168     public void test5() throws IOException {
169         // @formatter:off
170         String jsonString = "{\n"
171                 + "    \"time-stamp\": \"2017-03-01T06:45:00.0Z\",\n"
172                 + "    \"node-name\": \"Sim2230\",\n"
173                 + "    \"uuid-interface\": \"LP-MWPS-TTP-RADIO\",\n"
174                 + "    \"scanner-id\": \"PM_RADIO_15M_14\",\n"
175                 + "    \"layer-protocol-name\": \"MWPS\",\n"
176                 + "    \"granularity-period\": \"Period15Min\",\n"
177                 + "    \"radio-signal-id\": \"Test8\",\n"
178                 + "    \"suspect-interval-flag\": false,\n"
179                 + "    \"performance-data\": {\n"
180                 + "        \"time4096-states-l\": -1,\n"
181                 + "        \"time16-states-s\": -1,\n"
182                 + "        \"tx-level-max\": 3,\n"
183                 + "        \"snir-max\": -99,\n"
184                 + "        \"time16-states\": 0,\n"
185                 + "        \"time64-states\": 900,\n"
186                 + "        \"unavailability\": 0,\n"
187                 + "        \"time8192-states-l\": -1,\n"
188                 + "        \"time512-states\": -1,\n"
189                 + "        \"xpd-min\": -99,\n"
190                 + "        \"xpd-avg\": -99,\n"
191                 + "        \"tx-level-avg\": 3,\n"
192                 + "        \"tx-level-min\": 3,\n"
193                 + "        \"rf-temp-min\": -99,\n"
194                 + "        \"rf-temp-avg\": -99,\n"
195                 + "        \"snir-avg\": -99,\n"
196                 + "        \"snir-min\": -99,\n"
197                 + "        \"time-period\": 900,\n"
198                 + "        \"time2-states\": 0,\n"
199                 + "        \"time4-states\": 0,\n"
200                 + "        \"time8-states\": -1,\n"
201                 + "        \"ses\": 0,\n"
202                 + "        \"time2048-states-l\": -1,\n"
203                 + "        \"time2048-states\": -1,\n"
204                 + "        \"xpd-max\": -99,\n"
205                 + "        \"rf-temp-max\": -99,\n"
206                 + "        \"time8192-states\": -1,\n"
207                 + "        \"time128-states\": -1,\n"
208                 + "        \"time256-states\": -1,\n"
209                 + "        \"rx-level-min\": -44,\n"
210                 + "        \"rx-level-avg\": -44,\n"
211                 + "        \"time1024-states-l\": -1,\n"
212                 + "        \"es\": 0,\n"
213                 + "        \"cses\": 0,\n"
214                 + "        \"time4-states-s\": 0,\n"
215                 + "        \"time1024-states\": -1,\n"
216                 + "        \"time512-states-l\": -1,\n"
217                 + "        \"time4096-states\": -1,\n"
218                 + "        \"rx-level-max\": -45,\n"
219                 + "        \"defect-blocks-sum\": -1,\n"
220                 + "        \"time32-states\": -1\n"
221                 + "    }\n"
222                 + "}";
223         // @formatter:on
224         // Map to JSON String to Object
225         Data generatedNode = mapper.readValue(jsonString.getBytes(), Data.class);
226         out(generatedNode.toString()); // Print it with specified indentation
227     }
228
229     @Test
230     public void test8() throws IOException {
231         out(method());
232         String input;
233         input = "id-dd-dd";
234         System.out.println("Map " + input + " to " + YangToolsMapperHelper.toCamelCaseAttributeName(input));
235         input = "idDdGg";
236         System.out.println("Map " + input + " to " + YangToolsMapperHelper.toCamelCaseAttributeName(input));
237         input = "_idDdGg";
238         System.out.println("Map " + input + " to " + YangToolsMapperHelper.toCamelCaseAttributeName(input));
239         input = "--ff--gfg";
240         System.out.println("Map " + input + " to " + YangToolsMapperHelper.toCamelCaseAttributeName(input));
241         input = "";
242         System.out.println("Map " + input + " to " + YangToolsMapperHelper.toCamelCaseAttributeName(input));
243     }
244
245     /* ---------------------------------
246      * Private
247      */
248     private static String method() {
249         String nameofCurrMethod = new Throwable().getStackTrace()[1].getMethodName();
250         return nameofCurrMethod;
251     }
252
253     private static void out(String text) {
254         System.out.println("----------------------");
255         System.out.println(text);
256     }
257
258     private static class DataProviderYangToolsMapper extends YangToolsMapper {
259
260         @SuppressWarnings("unused")
261         private final Logger LOG = LoggerFactory.getLogger(DataProviderYangToolsMapper.class);
262         private static final long serialVersionUID = 1L;
263
264         public DataProviderYangToolsMapper() {
265             super();
266             this.addDeserializer(Credentials.class, LoginPasswordBuilder.class.getName());
267             this.addKeyDeserializer(MeasurementKey.class, new IdentifierDeserializer());
268         }
269
270
271     }
272
273 }