Add PathArgument uint adaptation
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / CrossBrokerMountPointTest.java
1 /*
2  * Copyright (c) 2014 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.sal.binding.test.connect.dom;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FluentFuture;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
28 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
35 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
37 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
38 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
39 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
40 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
41 import org.opendaylight.mdsal.common.api.CommitInfo;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.List11SimpleAugment;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Key;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Key;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.top.top.level.list.list1.list1._1.Cont;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
59 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
60
61 public class CrossBrokerMountPointTest {
62
63     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
64     private static final String TLL_NAME = "foo:1";
65
66     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
67
68     private static final Map<QName, Object> TLL_KEY_BI = Collections.<QName, Object>singletonMap(TLL_NAME_QNAME,
69             TLL_NAME);
70
71     private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
72             .child(TopLevelList.class, TLL_KEY).build();
73
74     private static final List1Key LIST1_KEY = new List1Key("foo");
75     private static final List11Key LIST11_KEY = new List11Key(1);
76
77     private static final InstanceIdentifier<Cont> AUG_CONT_ID_BA = TLL_INSTANCE_ID_BA
78             .builder().augmentation(TllComplexAugment.class) //
79             .child(List1.class, LIST1_KEY) //
80             .child(List11.class, LIST11_KEY) //
81             .augmentation(List11SimpleAugment.class) //
82             .child(Cont.class) //
83             .build();
84
85     private static final QName AUG_CONT = QName.create(List11.QNAME,
86             Cont.QNAME.getLocalName());
87
88     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TLL_INSTANCE_ID_BI = //
89         YangInstanceIdentifier.builder() //
90             .node(Top.QNAME) //
91             .node(TopLevelList.QNAME) //
92             .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
93             .build();
94
95     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier GROUP_STATISTICS_ID_BI =
96         YangInstanceIdentifier
97             .builder(TLL_INSTANCE_ID_BI)
98             .nodeWithKey(QName.create(TllComplexAugment.QNAME, "list1"),
99                     QName.create(TllComplexAugment.QNAME, "attr-str"), LIST1_KEY.getAttrStr())
100             .nodeWithKey(QName.create(TllComplexAugment.QNAME, "list1-1"),
101                     QName.create(TllComplexAugment.QNAME, "attr-int"), LIST11_KEY.getAttrInt())
102             .node(AUG_CONT).build();
103
104     private BindingTestContext testContext;
105     private MountPointService bindingMountPointService;
106     private DOMMountPointService domMountPointService;
107
108     @Before
109     public void setup() {
110         final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
111         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
112         testFactory.setStartWithParsedSchema(true);
113         testContext = testFactory.getTestContext();
114
115         testContext.start();
116         bindingMountPointService = testContext.getBindingMountPointService();
117         domMountPointService = testContext.getDomMountProviderService();
118
119         // biRpcInvoker = testContext.getDomRpcInvoker();
120         assertNotNull(bindingMountPointService);
121         assertNotNull(domMountPointService);
122
123         // flowService = MessageCapturingFlowService.create(baRpcRegistry);
124     }
125
126     @Test
127     public void testMountPoint() throws ReadFailedException, TimeoutException {
128         final Integer attrIntValue = 500;
129         domMountPointService.createMountPoint(TLL_INSTANCE_ID_BI)
130             .addService(DOMDataBroker.class, new DOMDataBroker() {
131
132                 @Override
133                 public DOMDataWriteTransaction newWriteOnlyTransaction() {
134                     throw new UnsupportedOperationException();
135                 }
136
137                 @Override
138                 public DOMDataReadWriteTransaction newReadWriteTransaction() {
139                     return  new DOMDataReadWriteTransaction() {
140
141                         @Override
142                         @SuppressModernizer
143                         public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
144                                 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
145                             if (store == LogicalDatastoreType.OPERATIONAL && path.getLastPathArgument()
146                                     .equals(GROUP_STATISTICS_ID_BI.getLastPathArgument())) {
147
148                                 final ContainerNode data = Builders.containerBuilder()
149                                         .withNodeIdentifier(new NodeIdentifier(AUG_CONT))
150                                         .withChild(ImmutableNodes.leafNode(QName.create(AUG_CONT, "attr-int"),
151                                                 attrIntValue))
152                                         .build();
153
154                                 return Futures.immediateCheckedFuture(Optional.<NormalizedNode<?,?>>of(data));
155                             }
156                             return Futures.immediateFailedCheckedFuture(new ReadFailedException(TLL_NAME,
157                                     new Exception()));
158                         }
159
160                         @Override
161                         public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
162                                 final YangInstanceIdentifier path) {
163                             throw new UnsupportedOperationException();
164                         }
165
166                         @Override
167                         public Object getIdentifier() {
168                             return this;
169                         }
170
171                         @Override
172                         public boolean cancel() {
173                             return false;
174                         }
175
176                         @Override
177                         public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
178                             throw new UnsupportedOperationException();
179                         }
180
181                         @Override
182                         public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
183                                 final NormalizedNode<?, ?> data) {
184                             throw new UnsupportedOperationException();
185                         }
186
187                         @Override
188                         public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
189                                 final NormalizedNode<?, ?> data) {
190                             throw new UnsupportedOperationException();
191                         }
192
193                         @Override
194                         public FluentFuture<? extends CommitInfo> commit() {
195                             throw new UnsupportedOperationException();
196                         }
197                     };
198                 }
199
200                 @Override
201                 public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
202                     throw new UnsupportedOperationException();
203                 }
204
205                 @Override
206                 public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
207                     throw new UnsupportedOperationException();
208                 }
209
210                 @Override
211                 public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
212                     return Collections.emptyMap();
213                 }
214             }).register();
215
216         final Optional<MountPoint> bindingMountPoint = bindingMountPointService.getMountPoint(TLL_INSTANCE_ID_BA);
217         assertTrue(bindingMountPoint.isPresent());
218
219         final Optional<DataBroker> dataBroker = bindingMountPoint.get().getService(DataBroker.class);
220         assertTrue(dataBroker.isPresent());
221
222         final Optional<Cont> data = dataBroker.get().newReadWriteTransaction().read(LogicalDatastoreType.OPERATIONAL,
223                 AUG_CONT_ID_BA).checkedGet(5, TimeUnit.SECONDS);
224         assertTrue(data.isPresent());
225         assertEquals(attrIntValue ,data.get().getAttrInt());
226     }
227 }

©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.