Fixed NAT in OFOverlay based on EIG
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / SourceMapperTest.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 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.groupbasedpolicy.dto.EgKey;
26 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
27 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
28 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
29 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
30 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
31 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
32 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
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.EndpointGroupId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
42
43 public class SourceMapperTest {
44
45     private SourceMapper mapper;
46
47     private OfContext ctx;
48     private short tableId;
49     private NodeId nodeId;
50     private PolicyInfo policyInfo;
51     private OfWriter ofWriter;
52     private Endpoint endpoint;
53     private EndpointManager endpointManager;
54     private IndexedTenant tenant;
55     private TenantId tenantId;
56     private PolicyManager policyManager;
57     private OfOverlayContext ofOverlayContext;
58     private NodeConnectorId nodeConnectorId;
59     private EndpointGroupId endpointGroupIdSingle;
60     private EndpointGroupId endpointGroupIdList;
61     private EndpointGroup endpointGroup;
62     private SwitchManager switchManager;
63
64     @Before
65     public void initialisation() {
66         ctx = mock(OfContext.class);
67         tableId = 5;
68         nodeId = mock(NodeId.class);
69         ofWriter = mock(OfWriter.class);
70
71         mapper = new SourceMapper(ctx, tableId);
72
73         endpointManager = mock(EndpointManager.class);
74         when(ctx.getEndpointManager()).thenReturn(endpointManager);
75         endpoint = mock(Endpoint.class);
76         List<Endpoint> endpointsForNode = Arrays.asList(endpoint);
77         when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(endpointsForNode);
78
79         ofOverlayContext = mock(OfOverlayContext.class);
80         when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayContext);
81         nodeConnectorId = mock(NodeConnectorId.class);
82         when(ofOverlayContext.getNodeConnectorId()).thenReturn(nodeConnectorId);
83
84         tenantId = mock(TenantId.class);
85         when(endpoint.getTenant()).thenReturn(tenantId);
86         tenant = mock(IndexedTenant.class);
87         when(ctx.getTenant(tenantId)).thenReturn(tenant);
88         policyManager = mock(PolicyManager.class);
89         when(ctx.getPolicyManager()).thenReturn(policyManager);
90         policyInfo = mock(PolicyInfo.class);
91         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
92
93         endpointGroup = mock(EndpointGroup.class);
94         when(tenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(endpointGroup);
95
96         Set<NodeId> nodeIdPeers = new HashSet<NodeId>(Arrays.asList(nodeId));
97         when(endpointManager.getNodesForGroup(any(EgKey.class))).thenReturn(nodeIdPeers);
98
99         switchManager = mock(SwitchManager.class);
100         when(ctx.getSwitchManager()).thenReturn(switchManager);
101         when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(nodeConnectorId);
102     }
103
104     @Test
105     public void constructorTest() {
106         Assert.assertEquals(tableId, mapper.getTableId());
107     }
108
109     @Test
110     public void syncTestEndpointGroup() throws Exception {
111         endpointGroupIdSingle = mock(EndpointGroupId.class);
112         when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
113         when(endpoint.getEndpointGroups()).thenReturn(null);
114
115         mapper.sync(nodeId, ofWriter);
116         verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
117     }
118
119     @Test
120     public void syncTestEndpointGroups() throws Exception {
121         endpointGroupIdList = mock(EndpointGroupId.class);
122         List<EndpointGroupId> endpointGroups = Arrays.asList(endpointGroupIdList);
123         when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
124
125         mapper.sync(nodeId, ofWriter);
126         verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
127     }
128
129     @Test
130     public void syncTestEndpointGroupPeers() throws Exception {
131         endpointGroupIdSingle = mock(EndpointGroupId.class);
132         when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
133         when(endpoint.getEndpointGroups()).thenReturn(null);
134
135         mapper.sync(nodeId, ofWriter);
136         verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
137     }
138
139     @Test
140     public void syncTestEndpointGroupTunPortNull() throws Exception {
141         endpointGroupIdSingle = mock(EndpointGroupId.class);
142         when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
143         when(endpoint.getEndpointGroups()).thenReturn(null);
144         when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
145
146         mapper.sync(nodeId, ofWriter);
147         verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
148     }
149
150     @Test
151     public void syncTestTenantNull() throws Exception {
152         when(ctx.getTenant(tenantId)).thenReturn(null);
153
154         mapper.sync(nodeId, ofWriter);
155         verify(ofWriter, times(1)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
156     }
157 }