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