BUG-2682: switch sal-binding-dom-it to sal test models
[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 /**
48  * FIXME: Migrate to use new Data Broker APIs
49  */
50 @SuppressWarnings("deprecation")
51 public class ChangeOriginatedInDomBrokerTest extends AbstractDataServiceTest {
52
53     protected static final Logger LOG = LoggerFactory.getLogger(ChangeOriginatedInDomBrokerTest.class);
54
55     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
56     private static final QName LIST1_ATTR_STR_QNAME = QName.create(List1.QNAME, "attr-str");
57
58     private static final String TLL_NAME = "1";
59     private static final int LIST11_ATTR_INT = 1234;
60     private static final String LIST1_ATTR_STR = "foo:foo";
61
62     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
63     private static final List1Key LIST1_KEY = new List1Key(LIST1_ATTR_STR);
64     private static final List11Key LIST11_KEY = new List11Key(LIST11_ATTR_INT);
65
66     protected final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> modificationCapture = SettableFuture.create();
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> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
72             .child(TopLevelList.class, TLL_KEY).toInstance();
73
74     private static final Map<QName, Object> LIST1_KEY_BI = //
75     ImmutableMap.<QName, Object> of(LIST1_ATTR_STR_QNAME, LIST1_ATTR_STR);;
76
77     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier LIST1_INSTANCE_ID_BI = //
78     org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
79             .node(Top.QNAME) //
80             .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
81             .nodeWithKey(List1.QNAME, LIST1_KEY_BI) //
82             .toInstance();
83
84     private static final InstanceIdentifier<List1> LIST1_PATH_BA = //
85             NODE_INSTANCE_ID_BA.builder() //
86             .augmentation(TllComplexAugment.class) //
87             .child(List1.class, LIST1_KEY) //
88             .toInstance();
89
90     @Test
91     public void simpleModifyOperation() throws Exception {
92
93         assertNull(biDataService.readConfigurationData(LIST1_INSTANCE_ID_BI));
94
95         registerChangeListener();
96
97         CompositeNode domflow = createTestList1();
98         DataModificationTransaction biTransaction = biDataService.beginTransaction();
99         biTransaction.putConfigurationData(LIST1_INSTANCE_ID_BI, domflow);
100         RpcResult<TransactionStatus> biResult = biTransaction.commit().get();
101         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
102         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = modificationCapture.get(1000,TimeUnit.MILLISECONDS);
103         assertNotNull(event);
104         LOG.info("Created Configuration :{}",event.getCreatedConfigurationData());
105         List1 list1 = (List1) event.getCreatedConfigurationData().get(LIST1_PATH_BA);
106         assertNotNull(list1);
107         assertNotNull(list1.getAttrStr());
108         assertNotNull(list1.getList11());
109         assertNotNull(list1.getList12());
110         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
111
112     }
113
114     private void registerChangeListener() {
115         baDataService.registerDataChangeListener(LIST1_PATH_BA, new DataChangeListener() {
116
117             @Override
118             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
119                 LOG.info("Data Change listener invoked.");
120                 modificationCapture.set(change);
121             }
122         });
123     }
124
125     private CompositeNode createTestList1() {
126         List1Builder l1b = new List1Builder();
127         List11Builder l11b = new List11Builder();
128         List12Builder l12b = new List12Builder();
129         l11b.setKey(LIST11_KEY);
130         l11b.setAttrStr("foo:foo:foo");
131         l12b.setKey(new List12Key(321));
132         l12b.setAttrStr("foo:foo:bar");
133         l1b.setKey(LIST1_KEY);
134         l1b.setList11(ImmutableList.of(l11b.build()));
135         l1b.setList12(ImmutableList.of(l12b.build()));
136         CompositeNode domList1 = mappingService.toDataDom(l1b.build());
137         return domList1;
138     }
139 }

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