Minor cleanup of test suites in MD-SAL
[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.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.TimeUnit;
19
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
23 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
24 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
25 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 import com.google.common.collect.ImmutableMap;
63 import com.google.common.util.concurrent.SettableFuture;
64
65 // FIXME: Migrate to use new Data Broker APIs
66 @SuppressWarnings("deprecation")
67 public class ChangeOriginatedInDomBrokerTest extends AbstractDataServiceTest {
68
69     private static final Logger LOG = LoggerFactory.getLogger(ChangeOriginatedInDomBrokerTest.class);
70
71     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
72     private static final QName FLOW_ID_QNAME = QName.create(Flow.QNAME, "id");
73     private static final QName TABLE_ID_QNAME = QName.create(Table.QNAME, "id");
74
75     private static final String NODE_ID = "node:1";
76     private static final FlowId FLOW_ID = new FlowId("1234");
77     private static final Short TABLE_ID = Short.valueOf((short) 0);
78
79     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
80     private static final FlowKey FLOW_KEY = new FlowKey(FLOW_ID);
81
82     private final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> modificationCapture = SettableFuture.create();
83
84     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
85             NODE_ID);
86
87     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
88             .child(Node.class, NODE_KEY).toInstance();
89
90     private static final Map<QName, Object> FLOW_KEY_BI = //
91     ImmutableMap.<QName, Object> of(FLOW_ID_QNAME, FLOW_ID.getValue());
92
93     private static final Map<QName, Object> TABLE_KEY_BI = //
94     ImmutableMap.<QName, Object> of(TABLE_ID_QNAME, TABLE_ID);;
95
96     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier FLOW_INSTANCE_ID_BI = //
97     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
98             .node(Nodes.QNAME) //
99             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
100             .nodeWithKey(Table.QNAME, TABLE_KEY_BI) //
101             .nodeWithKey(Flow.QNAME, FLOW_KEY_BI) //
102             .toInstance();
103     private static final TableKey TABLE_KEY_BA = new TableKey((short) 0);
104
105     private static final InstanceIdentifier<Flow> FLOWS_PATH_BA = //
106             NODE_INSTANCE_ID_BA.builder() //
107             .augmentation(FlowCapableNode.class) //
108             .child(Table.class, TABLE_KEY_BA) //
109             .child(Flow.class) //
110             .toInstance();
111
112     private static final InstanceIdentifier<Flow> FLOW_INSTANCE_ID_BA = //
113     FLOWS_PATH_BA.firstIdentifierOf(Table.class).child(Flow.class, FLOW_KEY);
114
115     @Test
116     public void simpleModifyOperation() throws Exception {
117
118         assertNull(biDataService.readConfigurationData(FLOW_INSTANCE_ID_BI));
119
120         registerChangeListener();
121
122         CompositeNode domflow = createTestFlow();
123         DataModificationTransaction biTransaction = biDataService.beginTransaction();
124         biTransaction.putConfigurationData(FLOW_INSTANCE_ID_BI, domflow);
125         RpcResult<TransactionStatus> biResult = biTransaction.commit().get();
126         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
127         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = modificationCapture.get(1000,TimeUnit.MILLISECONDS);
128         assertNotNull(event);
129         LOG.info("Created Configuration :{}",event.getCreatedConfigurationData());
130         Flow flow = (Flow) event.getCreatedConfigurationData().get(FLOW_INSTANCE_ID_BA);
131         assertNotNull(flow);
132         assertNotNull(flow.getMatch());
133         assertEquals(TransactionStatus.COMMITED, biResult.getResult());
134
135     }
136
137     private void registerChangeListener() {
138         baDataService.registerDataChangeListener(FLOWS_PATH_BA, new DataChangeListener() {
139
140             @Override
141             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
142                 LOG.info("Data Change listener invoked.");
143                 modificationCapture.set(change);
144             }
145         });
146     }
147
148     private CompositeNode createTestFlow() {
149         FlowBuilder flow = new FlowBuilder();
150         flow.setKey(FLOW_KEY);
151         Short tableId = 0;
152         flow.setTableId(tableId);
153         MatchBuilder match = new MatchBuilder();
154         match.setIpMatch(new IpMatchBuilder().setIpProto(IpVersion.Ipv4).build());
155         Ipv4MatchBuilder ipv4Match = new Ipv4MatchBuilder();
156         // ipv4Match.setIpv4Destination(new Ipv4Prefix(cliInput.get(4)));
157         Ipv4Prefix prefix = new Ipv4Prefix("10.0.0.1/24");
158         ipv4Match.setIpv4Destination(prefix);
159         Ipv4Match i4m = ipv4Match.build();
160         match.setLayer3Match(i4m);
161         flow.setMatch(match.build());
162
163
164
165         // Create a drop action
166         /*
167          * Note: We are mishandling drop actions DropAction dropAction = new
168          * DropActionBuilder().build(); ActionBuilder ab = new ActionBuilder();
169          * ab.setAction(dropAction);
170          */
171
172         DecNwTtl decNwTtl = new DecNwTtlBuilder().build();
173         ActionBuilder ab = new ActionBuilder();
174         ActionKey actionKey = new ActionKey(0);
175         ab.setKey(actionKey );
176         ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
177
178         // Add our drop action to a list
179         List<Action> actionList = new ArrayList<Action>();
180         actionList.add(ab.build());
181
182         // Create an Apply Action
183         ApplyActionsBuilder aab = new ApplyActionsBuilder();
184         aab.setAction(actionList);
185
186         // Wrap our Apply Action in an Instruction
187         InstructionBuilder ib = new InstructionBuilder();
188         ib.setOrder(0);
189         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
190
191         // Put our Instruction in a list of Instructions
192         InstructionsBuilder isb = new InstructionsBuilder();
193         List<Instruction> instructions = new ArrayList<Instruction>();
194         instructions.add(ib.build());
195         isb.setInstruction(instructions);
196
197         // Add our instructions to the flow
198         flow.setInstructions(isb.build());
199
200         flow.setPriority(2);
201         flow.setFlowName("Foo Name");
202         CompositeNode domFlow = mappingService.toDataDom(flow.build());
203         return domFlow;
204     }
205 }

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