Fix checkstyle violations in sal-binding-broker
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / BindingNormalizedCodecTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.binding.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.ImmutableSetMultimap;
19 import com.google.common.collect.SetMultimap;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import java.lang.reflect.Method;
22 import java.net.URI;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28 import javassist.ClassPool;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.binding.test.AbstractSchemaAwareTest;
31 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
32 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
33 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
34 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
35 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeLeafOnlyAugment;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.QNameModule;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.model.api.Module;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
52
53 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
54
55     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
56     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
57             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
58     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
59             BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
60     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
61             BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
62     private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
63     private static final QName NAME_QNAME = QName.create(Top.QNAME, "name");
64     private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder()
65             .node(Top.QNAME).node(TopLevelList.QNAME).nodeWithKey(
66                     TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
67
68
69     private BindingToNormalizedNodeCodec codec;
70     private SchemaContext context;
71
72     @Override
73     protected void setupWithSchema(final SchemaContext schemaContext) {
74         this.context = schemaContext;
75         final DataObjectSerializerGenerator streamWriter = StreamWriterGenerator
76                 .create(JavassistUtils.forClassPool(ClassPool.getDefault()));
77         final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry(streamWriter);
78         this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
79                 registry, true);
80     }
81
82     @Test
83     public void testComplexAugmentationSerialization() {
84         this.codec.onGlobalContextUpdated(this.context);
85         final PathArgument lastArg = this.codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
86         assertTrue(lastArg instanceof AugmentationIdentifier);
87     }
88
89
90     @Test
91     public void testLeafOnlyAugmentationSerialization() {
92         this.codec.onGlobalContextUpdated(this.context);
93         final PathArgument leafOnlyLastArg = this.codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
94                 .getLastPathArgument();
95         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
96         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
97     }
98
99     @Test
100     @SuppressWarnings("checkstyle:IllegalCatch")
101     public void testToYangInstanceIdentifierBlocking() {
102         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
103
104         final CountDownLatch done = new CountDownLatch(1);
105         final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
106         final AtomicReference<RuntimeException> error = new AtomicReference<>();
107
108         new Thread(() -> {
109             try {
110                 yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
111             } catch (RuntimeException e) {
112                 error.set(e);
113             } finally {
114                 done.countDown();
115             }
116         }).start();
117
118         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
119         this.codec.onGlobalContextUpdated(this.context);
120
121         assertEquals("toYangInstanceIdentifierBlocking completed", true,
122                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
123         if (error.get() != null) {
124             throw error.get();
125         }
126
127         assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
128     }
129
130     @Test
131     public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
132         testGetRpcMethodToSchemaPath();
133     }
134
135     @Test
136     public void testGetRpcMethodToSchemaPathBlocking() {
137         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
138         testGetRpcMethodToSchemaPath();
139     }
140
141     @SuppressWarnings("checkstyle:IllegalCatch")
142     private void testGetRpcMethodToSchemaPath() {
143         final CountDownLatch done = new CountDownLatch(1);
144         final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
145         final AtomicReference<RuntimeException> error = new AtomicReference<>();
146         new Thread(() -> {
147             try {
148                 retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
149                         OpendaylightTestRpcServiceService.class));
150             } catch (RuntimeException e) {
151                 error.set(e);
152             } finally {
153                 done.countDown();
154             }
155         }).start();
156
157         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
158         this.codec.onGlobalContextUpdated(this.context);
159
160         assertEquals("getRpcMethodToSchemaPath completed", true,
161                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
162         if (error.get() != null) {
163             throw error.get();
164         }
165
166         for (final Method method: retMap.get().keySet()) {
167             if (method.getName().equals("rockTheHouse")) {
168                 return;
169             }
170         }
171
172         fail("rockTheHouse RPC method not found");
173     }
174
175     static class EmptySchemaContext extends AbstractSchemaContext {
176         @Override
177         public Set<Module> getModules() {
178             return ImmutableSet.of();
179         }
180
181         @Override
182         protected Map<QNameModule, Module> getModuleMap() {
183             return ImmutableMap.of();
184         }
185
186         @Override
187         protected SetMultimap<URI, Module> getNamespaceToModules() {
188             return ImmutableSetMultimap.of();
189         }
190
191         @Override
192         protected SetMultimap<String, Module> getNameToModules() {
193             return ImmutableSetMultimap.of();
194         }
195     }
196 }