Deprecate all MD-SAL APIs
[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 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
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;
21 import java.net.URI;
22 import java.util.Map;
23 import java.util.Set;
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;
47
48 @Deprecated
49 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
50
51     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
52     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
53             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
54     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
55             BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
56     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES =
57             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 schemaContext) {
70         this.context = schemaContext;
71         final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry();
72         this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
73                 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)
88                 .getLastPathArgument();
89         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
90         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
91     }
92
93     @Test
94     @SuppressWarnings("checkstyle:IllegalCatch")
95     public void testToYangInstanceIdentifierBlocking() {
96         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
97
98         final CountDownLatch done = new CountDownLatch(1);
99         final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
100         final AtomicReference<RuntimeException> error = new AtomicReference<>();
101
102         new Thread(() -> {
103             try {
104                 yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
105             } catch (RuntimeException e) {
106                 error.set(e);
107             } finally {
108                 done.countDown();
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     @SuppressWarnings("checkstyle:IllegalCatch")
136     private void testGetRpcMethodToSchemaPath() {
137         final CountDownLatch done = new CountDownLatch(1);
138         final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
139         final AtomicReference<RuntimeException> error = new AtomicReference<>();
140         new Thread(() -> {
141             try {
142                 retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
143                         OpendaylightTestRpcServiceService.class));
144             } catch (RuntimeException e) {
145                 error.set(e);
146             } finally {
147                 done.countDown();
148             }
149         }).start();
150
151         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
152         this.codec.onGlobalContextUpdated(this.context);
153
154         assertEquals("getRpcMethodToSchemaPath completed", true,
155                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
156         if (error.get() != null) {
157             throw error.get();
158         }
159
160         for (final Method method: retMap.get().keySet()) {
161             if (method.getName().equals("rockTheHouse")) {
162                 return;
163             }
164         }
165
166         fail("rockTheHouse RPC method not found");
167     }
168
169     static class EmptySchemaContext extends AbstractSchemaContext {
170         @Override
171         public Set<Module> getModules() {
172             return ImmutableSet.of();
173         }
174
175         @Override
176         protected Map<QNameModule, Module> getModuleMap() {
177             return ImmutableMap.of();
178         }
179
180         @Override
181         protected SetMultimap<URI, Module> getNamespaceToModules() {
182             return ImmutableSetMultimap.of();
183         }
184
185         @Override
186         protected SetMultimap<String, Module> getNameToModules() {
187             return ImmutableSetMultimap.of();
188         }
189     }
190 }