f7aef6b6d533413d09c901e60e2dc0c0357f5579
[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.netconfnodestateservice.test;
23
24 import com.google.gson.stream.JsonWriter;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import org.junit.Test;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotification;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotificationBuilder;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
33 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
34 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
35 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class TestDom {
42
43     private static final Logger LOG = LoggerFactory.getLogger(TestDom.class);
44
45     @Test
46     public void test2() {
47         FaultNotification faultNotification =
48                 new FaultNotificationBuilder().setCounter(1).setNodeId("Node1").setSeverity(SeverityType.Major).build();
49
50 //        final NormalizedNode<?, ?> data = DomContext.getBINDING_CONTEXT()
51 //                .toNormalizedNode(InstanceIdentifier.create(FaultNotification.class), faultNotification).getValue();
52
53 //        LOG.info("Normalized node: {}", data);
54 //        final String json = toJson(data, schemaContext);
55 //        LOG.info(json);
56
57     }
58     /**
59      * Serialization of {@link NormalizedNode} into {@link String}.
60      *
61      * @param node          to be serialized data
62      * @param schemaContext schema context
63      * @return serialized data
64      */
65     private static String toJson(final NormalizedNode<?, ?> node, final EffectiveModelContext schemaContext) {
66         final JSONCodecFactory codecFactory = JSONCodecFactorySupplier.RFC7951.createSimple(schemaContext);
67         try (StringWriter stringWriter = new StringWriter();
68              JsonWriter jsonWriter = new JsonWriter(stringWriter);
69              NormalizedNodeWriter nodeStreamer = NormalizedNodeWriter.forStreamWriter(
70                      JSONNormalizedNodeStreamWriter.createNestedWriter(codecFactory, SchemaPath.ROOT,
71                              schemaContext.getQName().getNamespace(), jsonWriter))) {
72             jsonWriter.beginObject();
73             nodeStreamer.write(node);
74             jsonWriter.endObject();
75             return stringWriter.toString();
76         } catch (IOException e) {
77             throw new IllegalStateException("Failed to convert input node to JSON: " + node, e);
78         }
79     }
80 }