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