Upgrade ietf-{inet,yang}-types to 2013-07-15
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowConverterTest.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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  * Contributor: usha.m.s@ericsson.com
9  */
10 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11
12 import java.math.BigInteger;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
44 import org.opendaylight.yangtools.yang.binding.Augmentation;
45 import org.opendaylight.yangtools.yang.binding.DataContainer;
46
47 /**
48  * test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor}
49  */
50 public class FlowConverterTest {
51
52
53     @Test
54     public void testCloneAndAugmentFlowWithSetVlanId() {
55         MockFlow mockFlow = new MockFlow();
56         Action action1 = createAction(
57             new SetVlanIdActionCaseBuilder().setSetVlanIdAction(
58                 new SetVlanIdActionBuilder().setVlanId(new VlanId(10)).build())
59                 .build(),
60             0);
61
62         mockFlow.setMatch(new MatchBuilder().setEthernetMatch(createEthernetMatch()).build());
63         mockFlow.setInstructions(toApplyInstruction(Collections.singletonList(action1)));
64
65         List<FlowModInputBuilder> flowModInputBuilders =
66             FlowConvertor.toFlowModInputs(mockFlow, OFConstants.OFP_VERSION_1_3, BigInteger.ONE);
67         Assert.assertEquals(2, flowModInputBuilders.size());
68
69     }
70
71     private static Action createAction(final org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action actionCase,
72         final int order) {
73         Action action = new ActionBuilder().setOrder(order).setAction(actionCase).build();
74         return action;
75     }
76
77     private static EthernetMatch createEthernetMatch() {
78         EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
79         ethernetMatchBuilder.setEthernetType(new EthernetTypeBuilder().setType(new EtherType(33024L)).build());
80         return ethernetMatchBuilder.build();
81     }
82
83     private static Instructions toApplyInstruction(
84         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actions) {
85         return new InstructionsBuilder()
86             .setInstruction(
87                 Collections.singletonList(
88                     new InstructionBuilder()
89                         .setOrder(0)
90                         .setInstruction(
91                             new ApplyActionsCaseBuilder()
92                                 .setApplyActions((new ApplyActionsBuilder()).setAction(actions).build())
93                                 .build()
94                         ).build())
95             ).build();
96     }
97
98     private static Flow createFlowWithActions(final Action...actions) {
99         MockFlow mockFlow = new MockFlow();
100         mockFlow.setInstructions(toApplyInstruction(Arrays.asList(actions)));
101         return mockFlow;
102     }
103
104     private static class MockFlow implements AddFlowInput {
105         private Instructions instructions;
106         private Match match;
107
108         public void setInstructions(final Instructions instructions) {
109             this.instructions = instructions;
110         }
111
112         public void setMatch(final Match match) {
113             this.match = match;
114         }
115
116
117         @Override
118         public FlowRef getFlowRef() {
119             return null;
120         }
121
122         @Override
123         public <E extends Augmentation<AddFlowInput>> E getAugmentation(final Class<E> augmentationType) {
124             return null;
125         }
126
127         @Override
128         public FlowTableRef getFlowTable() {
129             return null;
130         }
131
132         @Override
133         public Match getMatch() {
134             return match;
135         }
136
137         @Override
138         public Instructions getInstructions() {
139             return instructions;
140         }
141
142         @Override
143         public String getContainerName() {
144             return null;
145         }
146
147         @Override
148         public FlowCookie getCookieMask() {
149             return null;
150         }
151
152         @Override
153         public Long getBufferId() {
154             return null;
155         }
156
157         @Override
158         public BigInteger getOutPort() {
159             return null;
160         }
161
162         @Override
163         public Long getOutGroup() {
164             return null;
165         }
166
167         @Override
168         public FlowModFlags getFlags() {
169             return null;
170         }
171
172         @Override
173         public String getFlowName() {
174             return null;
175         }
176
177         @Override
178         public Boolean isInstallHw() {
179             return null;
180         }
181
182         @Override
183         public Boolean isBarrier() {
184             return null;
185         }
186
187         @Override
188         public Boolean isStrict() {
189             return null;
190         }
191
192         @Override
193         public Integer getPriority() {
194             return null;
195         }
196
197         @Override
198         public Integer getIdleTimeout() {
199             return null;
200         }
201
202         @Override
203         public Integer getHardTimeout() {
204             return null;
205         }
206
207         @Override
208         public FlowCookie getCookie() {
209             return null;
210         }
211
212         @Override
213         public Short getTableId() {
214             return null;
215         }
216
217         @Override
218         public NodeRef getNode() {
219             return null;
220         }
221
222         @Override
223         public Uri getTransactionUri() {
224             return null;
225         }
226
227         @Override
228         public Class<? extends DataContainer> getImplementedInterface() {
229             return null;
230         }
231     }
232
233 }