Open Kilda Java Documentation
NetworkCacheTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.pce.cache;
17 
18 import static org.junit.Assert.assertEquals;
19 
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Set;
33 
34 public class NetworkCacheTest {
35  private final NetworkCache networkCache = new NetworkCache();
36 
37  @Before
38  public void setUp() throws Exception {
39  }
40 
41  @After
42  public void tearDown() throws Exception {
43  networkCache.clear();
44  }
45 
46  @Test
47  public void getSwitch() throws Exception {
50  assertEquals(NetworkTopologyConstants.sw1,
52  assertEquals(NetworkTopologyConstants.sw2,
54  }
55 
56  @Test
57  public void createSwitch() throws Exception {
60  assertEquals(2, networkCache.dumpSwitches().size());
61  }
62 
63  @Test
64  public void updateSwitch() throws Exception {
65  SwitchId swId = new SwitchId("ff:07");
66  SwitchInfoData sw7 = new SwitchInfoData(swId, SwitchState.ACTIVATED, "", "", "", "");
67  networkCache.createSwitch(sw7);
68  assertEquals(sw7, networkCache.getSwitch(swId));
69 
70  SwitchInfoData sw7updated = new SwitchInfoData(swId, SwitchState.ACTIVATED, "", "", "", "");
71  networkCache.updateSwitch(sw7updated);
72  assertEquals(sw7updated, networkCache.getSwitch(swId));
73 
74  networkCache.deleteSwitch(swId);
75  Set<SwitchInfoData> switches = networkCache.dumpSwitches();
76  assertEquals(Collections.emptySet(), switches);
77  }
78 
79  @Test
80  public void createOrUpdateSwitch() throws Exception {
83 
84  assertEquals(2, networkCache.dumpSwitches().size());
85 
88 
89  assertEquals(2, networkCache.dumpSwitches().size());
90  }
91 
92  @Test
93  public void deleteSwitch() throws Exception {
94  createSwitch();
97  assertEquals(0, networkCache.dumpSwitches().size());
98  }
99 
100  @Test
101  public void dumpSwitches() throws Exception {
102  createSwitch();
103  Set<SwitchInfoData> switches = networkCache.dumpSwitches();
104  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.sw1,
105  NetworkTopologyConstants.sw2)), switches);
106  }
107 
108  @Test
109  public void getStateSwitches() throws Exception {
113  Set<SwitchInfoData> activeSwitches = networkCache.getStateSwitches(SwitchState.ACTIVATED);
114  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.sw1,
115  NetworkTopologyConstants.sw2)), activeSwitches);
116  }
117 
118  @Test
119  public void getControllerSwitches() throws Exception {
123  Set<SwitchInfoData> activeSwitches = networkCache.getControllerSwitches("localhost");
124  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.sw1,
125  NetworkTopologyConstants.sw2)), activeSwitches);
126  }
127 
128  @Test
129  public void getDirectlyConnectedSwitches() throws Exception {
133 
134  Set<SwitchInfoData> directlyConnected = networkCache.getDirectlyConnectedSwitches(
136  assertEquals(new HashSet<>(), directlyConnected);
137 
142 
143  directlyConnected = networkCache.getDirectlyConnectedSwitches(NetworkTopologyConstants.sw2.getSwitchId());
144  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.sw1, NetworkTopologyConstants.sw3)),
145  directlyConnected);
146  }
147 
148  @Test
149  public void createOrUpdateIsl() throws Exception {
153 
158 
159  assertEquals(4, networkCache.dumpIsls().size());
160 
165 
166  assertEquals(4, networkCache.dumpIsls().size());
167  }
168 
169  @Test
170  public void deleteIsl() throws Exception {
172 
173  networkCache.deleteIsl(NetworkTopologyConstants.isl12.getId());
174  networkCache.deleteIsl(NetworkTopologyConstants.isl21.getId());
175  networkCache.deleteIsl(NetworkTopologyConstants.isl23.getId());
176  networkCache.deleteIsl(NetworkTopologyConstants.isl32.getId());
177 
178  assertEquals(0, networkCache.dumpIsls().size());
179  }
180 
181  @Test
182  public void getIsl() throws Exception {
184  assertEquals(NetworkTopologyConstants.isl12, networkCache.getIsl(NetworkTopologyConstants.isl12.getId()));
185  assertEquals(NetworkTopologyConstants.isl21, networkCache.getIsl(NetworkTopologyConstants.isl21.getId()));
186  assertEquals(NetworkTopologyConstants.isl23, networkCache.getIsl(NetworkTopologyConstants.isl23.getId()));
187  assertEquals(NetworkTopologyConstants.isl32, networkCache.getIsl(NetworkTopologyConstants.isl32.getId()));
188  }
189 
190  @Test
191  public void dumpIsls() throws Exception {
193  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.isl12, NetworkTopologyConstants.isl21,
195  }
196 
197  @Test
198  public void getIslsBySwitch() throws Exception {
205  assertEquals(new HashSet<>(Arrays.asList(
209  }
210 
211  @Test
212  public void getIslsBySource() throws Exception {
219  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.isl21,
222  }
223 
224  @Test
225  public void getIslsByDestination() throws Exception {
232  assertEquals(new HashSet<>(Arrays.asList(NetworkTopologyConstants.isl12,
235  }
236 }
IslInfoData deleteIsl(String islId)
Set< SwitchInfoData > getControllerSwitches(String controller)
Set< SwitchInfoData > getStateSwitches(SwitchState state)
SwitchInfoData updateSwitch(SwitchInfoData newSwitch)
Set< IslInfoData > getIslsBySource(SwitchId switchId)
IslInfoData createOrUpdateIsl(IslInfoData isl)
Set< SwitchInfoData > getDirectlyConnectedSwitches(SwitchId switchId)
Set< IslInfoData > getIslsByDestination(SwitchId switchId)
SwitchInfoData deleteSwitch(SwitchId switchId)
SwitchInfoData createSwitch(SwitchInfoData newSwitch)
Set< IslInfoData > getIslsBySwitch(SwitchId switchId)
Set< SwitchInfoData > dumpSwitches()
IslInfoData getIsl(String islId)
SwitchInfoData getSwitch(SwitchId switchId)
SwitchInfoData createOrUpdateSwitch(SwitchInfoData newSwitch)