caf7d9c1b61dc08d445bfee8905057a19c0a0b0e
[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.example;
23
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.Objects;
27 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.DomContext;
28 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomParser;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.Configuration;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.common.XMLNamespace;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
38 import org.opendaylight.yangtools.yang.parser.api.YangParser;
39 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class ExampleConfig {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ExampleConfig.class);
47
48     // specification of YANG module
49     private static final QNameModule CONFIG_MODULE = QNameModule.create(
50             XMLNamespace.of("urn:ietf:params:xml:ns:yang:config"), Revision.of("2020-12-08"));
51     // path to 'configuration' container (it is a root container)
52     private static final YangInstanceIdentifier CONFIGURATION_PATH = YangInstanceIdentifier.builder()
53             .node(QName.create(CONFIG_MODULE, "configuration"))
54             .build();
55
56
57     public static void exampleConfig(DomContext domContext) throws YangParserException, IOException {
58         // (1) preparation of schema context with module that describes configuration (it is possible to load multiple
59         // schemas into parser)
60         //final YangParser parser = new YangParserFactoryImpl().createParser();
61         final YangParser parser = domContext.getYangParserFactory().createParser();
62         parser.addSource(YangTextSchemaSource.forResource("/META-INF/yang/config@2020-12-08.yang"));
63         final EffectiveModelContext schemaContext = parser.buildEffectiveModel();
64
65         // (2) parsing of configuration into binding-independent format
66         final NormalizedNode data = DomParser.parseJsonFile("/example.json", schemaContext);
67
68         // (3) conversion into binding-aware format (md-sal codec needs to know about path on which data is placed)
69         final Configuration config = (Configuration) domContext.getBindingNormalizedNodeSerializer().fromNormalizedNode(CONFIGURATION_PATH, data)
70                 .getValue();
71
72         // (4) printing some useful information
73         LOG.info("Value of 'config1': {}", config.getConfig1());
74         LOG.info("Value of 'config2': {}", config.requireConfig2());
75         Objects.requireNonNull(config.getEntry()).forEach((entryKey, entry) ->
76                 LOG.info("Value of '{}' setting: {}", entry.getSetting(), entry.getValue()));
77     }
78
79 }