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