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