Bug 3302: fix for GroupTable
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / EgressNatMapperTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.util.Arrays;
18
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.groupbasedpolicy.endpoint.EpKey;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointManager;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
27 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
28 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.napt.translations.fields.napt.translations.NaptTranslation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39
40 public class EgressNatMapperTest {
41
42     private EgressNatMapper mapper;
43
44     private NodeId nodeId;
45     private PolicyInfo policyInfo;
46     private OfWriter ofWriter;
47
48     private IpAddress ipAddressNapt;
49     private IpAddress ipAddressL3Ep;
50
51     private static final short TABLE_ID = (short) 5;
52     private static final String IPV4_ADDRESS = "127.0.0.1";
53     private static final String MAC_ADDRESS = "FF:FF:FF:FF:FF:FF";
54     private static final String IPV6_ADDRESS = "0:0:0:0:0:0:0:1";
55
56     @Before
57     public void initialisation() {
58         OfContext ctx = mock(OfContext.class);
59
60         EndpointManager endpointManager = mock(EndpointManager.class);
61         when(ctx.getEndpointManager()).thenReturn(endpointManager);
62
63         // endpointL3
64         EndpointL3 endpointL3 = mock(EndpointL3.class);
65         when(endpointManager.getL3EndpointsWithNat()).thenReturn(Arrays.asList(endpointL3));
66         ipAddressL3Ep = mock(IpAddress.class);
67         when(endpointL3.getIpAddress()).thenReturn(ipAddressL3Ep);
68         Ipv4Address ipv4AddressL3Ep = mock(Ipv4Address.class);
69         when(ipAddressL3Ep.getIpv4Address()).thenReturn(ipv4AddressL3Ep);
70         when(ipv4AddressL3Ep.getValue()).thenReturn(IPV4_ADDRESS);
71         L2BridgeDomainId l2BridgeDomainId = mock(L2BridgeDomainId.class);
72         when(endpointL3.getL2Context()).thenReturn(l2BridgeDomainId);
73         MacAddress macAddress = mock(MacAddress.class);
74         when(endpointL3.getMacAddress()).thenReturn(macAddress);
75         when(macAddress.getValue()).thenReturn(MAC_ADDRESS);
76         Ipv6Address ipv6AddressL3Ep = mock(Ipv6Address.class);
77         when(ipAddressL3Ep.getIpv6Address()).thenReturn(ipv6AddressL3Ep);
78         when(ipv6AddressL3Ep.getValue()).thenReturn(IPV6_ADDRESS);
79
80         Endpoint endpoint = mock(Endpoint.class);
81         when(endpointManager.getEndpoint(any(EpKey.class))).thenReturn(endpoint);
82         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(Arrays.asList(endpoint));
83
84         // createNatFlow
85         NaptTranslation nat = mock(NaptTranslation.class);
86         when(endpointManager.getNaptAugL3Endpoint(any(EndpointL3.class))).thenReturn(Arrays.asList(nat));
87         ipAddressNapt = mock(IpAddress.class);
88         when(nat.getIpAddress()).thenReturn(ipAddressNapt);
89         Ipv4Address ipv4AddressNapt = mock(Ipv4Address.class);
90         when(ipAddressNapt.getIpv4Address()).thenReturn(ipv4AddressNapt);
91         when(ipv4AddressNapt.getValue()).thenReturn(IPV4_ADDRESS);
92         Ipv6Address ipv6AddressNapt = mock(Ipv6Address.class);
93         when(ipAddressNapt.getIpv6Address()).thenReturn(ipv6AddressNapt);
94         when(ipv6AddressNapt.getValue()).thenReturn(IPV6_ADDRESS);
95
96         // buildNatFlow
97         PolicyManager policyManager = mock(PolicyManager.class);
98         when(ctx.getPolicyManager()).thenReturn(policyManager);
99         when(policyManager.getTABLEID_DESTINATION_MAPPER()).thenReturn(TABLE_ID);
100
101         // EndpointFwdCtxOrdinals
102         PolicyResolver policyResolver = mock(PolicyResolver.class);
103         when(ctx.getPolicyResolver()).thenReturn(policyResolver);
104
105         nodeId = mock(NodeId.class);
106         policyInfo = mock(PolicyInfo.class);
107         ofWriter = mock(OfWriter.class);
108
109         mapper = new EgressNatMapper(ctx, TABLE_ID);
110     }
111
112     @Test
113     public void getTableIdTest() {
114         Assert.assertEquals(TABLE_ID, mapper.getTableId());
115     }
116
117     @Test
118     public void syncTestIpv4() throws Exception {
119         mapper.sync(nodeId, policyInfo, ofWriter);
120         verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
121     }
122
123     @Test
124     public void syncTestIpv6() throws Exception {
125         when(ipAddressNapt.getIpv4Address()).thenReturn(null);
126         mapper.sync(nodeId, policyInfo, ofWriter);
127         verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
128     }
129
130 }