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