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