5a436c81b6ce25bf2d84103c2758ac8704388298
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / ChangeOriginatedInDomBrokerTest.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.assertNull;
13
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.concurrent.TimeUnit;
17
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
21 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
22 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
23 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Key;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Key;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List12Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List12Key;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.collect.ImmutableList;
44 import com.google.common.collect.ImmutableMap;
45 import com.google.common.util.concurrent.SettableFuture;
46
47 // FIXME: Migrate to use new Data Broker APIs
48 @SuppressWarnings("deprecation")
49 public class ChangeOriginatedInDomBrokerTest extends AbstractDataServiceTest {
50
51     private static final Logger LOG = LoggerFactory.getLogger(ChangeOriginatedInDomBrokerTest.class);
52
53     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
54     private static final QName LIST1_ATTR_STR_QNAME = QName.create(List1.QNAME, "attr-str");
55
56     private static final String TLL_NAME = "1";
57     private static final int LIST11_ATTR_INT = 1234;
58     private static final String LIST1_ATTR_STR = "foo:foo";
59
60     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
61     private static final List1Key LIST1_KEY = new List1Key(LIST1_ATTR_STR);
62     private static final List11Key LIST11_KEY = new List11Key(LIST11_ATTR_INT);
63
64     private final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> modificationCapture = SettableFuture.create();
65
66     private static final Map<QName, Object> TLL_KEY_BI = Collections.<QName, Object> singletonMap(TLL_NAME_QNAME,
67             TLL_NAME);
68
69     private static final InstanceIdentifier<TopLevelList> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
70             .child(TopLevelList.class, TLL_KEY).toInstance();
71
72     private static final Map<QName, Object> LIST1_KEY_BI = //
73     ImmutableMap.<QName, Object> of(LIST1_ATTR_STR_QNAME, LIST1_ATTR_STR);;
74
75     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier LIST1_INSTANCE_ID_BI = //
76     org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
77             .node(Top.QNAME) //
78             .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
79             .nodeWithKey(List1.QNAME, LIST1_KEY_BI) //
80             .toInstance();
81
82     private static final InstanceIdentifier<List1> LIST1_PATH_BA = //
83             NODE_INSTANCE_ID_BA.builder() //
84             .augmentation(TllComplexAugment.class) //
85             .child(List1.class, LIST1_KEY) //
86             .toInstance();
87
88     @Test
89     public void simpleModifyOperation() throws Exception {
90
91         assertNull(biDataService.readConfigurationData(LIST1_INSTANCE_ID_BI));
92
93         registerChangeListener();
94
95         CompositeNode domflow = createTestList1();
96         DataModificationTransaction biTransaction = biDataService.beginTransaction();
97         biTransaction.putConfigurationData(LIST1_INSTANCE_ID_BI, domflow);
98         RpcResult<TransactionStatus> biResult = biTransaction.commit().get();
99         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
100         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = modificationCapture.get(1000,TimeUnit.MILLISECONDS);
101         assertNotNull(event);
102         LOG.info("Created Configuration :{}",event.getCreatedConfigurationData());
103         List1 list1 = (List1) event.getCreatedConfigurationData().get(LIST1_PATH_BA);
104         assertNotNull(list1);
105         assertNotNull(list1.getAttrStr());
106         assertNotNull(list1.getList11());
107         assertNotNull(list1.getList12());
108         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
109
110     }
111
112     private void registerChangeListener() {
113         baDataService.registerDataChangeListener(LIST1_PATH_BA, new DataChangeListener() {
114
115             @Override
116             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
117                 LOG.info("Data Change listener invoked.");
118                 modificationCapture.set(change);
119             }
120         });
121     }
122
123     private CompositeNode createTestList1() {
124         List1Builder l1b = new List1Builder();
125         List11Builder l11b = new List11Builder();
126         List12Builder l12b = new List12Builder();
127         l11b.setKey(LIST11_KEY);
128         l11b.setAttrStr("foo:foo:foo");
129         l12b.setKey(new List12Key(321));
130         l12b.setAttrStr("foo:foo:bar");
131         l1b.setKey(LIST1_KEY);
132         l1b.setList11(ImmutableList.of(l11b.build()));
133         l1b.setList12(ImmutableList.of(l12b.build()));
134         CompositeNode domList1 = mappingService.toDataDom(l1b.build());
135         return domList1;
136     }
137 }

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