Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / DOMCodecBug01Test.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.bugfix;
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.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
24 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
25 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.mpls.action._case.PopMplsActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.Flows;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
53
54 import com.google.common.util.concurrent.Futures;
55 import com.google.common.util.concurrent.ListenableFuture;
56 import com.google.common.util.concurrent.ListeningExecutorService;
57 import com.google.common.util.concurrent.MoreExecutors;
58
59 @SuppressWarnings("deprecation")
60 public class DOMCodecBug01Test extends AbstractDataServiceTest {
61
62     private static final long FLOW_ID = 1234;
63     private static final String NODE_ID = "node:1";
64
65     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
66
67     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
68             .child(Node.class, NODE_KEY).toInstance();
69
70     private static final NodeRef NODE_REF = new NodeRef(NODE_INSTANCE_ID_BA);
71
72     private static final FlowKey FLOW_KEY = new FlowKey(FLOW_ID, NODE_REF);
73
74     private static final InstanceIdentifier<? extends DataObject> FLOW_INSTANCE_ID_BA = //
75     InstanceIdentifier.builder(Flows.class) //
76             .child(Flow.class, FLOW_KEY) //
77             .toInstance();
78
79
80
81     /**
82      *
83      * Testcase for https://bugs.opendaylight.org/show_bug.cgi?id=
84      *
85      * Cannot compile CoDec for
86      * org.opendaylight.yang.gen.v1.urn.opendaylight.flow
87      * .config.rev130819.flows.Flow
88      *
89      * When invoking following code in the consumer, user got an
90      * IllegalStateException during creation of mapping between Java DTOs and
91      * data-dom.
92      *
93      * Exception was compilation error which was caused by incorect generation
94      * of code.
95      *
96      * Reported by Depthi V V
97      *
98      * @deprecated This test tests indirect generation, which should be tested
99      *    different way. the test creates conflicting transactions
100      *    and assumes correct commit - to test codec generation
101      *
102      */
103     @Test
104     @Ignore
105     @Deprecated
106     public void testIndirectGeneration() throws Exception {
107
108         ExecutorService basePool = Executors.newFixedThreadPool(2);
109         ListeningExecutorService listenablePool = MoreExecutors.listeningDecorator(basePool);
110
111         createFlow();
112
113         Object lock = new Object();
114         CreateFlowTask task1 = new CreateFlowTask(lock);
115         CreateFlowTask task2 = new CreateFlowTask(lock);
116         CreateFlowTask task3 = new CreateFlowTask(lock);
117
118         ListenableFuture<Void> task1Future = listenablePool.submit(task1);
119         ListenableFuture<Void> task2Future = listenablePool.submit(task2);
120         ListenableFuture<Void> task3Future = listenablePool.submit(task3);
121
122
123         @SuppressWarnings("unchecked")
124         ListenableFuture<List<Void>> compositeFuture = Futures.allAsList(task1Future,task2Future,task3Future);
125
126         Thread.sleep(500);
127         //lock.notifyAll();
128         compositeFuture.get();
129
130         verifyDataAreStoredProperly();
131
132         DataModificationTransaction modification2 = baDataService.beginTransaction();
133         modification2.removeConfigurationData(FLOW_INSTANCE_ID_BA);
134
135         DataObject originalData = modification2.getOriginalConfigurationData().get(FLOW_INSTANCE_ID_BA);
136         assertNotNull(originalData);
137         RpcResult<TransactionStatus> ret2 = modification2.commit().get();
138
139         assertNotNull(ret2);
140         assertEquals(TransactionStatus.COMMITED, ret2.getResult());
141
142         // Data are not in the store.
143         assertNull(baDataService.readConfigurationData(FLOW_INSTANCE_ID_BA));
144
145     }
146
147     private void createFlow() throws Exception {
148
149         DataModificationTransaction modification = baDataService.beginTransaction();
150
151         FlowBuilder flow = new FlowBuilder();
152         MatchBuilder match = new MatchBuilder();
153         VlanMatchBuilder vlanBuilder = new VlanMatchBuilder();
154         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
155         VlanId vlanId = new VlanId(10);
156         vlanBuilder.setVlanId(vlanIdBuilder.setVlanId(vlanId).build());
157         match.setVlanMatch(vlanBuilder.build());
158
159         flow.setKey(FLOW_KEY);
160         flow.setMatch(match.build());
161         flow.setNode(NODE_REF);
162         InstructionsBuilder instructions = new InstructionsBuilder();
163         InstructionBuilder instruction = new InstructionBuilder();
164
165         instruction.setOrder(10);
166         ApplyActionsBuilder applyActions = new ApplyActionsBuilder();
167         List<Action> actionList = new ArrayList<>();
168         PopMplsActionBuilder popMplsAction = new PopMplsActionBuilder();
169         popMplsAction.setEthernetType(34);
170         actionList.add(new ActionBuilder().setAction(new PopMplsActionCaseBuilder().setPopMplsAction(popMplsAction.build()).build()).setOrder(10).build());
171
172         applyActions.setAction(actionList );
173
174         instruction.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(applyActions.build()).build());
175
176
177         List<Instruction> instructionList = Collections.<Instruction>singletonList(instruction.build());
178         instructions.setInstruction(instructionList );
179
180         flow.setInstructions(instructions.build());
181         modification.putConfigurationData(FLOW_INSTANCE_ID_BA, flow.build());
182         RpcResult<TransactionStatus> ret = modification.commit().get();
183         assertNotNull(ret);
184         assertEquals(TransactionStatus.COMMITED, ret.getResult());
185     }
186
187     private void createFlow2() throws Exception {
188         DataModificationTransaction modification = baDataService.beginTransaction();
189         long id = 123;
190         FlowKey key = new FlowKey(id, new NodeRef(NODE_INSTANCE_ID_BA));
191         InstanceIdentifier<?> path1;
192         FlowBuilder flow = new FlowBuilder();
193         flow.setKey(key);
194         MatchBuilder match = new MatchBuilder();
195         match.setLayer4Match(new TcpMatchBuilder().build());
196         flow.setMatch(match.build());
197
198         path1 = InstanceIdentifier.builder(Flows.class).child(Flow.class, key).toInstance();
199        // DataObject cls = (DataObject) modification.readConfigurationData(path1);
200         modification.putConfigurationData(path1, flow.build());
201         modification.commit();
202
203     }
204
205     private class CreateFlowTask implements Callable<Void> {
206
207         public CreateFlowTask(final Object startSync) {
208         }
209
210         @Override
211         public Void call() {
212             try {
213                 //startSyncObj          ect.wait();
214                 //Thread.sleep(500);
215                 createFlow();
216                 createFlow2();
217             } catch (Exception e) {
218                 throw new RuntimeException(e);
219             }
220             return null;
221         }
222     }
223
224     private void verifyDataAreStoredProperly() {
225         CompositeNode biFlows = biDataService.readConfigurationData(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.of(Flows.QNAME));
226         assertNotNull(biFlows);
227         CompositeNode biFlow = biFlows.getFirstCompositeByName(Flow.QNAME);
228         assertNotNull(biFlow);
229     }
230
231
232 }