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