GBP coexistence - OF renderer can remove only its own flows from node
[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.Mockito.inOrder;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.HashMap;
21 import java.util.concurrent.ScheduledExecutorService;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.groupbasedpolicy.dto.EgKey;
33 import org.opendaylight.groupbasedpolicy.dto.EpKey;
34 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2ContextId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 import com.google.common.base.Optional;
47 import com.google.common.util.concurrent.CheckedFuture;
48
49 public class PolicyManagerTest {
50
51     // constant values used by the tested class implementation
52     private static final short TABLEID_PORTSECURITY = 0;
53     private static final short TABLEID_INGRESS_NAT = 1;
54     private static final short TABLEID_SOURCE_MAPPER = 2;
55     private static final short TABLEID_DESTINATION_MAPPER = 3;
56     private static final short TABLEID_POLICY_ENFORCER = 4;
57     private static final short TABLEID_EGRESS_NAT = 5;
58     private static final short TABLEID_EXTERNAL_MAPPER = 6;
59
60     private PolicyManager manager;
61
62     private DataBroker dataBroker;
63     private SwitchManager switchManager;
64     private short tableOffset;
65
66     private ReadWriteTransaction readWriteTransaction;
67
68     private NodeId nodeId;
69     private short tableId;
70     private Flow flow;
71
72     @Before
73     public void setUp() {
74         EndpointManager endpointManager = mock(EndpointManager.class);
75         ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
76         dataBroker = mock(DataBroker.class);
77         switchManager = mock(SwitchManager.class);
78         tableOffset = 5;
79
80         WriteTransaction writeTransaction = mock(WriteTransaction.class);
81         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
82
83         readWriteTransaction = mock(ReadWriteTransaction.class);
84         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
85
86         manager = new PolicyManager(dataBroker, switchManager,
87                 endpointManager, executor, tableOffset);
88
89         nodeId = mock(NodeId.class);
90         tableId = 5;
91         flow = mock(Flow.class);
92     }
93
94     @SuppressWarnings("unchecked")
95     @Test
96     public void flowMapTestAddition() throws Exception {
97         OfWriter flowMap = new OfWriter();
98         flowMap.writeFlow(nodeId, tableId, flow);
99
100         Optional<Table> optional = mock(Optional.class);
101         CheckedFuture<Optional<Table>, ReadFailedException> readFuture = mock(CheckedFuture.class);
102         when(readWriteTransaction.read(any(LogicalDatastoreType.class),
103                 any(InstanceIdentifier.class))).thenReturn(readFuture);
104         when(readFuture.get()).thenReturn(optional);
105         when(optional.isPresent()).thenReturn(true);
106         Table currentTable = mock(Table.class);
107         when(optional.get()).thenReturn(currentTable);
108
109         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = mock(CheckedFuture.class);
110         when(readWriteTransaction.submit()).thenReturn(submitFuture);
111
112         flowMap.commitToDataStore(dataBroker, new HashMap<InstanceIdentifier<Table>, TableBuilder>());
113
114         InOrder orderCheck = inOrder(readWriteTransaction);
115         orderCheck.verify(readWriteTransaction).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
116                 any(Flow.class), any(Boolean.class));
117         orderCheck.verify(readWriteTransaction).submit();
118     }
119
120     @Test
121     public void changeOpenFlowTableOffsetTest() throws Exception {
122         short tableOffset = 3;
123         assertTrue(manager.changeOpenFlowTableOffset(tableOffset) != null);
124         verify(switchManager, times(7)).getReadySwitches();
125     }
126
127     @Test
128     public void groupEndpointUpdatedTest() throws Exception {
129         EgKey egKey = new EgKey(
130                 new TenantId("9040b0be-15a0-40ee-a6ca-b19b454c7697"),
131                 new EndpointGroupId("dc630fd5-5ca3-42ea-8baa-aa80a38df5e3"));
132         EpKey epKey = new EpKey(
133                 new L2ContextId("10fdfde9-c0f2-412d-822d-59d38711bde8"),
134                 new MacAddress("24:77:03:D8:E9:B4"));
135         // TODO finish this test
136     }
137
138     @Test
139     public void verifyMaxTableIdTest() throws Exception {
140         short tableOffset = 255 - TABLEID_EXTERNAL_MAPPER;
141         assertEquals(255, manager.verifyMaxTableId(tableOffset).getValue().shortValue());
142     }
143
144     @Test(expected = IllegalArgumentException.class)
145     public void verifyMaxTableIdTestInvalidTableOffset() throws Exception {
146         short tableOffset = 255 - TABLEID_EXTERNAL_MAPPER + 1;
147         assertEquals(255, manager.verifyMaxTableId(tableOffset).getValue().shortValue());
148     }
149
150     @Test
151     public void getTableIdsTest() {
152         assertEquals(tableOffset + TABLEID_PORTSECURITY, manager.getTABLEID_PORTSECURITY());
153         assertEquals(tableOffset + TABLEID_INGRESS_NAT, manager.getTABLEID_INGRESS_NAT());
154         assertEquals(tableOffset + TABLEID_SOURCE_MAPPER, manager.getTABLEID_SOURCE_MAPPER());
155         assertEquals(tableOffset + TABLEID_DESTINATION_MAPPER, manager.getTABLEID_DESTINATION_MAPPER());
156         assertEquals(tableOffset + TABLEID_POLICY_ENFORCER, manager.getTABLEID_POLICY_ENFORCER());
157         assertEquals(tableOffset + TABLEID_EGRESS_NAT, manager.getTABLEID_EGRESS_NAT());
158         assertEquals(tableOffset + TABLEID_EXTERNAL_MAPPER, manager.getTABLEID_EXTERNAL_MAPPER());
159     }
160 }