2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.binding.dom.adapter;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.Multimaps;
17 import com.google.common.collect.SetMultimap;
18 import com.google.common.util.concurrent.Uninterruptibles;
19 import java.lang.reflect.Method;
21 import java.util.Collections;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicReference;
27 import javassist.ClassPool;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractSchemaAwareTest;
30 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
31 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
32 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
33 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
34 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
52 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
54 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
55 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
56 .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
57 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
58 .augmentation(TreeLeafOnlyAugment.class);
59 private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES = BA_TOP_LEVEL_LIST
60 .augmentation(TreeComplexUsesAugment.class);
61 private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
62 private static final QName NAME_QNAME = QName.create(Top.QNAME, "name");
63 private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder().node(Top.QNAME)
64 .node(TopLevelList.QNAME).nodeWithKey(TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
66 private BindingToNormalizedNodeCodec codec;
67 private SchemaContext context;
70 protected void setupWithSchema(final SchemaContext schemaContext) {
71 this.context = schemaContext;
72 final DataObjectSerializerGenerator streamWriter = StreamWriterGenerator.create(JavassistUtils.forClassPool(
73 ClassPool.getDefault()));
74 final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry(streamWriter);
75 this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
80 public void testComplexAugmentationSerialization() {
81 this.codec.onGlobalContextUpdated(this.context);
82 final PathArgument lastArg = this.codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
83 assertTrue(lastArg instanceof AugmentationIdentifier);
88 public void testLeafOnlyAugmentationSerialization() {
89 this.codec.onGlobalContextUpdated(this.context);
90 final PathArgument leafOnlyLastArg = this.codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
91 .getLastPathArgument();
92 assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
93 assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
97 @SuppressWarnings("checkstyle:illegalCatch")
98 public void testToYangInstanceIdentifierBlocking() {
99 this.codec.onGlobalContextUpdated(new EmptySchemaContext());
101 final CountDownLatch done = new CountDownLatch(1);
102 final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
103 final AtomicReference<RuntimeException> error = new AtomicReference<>();
106 yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
107 } catch (final RuntimeException e) {
114 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
115 this.codec.onGlobalContextUpdated(this.context);
117 assertTrue("toYangInstanceIdentifierBlocking completed",
118 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
119 if (error.get() != null) {
123 assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
127 public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
128 testGetRpcMethodToSchemaPath();
132 public void testGetRpcMethodToSchemaPathBlocking() {
133 this.codec.onGlobalContextUpdated(new EmptySchemaContext());
134 testGetRpcMethodToSchemaPath();
137 @SuppressWarnings("checkstyle:illegalCatch")
138 private void testGetRpcMethodToSchemaPath() {
139 final CountDownLatch done = new CountDownLatch(1);
140 final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
141 final AtomicReference<RuntimeException> error = new AtomicReference<>();
144 retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
145 OpendaylightTestRpcServiceService.class));
146 } catch (final RuntimeException e) {
153 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
154 this.codec.onGlobalContextUpdated(this.context);
156 assertTrue("getRpcMethodToSchemaPath completed",
157 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
158 if (error.get() != null) {
162 for (final Method method : retMap.get().keySet()) {
163 if (method.getName().equals("rockTheHouse")) {
168 fail("rockTheHouse RPC method not found");
171 static class EmptySchemaContext extends AbstractSchemaContext {
173 public Set<Module> getModules() {
174 return Collections.emptySet();
178 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
179 return Collections.emptyMap();
183 protected SetMultimap<URI, Module> getNamespaceToModules() {
184 return Multimaps.forMap(Collections.emptyMap());
188 protected SetMultimap<String, Module> getNameToModules() {
189 return Multimaps.forMap(Collections.emptyMap());