Bug 6859 - Binding generator v1 refactoring
[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.generator.impl.GeneratedClassLoadingStrategy;
29 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeLeafOnlyAugment;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
36 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
37 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
38 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
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             @Override
101             public void run() {
102                 try {
103                     yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
104                 } catch(final RuntimeException e) {
105                     error.set(e);
106                 } finally {
107                     done.countDown();
108                 }
109             }
110         }.start();
111
112         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
113         this.codec.onGlobalContextUpdated(this.context);
114
115         assertEquals("toYangInstanceIdentifierBlocking completed", true,
116                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
117         if(error.get() != null) {
118             throw error.get();
119         }
120
121         assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
122     }
123
124     @Test
125     public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
126         testGetRpcMethodToSchemaPath();
127     }
128
129     @Test
130     public void testGetRpcMethodToSchemaPathBlocking() {
131         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
132         testGetRpcMethodToSchemaPath();
133     }
134
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<>();
139         new Thread() {
140             @Override
141             public void run() {
142                 try {
143                     retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(OpendaylightTestRpcServiceService.class));
144                 } catch(final RuntimeException e) {
145                     error.set(e);
146                 } finally {
147                     done.countDown();
148                 }
149             }
150         }.start();
151
152         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
153         this.codec.onGlobalContextUpdated(this.context);
154
155         assertEquals("getRpcMethodToSchemaPath completed", true,
156                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
157         if(error.get() != null) {
158             throw error.get();
159         }
160
161         for(final Method method: retMap.get().keySet()) {
162             if(method.getName().equals("rockTheHouse")) {
163                 return;
164             }
165         }
166
167         fail("rockTheHouse RPC method not found");
168     }
169
170     static class EmptySchemaContext extends AbstractSchemaContext {
171         @Override
172         public Set<Module> getModules() {
173             return Collections.emptySet();
174         }
175
176         @Override
177         protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
178             return Collections.emptyMap();
179         }
180
181         @Override
182         protected SetMultimap<URI, Module> getNamespaceToModules() {
183             return Multimaps.forMap(Collections.emptyMap());
184         }
185
186         @Override
187         protected SetMultimap<String, Module> getNameToModules() {
188             return Multimaps.forMap(Collections.emptyMap());
189         }
190     }
191 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.