BUG-2794: incorporate openflowjava api changes to openflowplugin
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / ActionSetNwSrcReactorTest.java
1 /**
2  * Copyright (c) 2013 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.openflowplugin.openflow.md.core.sal.convertor.action;
9
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.openflowplugin.api.OFConstants;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.IpAddressAction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmFieldsAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4SrcCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv6SrcCase;
30 import java.math.BigInteger;
31
32 /**
33  * match conversion and injection test
34  */
35 public class ActionSetNwSrcReactorTest {
36
37     private Address[] addresses;
38
39     /**
40      * prepare input match
41      */
42     @Before
43     public void setUp() {
44         addresses = new Address[]{
45                 new Ipv4Builder().setIpv4Address(new Ipv4Prefix("10.0.10.1/32")).build(),
46                 new Ipv4Builder().setIpv4Address(new Ipv4Prefix("10.0.10.1/16")).build(),
47                 new Ipv6Builder().setIpv6Address(new Ipv6Prefix("1234:5678:9abc:def1:2345:6789:abcd:ef12")).build(),
48                 new Ipv6Builder().setIpv6Address(new Ipv6Prefix("1234:5678:9abc:def1:2345:6789:abcd:ef12/42")).build(),
49         };
50     }
51
52     /**
53      * convert for OF-1.3, inject into {@link ActionBuilder}
54      */
55
56     @Test
57     public void testMatchConvertorV13_flow() {
58         ActionBuilder target = new ActionBuilder();
59         for (Address address : addresses) {
60             SetNwSrcActionCase action = prepareSetNwSrcActionCase(address);
61             ActionSetNwSrcReactor.getInstance().convert(action,
62                     OFConstants.OFP_VERSION_1_3, target, BigInteger.ONE);
63             MatchEntry mEntry = target.getAugmentation(OxmFieldsAction.class).getMatchEntry().get(0);
64             Assert.assertNotNull(mEntry);
65             if (address instanceof Ipv4) {
66                 Ipv4SrcCase ipv4SrcCase = ((Ipv4SrcCase) mEntry.getMatchEntryValue());
67                 Assert.assertNotNull(ipv4SrcCase.getIpv4Src());
68             } else if (address instanceof Ipv6) {
69                 Ipv6SrcCase ipv6SrcCase = ((Ipv6SrcCase) mEntry.getMatchEntryValue());
70                 Assert.assertNotNull(ipv6SrcCase.getIpv6Src().getIpv6Address());
71             } else {
72                 Assert.fail("not tested yet: " + address.getClass().getName());
73             }
74         }
75     }
76
77     /**
78      * @param address
79      * @return
80      */
81     private static SetNwSrcActionCase prepareSetNwSrcActionCase(Address address) {
82         return new SetNwSrcActionCaseBuilder().setSetNwSrcAction(
83                 new SetNwSrcActionBuilder().setAddress(address).build()).build();
84     }
85
86     /**
87      * convert for OF-1.0, inject into {@link ActionBuilder}
88      */
89     @Test
90     public void testMatchConvertorV10_flow() {
91         ActionBuilder target = new ActionBuilder();
92         for (Address address : addresses) {
93             SetNwSrcActionCase action = prepareSetNwSrcActionCase(address);
94
95             if (address instanceof Ipv4) {
96                 ActionSetNwSrcReactor.getInstance().convert(action,
97                         OFConstants.OFP_VERSION_1_0, target, BigInteger.ONE);
98                 Assert.assertNotNull(target.getAugmentation(IpAddressAction.class).getIpAddress());
99             } else {
100                 try {
101                     ActionSetNwSrcReactor.getInstance().convert(action,
102                             OFConstants.OFP_VERSION_1_0, target, BigInteger.ONE);
103                     Assert.fail("address of this type must not pass the reactor: " + address.getClass().getName());
104                 } catch (Exception e) {
105                     //expected
106                     Assert.assertEquals(IllegalArgumentException.class, e.getClass());
107                 }
108             }
109         }
110     }
111 }