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
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
14 import com.google.common.collect.ImmutableBiMap;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.ImmutableSetMultimap;
18 import com.google.common.collect.SetMultimap;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import java.lang.reflect.Method;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicReference;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.binding.test.AbstractSchemaAwareTest;
29 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
30 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeLeafOnlyAugment;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
48 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
50 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
51 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
52 .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
53 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
54 BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
55 private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
56 BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
57 private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
58 private static final QName NAME_QNAME = QName.create(Top.QNAME, "name");
59 private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder()
60 .node(Top.QNAME).node(TopLevelList.QNAME).nodeWithKey(
61 TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
64 private BindingToNormalizedNodeCodec codec;
65 private SchemaContext context;
68 protected void setupWithSchema(final SchemaContext schemaContext) {
69 this.context = schemaContext;
70 final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry();
71 this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
76 public void testComplexAugmentationSerialization() {
77 this.codec.onGlobalContextUpdated(this.context);
78 final PathArgument lastArg = this.codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
79 assertTrue(lastArg instanceof AugmentationIdentifier);
84 public void testLeafOnlyAugmentationSerialization() {
85 this.codec.onGlobalContextUpdated(this.context);
86 final PathArgument leafOnlyLastArg = this.codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
87 .getLastPathArgument();
88 assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
89 assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
93 @SuppressWarnings("checkstyle:IllegalCatch")
94 public void testToYangInstanceIdentifierBlocking() {
95 this.codec.onGlobalContextUpdated(new EmptySchemaContext());
97 final CountDownLatch done = new CountDownLatch(1);
98 final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
99 final AtomicReference<RuntimeException> error = new AtomicReference<>();
103 yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
104 } catch (RuntimeException e) {
111 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
112 this.codec.onGlobalContextUpdated(this.context);
114 assertEquals("toYangInstanceIdentifierBlocking completed", true,
115 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
116 if (error.get() != null) {
120 assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
124 public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
125 testGetRpcMethodToSchemaPath();
129 public void testGetRpcMethodToSchemaPathBlocking() {
130 this.codec.onGlobalContextUpdated(new EmptySchemaContext());
131 testGetRpcMethodToSchemaPath();
134 @SuppressWarnings("checkstyle:IllegalCatch")
135 private void testGetRpcMethodToSchemaPath() {
136 final CountDownLatch done = new CountDownLatch(1);
137 final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
138 final AtomicReference<RuntimeException> error = new AtomicReference<>();
141 retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
142 OpendaylightTestRpcServiceService.class));
143 } catch (RuntimeException e) {
150 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
151 this.codec.onGlobalContextUpdated(this.context);
153 assertEquals("getRpcMethodToSchemaPath completed", true,
154 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
155 if (error.get() != null) {
159 for (final Method method: retMap.get().keySet()) {
160 if (method.getName().equals("rockTheHouse")) {
165 fail("rockTheHouse RPC method not found");
168 static class EmptySchemaContext extends AbstractSchemaContext {
170 public Set<Module> getModules() {
171 return ImmutableSet.of();
175 protected Map<QNameModule, Module> getModuleMap() {
176 return ImmutableMap.of();
180 protected SetMultimap<URI, Module> getNamespaceToModules() {
181 return ImmutableSetMultimap.of();
185 protected SetMultimap<String, Module> getNameToModules() {
186 return ImmutableSetMultimap.of();