e7af4dffae98b0b0543a8919c8ead80c6132512a
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / SchemaUpdateForTransactionTest.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.md.sal.dom.store.impl;
9
10 import static org.junit.Assert.assertNotNull;
11 import com.google.common.base.Throwables;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.concurrent.ExecutionException;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
20 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
21 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public class SchemaUpdateForTransactionTest {
27
28     private static final YangInstanceIdentifier TOP_PATH = YangInstanceIdentifier.of(Top.QNAME);
29     private SchemaContext schemaContext;
30     private InMemoryDOMDataStore domStore;
31
32     @Before
33     public void setupStore() {
34         domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.newDirectExecutorService());
35         loadSchemas(RockTheHouseInput.class);
36     }
37
38     public void loadSchemas(final Class<?>... classes) {
39         YangModuleInfo moduleInfo;
40         try {
41             ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
42             for (Class<?> clz : classes) {
43                 moduleInfo = BindingReflections.getModuleInfo(clz);
44
45                 context.registerModuleInfo(moduleInfo);
46             }
47             schemaContext = context.tryToCreateSchemaContext().get();
48             domStore.onGlobalContextUpdated(schemaContext);
49         } catch (Exception e) {
50             Throwables.propagateIfPossible(e);
51         }
52     }
53
54     /**
55      * Test suite tests allocating transaction when schema context
56      * does not contain module necessary for client write,
57      * then triggering update of global schema context
58      * and then performing write (according to new module).
59      *
60      * If transaction between allocation and schema context was
61      * unmodified, it is safe to change its schema context
62      * to new one (e.g. it will be same as if allocated after
63      * schema context update.)
64      *
65      * @throws InterruptedException
66      * @throws ExecutionException
67      */
68     @Test
69     public void testTransactionSchemaUpdate() throws InterruptedException, ExecutionException {
70
71         assertNotNull(domStore);
72
73         // We allocate transaction, initial schema context does not
74         // contain Lists model
75         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
76         assertNotNull(writeTx);
77
78         // we trigger schema context update to contain Lists model
79         loadSchemas(RockTheHouseInput.class, Top.class);
80
81         /**
82          *
83          * Writes /test in writeTx, this write should not fail
84          * with IllegalArgumentException since /test is in
85          * schema context.
86          *
87          */
88         writeTx.write(TOP_PATH, ImmutableNodes.containerNode(Top.QNAME));
89
90     }
91
92 }

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