Merge remote-tracking branch 'origin/master' into merge-branch
[netvirt.git] / neutron / src / test / java / org / opendaylight / ovsdb / neutron / AdminConfigManagerTest.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Hewlett-Packard Development Company, L.P. and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Dave Tucker (HP) - Added unit tests for the AdminConfigManager class.
10  *    Sam Hague - Added unit tests for getPhysicalInterfaceName.
11  *******************************************************************************/
12
13 package org.opendaylight.ovsdb.neutron;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNull;
17 import static org.mockito.Matchers.any;
18 import static org.mockito.Matchers.anyObject;
19 import static org.mockito.Matchers.anyString;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 import java.net.InetAddress;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.opendaylight.controller.sal.core.Node;
32 import org.opendaylight.controller.sal.utils.ServiceHelper;
33 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
34 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
35 import org.opendaylight.ovsdb.lib.table.Table;
36 import org.opendaylight.ovsdb.plugin.ConfigurationService;
37 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest(ServiceHelper.class)
44 public class AdminConfigManagerTest {
45
46     AdminConfigManager adminConfigManager;
47     private OVSDBConfigService ovsdbConfig;
48     private Node node;
49     private Open_vSwitch ovsTable;
50     private ConcurrentMap<String, Table<?>> ovsMap;
51     private OvsDBMap map;
52
53     private static String OPENVSWITCH = "Open_vSwitch";
54     private static String PROVIDER_MAPPINGS = "provider_mappings";
55     private static String PHYSNET1 = "physnet1";
56     private static String ETH1 = "eth1";
57     private static String PHYSNET2 = "physnet2";
58     private static String ETH2 = "eth2";
59     private static String PHYSNET3 = "physnet3";
60     private static String ETH3 = "eth3";
61     private static String LOCAL_IP = "local_ip";
62     private static String IPADDR = "10.10.10.10";
63
64     @Before
65     public void setUp(){
66         adminConfigManager = new AdminConfigManager();
67
68         node = mock(Node.class);
69         ovsdbConfig = mock(ConfigurationService.class);
70         PowerMockito.mockStatic(ServiceHelper.class);
71         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
72
73         ovsTable = new Open_vSwitch();
74         ovsMap = new ConcurrentHashMap<>();
75         map = new OvsDBMap();
76     }
77
78     @Test
79     public void testGetTunnelEndpoint() throws Exception {
80         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
81
82         Node mockNode = mock(Node.class);
83
84         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
85
86         Open_vSwitch ovsTable = new Open_vSwitch();
87         OvsDBMap localIp = new OvsDBMap();
88         localIp.put("local_ip", "10.10.10.10");
89         ovsTable.setOther_config(localIp);
90         ovsMap.put("Open_vSwitch", ovsTable);
91
92         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
93         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
94                                                                .thenReturn(ovsMap);
95
96         PowerMockito.mockStatic(ServiceHelper.class);
97         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
98
99         // OVSDBConfigService is null
100         assertEquals(null, adminConfigManager.getTunnelEndPoint(mockNode));
101
102         // Success...
103         assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
104     }
105
106     @Test
107     public void testGetTunnelEndpointWithNullRows() throws Exception {
108         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
109
110         Node mockNode = mock(Node.class);
111
112         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
113
114         Open_vSwitch nullRow = new Open_vSwitch();
115         Open_vSwitch ovsRow1 = new Open_vSwitch();
116         Open_vSwitch ovsRow2 = new Open_vSwitch();
117         OvsDBMap invalidLocalIp = new OvsDBMap();
118         OvsDBMap localIp = new OvsDBMap();
119
120         ovsRow1.setOther_config(invalidLocalIp);
121
122         localIp.put("local_ip","10.10.10.10");
123         ovsRow2.setOther_config(localIp);
124
125         ovsMap.put("0", nullRow);
126         ovsMap.put("1", ovsRow1);
127         ovsMap.put("2", ovsRow2);
128
129         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
130         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
131
132         PowerMockito.mockStatic(ServiceHelper.class);
133         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
134
135         // Success...
136         assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
137     }
138
139     // Add key:value pairs to the map.
140     // Calling again with the same key will overwrite the current pair.
141     private void initMap (String key, String value) {
142         map.put(key, value);
143         ovsTable.setOther_config(map);
144         ovsMap.put(OPENVSWITCH, ovsTable);
145     }
146
147     @Test
148     public void testGetPhysicalInterfaceName () throws Exception {
149         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
150
151         // Check if match can be found with a single pair
152         initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1);
153         assertEquals("Failed to find " + ETH1 + " in " + map.toString(),
154                 ETH1, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
155
156         // Check if match can be found with different pairs
157         initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2);
158         assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
159                 ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
160
161         // Check if match can be found with duplicate pairs
162         initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2 + "," + PHYSNET2 + ":" + ETH2);
163         assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
164                 ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
165
166         // Check if match can be found with multiple pairs and extra other_config
167         initMap(LOCAL_IP, IPADDR);
168         assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
169                 ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
170     }
171
172     @Test
173     public void testGetPhysicalInterfaceNameNegative () throws Exception {
174         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
175                 .thenReturn(ovsMap);
176
177         // Add a null row, an empty row and a good row to the table
178         Open_vSwitch nullRow = new Open_vSwitch();
179         Open_vSwitch emptyRow = new Open_vSwitch();
180         OvsDBMap emptyProviderMap = new OvsDBMap();
181         emptyRow.setOther_config(emptyProviderMap);
182         ovsMap.put("0", nullRow);
183         ovsMap.put("1", emptyRow);
184         initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1);
185
186         // Check if no rows/no table is handled
187         assertEquals("Failed to return null when ovsdb table is null",
188                 null, adminConfigManager.getTunnelEndPoint(node));
189
190         // Check if the null and empty rows are ignored
191         System.out.println("map = " + map.toString());
192         System.out.println("ovsMap = " + ovsMap.toString());
193         assertEquals("Failed to find " + ETH1 + " in " + map.toString(),
194                 ETH1, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
195
196         // Should not be able to find match
197         initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2);
198         assertNull("Found " + ETH3 + " in " + map.toString(),
199                 adminConfigManager.getPhysicalInterfaceName(node, PHYSNET3));
200
201         // Should not be able to find match with mal-formed values
202         initMap(PROVIDER_MAPPINGS, PHYSNET1 + "-" + ETH1);
203         assertNull("Found " + ETH1 + " in " + map.toString(),
204                 adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
205     }
206 }