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