0d87587503569526bdc5ca573ba0933bb363f146
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / PolicyManagerTest.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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.atLeastOnce;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.inOrder;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import java.util.concurrent.ScheduledExecutorService;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InOrder;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.groupbasedpolicy.endpoint.EpKey;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
38 import org.opendaylight.groupbasedpolicy.resolver.ActionInstanceValidator;
39 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
40 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
41 import org.opendaylight.groupbasedpolicy.resolver.PolicyScope;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionDefinitionId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2ContextId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.SubjectFeatureDefinitions;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52
53 import com.google.common.base.Optional;
54 import com.google.common.util.concurrent.CheckedFuture;
55 import com.google.common.util.concurrent.ListenableFuture;
56
57 public class PolicyManagerTest {
58
59     // constant values used by the tested class implementation
60     private static final short TABLEID_PORTSECURITY = 0;
61     private static final short TABLEID_INGRESS_NAT =  1;
62     private static final short TABLEID_SOURCE_MAPPER = 2;
63     private static final short TABLEID_DESTINATION_MAPPER = 3;
64     private static final short TABLEID_POLICY_ENFORCER = 4;
65     private static final short TABLEID_EGRESS_NAT = 5;
66     private static final short TABLEID_EXTERNAL_MAPPER = 6;
67
68     private PolicyManager manager;
69
70     private DataBroker dataBroker;
71     private PolicyResolver policyResolver;
72     private SwitchManager switchManager;
73     private EndpointManager endpointManager;
74     private RpcProviderRegistry rpcRegistry;
75     private ScheduledExecutorService executor;
76     private short tableOffset;
77
78     private WriteTransaction writeTransaction;
79     private ReadWriteTransaction readWriteTransaction;
80
81     private NodeId nodeId;
82     private short tableId;
83     private Flow flow;
84
85     private PolicyScope policyScope;
86
87     @Before
88     public void setUp() {
89         dataBroker = mock(DataBroker.class);
90         policyResolver = mock(PolicyResolver.class);
91         switchManager = mock(SwitchManager.class);
92         endpointManager = mock(EndpointManager.class);
93         rpcRegistry = mock(RpcProviderRegistry.class);
94         executor = mock(ScheduledExecutorService.class);
95         tableOffset = 5;
96
97         policyScope = mock(PolicyScope.class);
98         when(policyResolver.registerListener(any(PolicyManager.class))).thenReturn(policyScope);
99
100         writeTransaction = mock(WriteTransaction.class);
101         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
102
103         readWriteTransaction = mock(ReadWriteTransaction.class);
104         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
105
106         manager = new PolicyManager(dataBroker, policyResolver, switchManager,
107                 endpointManager, rpcRegistry, executor, tableOffset);
108
109         nodeId = mock(NodeId.class);
110         tableId = 5;
111         flow = mock(Flow.class);
112     }
113
114     @SuppressWarnings("unchecked")
115     @Test
116     public void constructorTest() {
117         WriteTransaction ctorWriteTransaction = mock(WriteTransaction.class);
118         when(dataBroker.newWriteOnlyTransaction()).thenReturn(ctorWriteTransaction);
119         PolicyResolver ctorPolicyResolver = mock(PolicyResolver.class);
120         PolicyManager ctorPolicyManager = new PolicyManager(dataBroker, ctorPolicyResolver,
121                 switchManager, endpointManager, rpcRegistry, executor, tableOffset);
122         verify(ctorPolicyResolver, atLeastOnce()).registerActionDefinitions(any(ActionDefinitionId.class),
123                 any(ActionInstanceValidator.class));
124     }
125
126     @SuppressWarnings("unchecked")
127     @Test
128     public void flowMapTestAddition() throws Exception {
129         OfWriter flowMap = new OfWriter();
130         flowMap.writeFlow(nodeId, tableId, flow);
131
132         Optional<Table> optional = mock(Optional.class);
133         CheckedFuture<Optional<Table>, ReadFailedException> readFuture = mock(CheckedFuture.class);
134         when(readWriteTransaction.read(any(LogicalDatastoreType.class),
135                 any(InstanceIdentifier.class))).thenReturn(readFuture);
136         when(readFuture.get()).thenReturn(optional);
137         when(optional.isPresent()).thenReturn(true);
138         Table currentTable = mock(Table.class);
139         when(optional.get()).thenReturn(currentTable);
140
141         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = mock(CheckedFuture.class);
142         when(readWriteTransaction.submit()).thenReturn(submitFuture);
143
144         flowMap.commitToDataStore(dataBroker);
145
146         InOrder orderCheck = inOrder(readWriteTransaction);
147         orderCheck.verify(readWriteTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
148         orderCheck.verify(readWriteTransaction).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
149                 any(Flow.class), any(Boolean.class));
150         orderCheck.verify(readWriteTransaction).submit();
151     }
152
153     @Test
154     public void changeOpenFlowTableOffsetTest() throws Exception {
155         short tableOffset = 3;
156         assertTrue(manager.changeOpenFlowTableOffset(tableOffset) instanceof ListenableFuture<?>);
157         verify(switchManager, times(7)).getReadySwitches();
158     }
159
160     @Test
161     public void groupEndpointUpdatedTest() throws Exception {
162         EgKey egKey = new EgKey(
163                 new TenantId("9040b0be-15a0-40ee-a6ca-b19b454c7697"),
164                 new EndpointGroupId("dc630fd5-5ca3-42ea-8baa-aa80a38df5e3"));
165         EpKey epKey = new EpKey(
166                 new L2ContextId("10fdfde9-c0f2-412d-822d-59d38711bde8"),
167                 new MacAddress("24:77:03:D8:E9:B4"));
168         doNothing().when(policyScope).addToScope(eq(egKey.getTenantId()), eq(egKey.getEgId()));
169         manager.groupEndpointUpdated(egKey, epKey);
170         verify(policyScope).addToScope(eq(egKey.getTenantId()), eq(egKey.getEgId()));
171     }
172
173     @Test
174     public void verifyMaxTableIdTest() throws Exception {
175         short tableOffset = 255 - TABLEID_EXTERNAL_MAPPER;
176         assertEquals(255, manager.verifyMaxTableId(tableOffset).getValue().shortValue());
177     }
178
179     @Test(expected = IllegalArgumentException.class)
180     public void verifyMaxTableIdTestInvalidTableOffset() throws Exception {
181         short tableOffset = 255 - TABLEID_EXTERNAL_MAPPER + 1;
182         assertEquals(255, manager.verifyMaxTableId(tableOffset).getValue().shortValue());
183     }
184
185     @Test
186     public void getTableIdsTest() {
187         assertEquals(tableOffset + TABLEID_PORTSECURITY, manager.getTABLEID_PORTSECURITY());
188         assertEquals(tableOffset + TABLEID_INGRESS_NAT, manager.getTABLEID_INGRESS_NAT());
189         assertEquals(tableOffset + TABLEID_SOURCE_MAPPER, manager.getTABLEID_SOURCE_MAPPER());
190         assertEquals(tableOffset + TABLEID_DESTINATION_MAPPER, manager.getTABLEID_DESTINATION_MAPPER());
191         assertEquals(tableOffset + TABLEID_POLICY_ENFORCER, manager.getTABLEID_POLICY_ENFORCER());
192         assertEquals(tableOffset + TABLEID_EGRESS_NAT, manager.getTABLEID_EGRESS_NAT());
193         assertEquals(tableOffset + TABLEID_EXTERNAL_MAPPER, manager.getTABLEID_EXTERNAL_MAPPER());
194     }
195 }