diff options
author | Ravi Pendurty <ravi.pendurty@highstreet-technologies.com> | 2021-02-03 14:43:58 +0100 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2021-02-05 22:56:21 +0000 |
commit | f7cc6a1daa39f583494dc0a71b89e1a1317a0a2b (patch) | |
tree | 6f780ecb8c6fc7be8ec2b3ab3f6baec1481be4c5 /sdnr/wt/netconfnode-state-service/provider/src/test | |
parent | 8bbecead253c30da60577ecd014d59983e4b6113 (diff) |
Migrate websocketmanager and netconfnode-state-service
Migrate websocketmanager and netconfnode-state-service to Aluminium
Issue-ID: CCSDK-3131
Signed-off-by: Ravi Pendurty <ravi.pendurty@highstreet-technologies.com>
Change-Id: If82aa071b8187f6cbd02f187d93b61a15463d718
Signed-off-by: Ravi Pendurty <ravi.pendurty@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/netconfnode-state-service/provider/src/test')
3 files changed, 176 insertions, 10 deletions
diff --git a/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestDom.java b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestDom.java new file mode 100644 index 000000000..f7aef6b6d --- /dev/null +++ b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestDom.java @@ -0,0 +1,80 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test; + +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.io.StringWriter; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotification; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotificationBuilder; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestDom { + + private static final Logger LOG = LoggerFactory.getLogger(TestDom.class); + + @Test + public void test2() { + FaultNotification faultNotification = + new FaultNotificationBuilder().setCounter(1).setNodeId("Node1").setSeverity(SeverityType.Major).build(); + +// final NormalizedNode<?, ?> data = DomContext.getBINDING_CONTEXT() +// .toNormalizedNode(InstanceIdentifier.create(FaultNotification.class), faultNotification).getValue(); + +// LOG.info("Normalized node: {}", data); +// final String json = toJson(data, schemaContext); +// LOG.info(json); + + } + /** + * Serialization of {@link NormalizedNode} into {@link String}. + * + * @param node to be serialized data + * @param schemaContext schema context + * @return serialized data + */ + private static String toJson(final NormalizedNode<?, ?> node, final EffectiveModelContext schemaContext) { + final JSONCodecFactory codecFactory = JSONCodecFactorySupplier.RFC7951.createSimple(schemaContext); + try (StringWriter stringWriter = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(stringWriter); + NormalizedNodeWriter nodeStreamer = NormalizedNodeWriter.forStreamWriter( + JSONNormalizedNodeStreamWriter.createNestedWriter(codecFactory, SchemaPath.ROOT, + schemaContext.getQName().getNamespace(), jsonWriter))) { + jsonWriter.beginObject(); + nodeStreamer.write(node); + jsonWriter.endObject(); + return stringWriter.toString(); + } catch (IOException e) { + throw new IllegalStateException("Failed to convert input node to JSON: " + node, e); + } + } +} diff --git a/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestNetconfNodeStateService.java b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestNetconfNodeStateService.java index ad410dd44..5de8b7075 100644 --- a/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestNetconfNodeStateService.java +++ b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/TestNetconfNodeStateService.java @@ -44,25 +44,26 @@ import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.VesNotificationListener; -import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.GenericTransactionUtils; -import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfAccessorImpl; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfNodeStateServiceImpl; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.rpc.NetconfnodeStateServiceRpcApiImpl; +import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.example.ExampleConfig; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.ClusterSingletonServiceProviderMock; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointMock; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointServiceMock; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.NotificationPublishServiceMock; import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.RpcProviderRegistryMock; +import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers; import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.DataObjectModification; import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType; import org.opendaylight.mdsal.binding.api.DataTreeChangeListener; -import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; import org.opendaylight.mdsal.binding.api.DataTreeModification; import org.opendaylight.mdsal.binding.api.MountPointService; import org.opendaylight.mdsal.binding.api.NotificationPublishService; import org.opendaylight.mdsal.binding.api.RpcProviderService; +import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer; +import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder; @@ -86,6 +87,9 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; +import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory; +import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -105,6 +109,7 @@ public class TestNetconfNodeStateService extends Mockito { private static final Logger LOG = LoggerFactory.getLogger(TestNetconfNodeStateService.class); + @SuppressWarnings("unchecked") @BeforeClass public static <T extends DataObject, L extends DataTreeChangeListener<T>> void before() throws InterruptedException, IOException { @@ -120,8 +125,6 @@ public class TestNetconfNodeStateService extends Mockito { //dataBrokerNetconf = new DataBrokerNetconfMock(); //dataBrokerNetconf.newReadWriteTransaction(); dataBrokerNetconf = mock(DataBroker.class); - ArgumentCaptor<DataTreeIdentifier<?>> argument1 = ArgumentCaptor.forClass(DataTreeIdentifier.class); - ArgumentCaptor<DataTreeChangeListener<?>> argument2 = ArgumentCaptor.forClass(DataTreeChangeListener.class); when(dataBrokerNetconf.registerDataTreeChangeListener(any(), any())).thenAnswer( invocation -> { Object pListener = invocation.getArguments()[1]; @@ -153,7 +156,8 @@ public class TestNetconfNodeStateService extends Mockito { NotificationPublishService notificationPublishService = new NotificationPublishServiceMock(); RpcProviderService rpcProviderRegistry = new RpcProviderRegistryMock(); IEntityDataProvider entityProviderMock = mock(IEntityDataProvider.class); - + YangParserFactory yangParserFactory = new YangParserFactoryImpl(); + BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer = new BindingCodecContext(BindingRuntimeHelpers.createRuntimeContext()); // start using blueprint interface netconfStateService = new NetconfNodeStateServiceImpl(); @@ -163,6 +167,8 @@ public class TestNetconfNodeStateService extends Mockito { netconfStateService.setRpcProviderRegistry(rpcProviderRegistry); netconfStateService.setClusterSingletonService(clusterSingletonService); netconfStateService.setEntityDataProvider(entityProviderMock); + netconfStateService.setYangParserFactory(yangParserFactory); + netconfStateService.setBindingNormalizedNodeSerializer(bindingNormalizedNodeSerializer); netconfStateService.init(); System.out.println("Initialization done"); } @@ -233,11 +239,10 @@ public class TestNetconfNodeStateService extends Mockito { String nodeIdString = "Test"; NodeId nodeId = new NodeId(nodeIdString); NodeBuilder nodeBuilder = new NodeBuilder(); - nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf); + + nodeBuilder.addAugmentation(rootNodeNetconf); nodeBuilder.setNodeId(nodeId); Node rootNode = nodeBuilder.build(); - NetconfAccessor acessor = new NetconfAccessorImpl(nodeId, rootNodeNetconf, mountPoint.getDataBroker(), - mountPoint, new GenericTransactionUtils()); DataObjectModification<Node> dom = mock(DataObjectModification.class); when(dom.getDataAfter()).thenReturn(rootNode); @@ -271,7 +276,7 @@ public class TestNetconfNodeStateService extends Mockito { NetconfNode rootNodeNetconf = netconfNodeBuilder.build(); NodeBuilder nodeBuilder = new NodeBuilder(); - nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf); + nodeBuilder.addAugmentation(rootNodeNetconf); nodeBuilder.setNodeId(new NodeId("Test")); Node rootNodeAfter = nodeBuilder.build(); @@ -351,6 +356,10 @@ public class TestNetconfNodeStateService extends Mockito { System.out.println("Output " + output); } + @Test + public void test10ApiPushNotifiction() throws YangParserException, IOException { + ExampleConfig.exampleConfig(netconfStateService.getDomContext()); + } // ------- private section diff --git a/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/example/ExampleConfig.java b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/example/ExampleConfig.java new file mode 100644 index 000000000..f17453803 --- /dev/null +++ b/sdnr/wt/netconfnode-state-service/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/test/example/ExampleConfig.java @@ -0,0 +1,77 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.example; + +import java.io.IOException; +import java.net.URI; +import java.util.Objects; +import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomContext; +import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomParser; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.config.rev201208.Configuration; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.Revision; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.parser.api.YangParser; +import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; +import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ExampleConfig { + + private static final Logger LOG = LoggerFactory.getLogger(ExampleConfig.class); + + // specification of YANG module + private static final QNameModule CONFIG_MODULE = QNameModule.create( + URI.create("urn:ietf:params:xml:ns:yang:config"), Revision.of("2020-12-08")); + // path to 'configuration' container (it is a root container) + private static final YangInstanceIdentifier CONFIGURATION_PATH = YangInstanceIdentifier.builder() + .node(QName.create(CONFIG_MODULE, "configuration")) + .build(); + + + public static void exampleConfig(DomContext domContext) throws YangParserException, IOException { + // (1) preparation of schema context with module that describes configuration (it is possible to load multiple + // schemas into parser) + //final YangParser parser = new YangParserFactoryImpl().createParser(); + final YangParser parser = domContext.getYangParserFactory().createParser(); + parser.addSource(YangTextSchemaSource.forResource("/META-INF/yang/config@2020-12-08.yang")); + final EffectiveModelContext schemaContext = parser.buildEffectiveModel(); + + // (2) parsing of configuration into binding-independent format + final NormalizedNode<?, ?> data = DomParser.parseJsonFile("/example.json", schemaContext); + + // (3) conversion into binding-aware format (md-sal codec needs to know about path on which data is placed) + final Configuration config = (Configuration) domContext.getBindingNormalizedNodeSerializer().fromNormalizedNode(CONFIGURATION_PATH, data) + .getValue(); + + // (4) printing some useful information + LOG.info("Value of 'config1': {}", config.getConfig1()); + LOG.info("Value of 'config2': {}", config.isConfig2()); + Objects.requireNonNull(config.getEntry()).forEach((entryKey, entry) -> + LOG.info("Value of '{}' setting: {}", entry.getSetting(), entry.getValue())); + } + +} |