blob: 6b146ca09d061590757b452e254b949461a8330f [file] [log] [blame]
Ray Milkey6688cd82014-03-11 16:40:46 -07001package net.onrc.onos.intent.runtime;
2
3import net.floodlightcontroller.core.module.FloodlightModuleContext;
4import net.floodlightcontroller.core.module.FloodlightModuleException;
5import net.onrc.onos.datagrid.IDatagridService;
6import net.onrc.onos.datagrid.IEventChannel;
7import net.onrc.onos.datagrid.IEventChannelListener;
8import net.onrc.onos.intent.*;
9import net.onrc.onos.intent.Intent.IntentState;
10import net.onrc.onos.intent.IntentOperation.Operator;
Ray Milkey6688cd82014-03-11 16:40:46 -070011import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
12import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
13import net.onrc.onos.registry.controller.IControllerRegistryService;
14
15import org.junit.After;
16import org.junit.Before;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.powermock.api.easymock.PowerMock;
20import org.powermock.core.classloader.annotations.PrepareForTest;
21import org.powermock.modules.junit4.PowerMockRunner;
22
23import java.util.Collection;
24
25import static org.easymock.EasyMock.*;
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.*;
28
29import org.hamcrest.Matchers;
30
31/**
32 * @author Ray Milkey (ray@onlab.us)
33 */
34@RunWith(PowerMockRunner.class)
35@PrepareForTest(PathCalcRuntimeModule.class)
36public class PathCalcRuntimeModuleTest {
37 private FloodlightModuleContext modContext;
38 private IDatagridService datagridService;
39 private INetworkGraphService networkGraphService;
40 private IControllerRegistryService controllerRegistryService;
41 private PersistIntent persistIntent;
42
43 @SuppressWarnings("unchecked")
44 @Before
45 public void setUp() throws Exception {
46 final MockNetworkGraph graph = new MockNetworkGraph();
47 graph.createSampleTopology1();
48
49 datagridService = createMock(IDatagridService.class);
50 networkGraphService = createMock(INetworkGraphService.class);
51 controllerRegistryService = createMock(IControllerRegistryService.class);
52 modContext = createMock(FloodlightModuleContext.class);
53 final IEventChannel eventChannel = createMock(IEventChannel.class);
54 persistIntent = PowerMock.createMock(PersistIntent.class);
55
56 PowerMock.expectNew(PersistIntent.class,
57 anyObject(IControllerRegistryService.class),
58 anyObject(INetworkGraphService.class)).andReturn(persistIntent);
59
60 expect(modContext.getServiceImpl(IDatagridService.class))
61 .andReturn(datagridService).once();
62 expect(modContext.getServiceImpl(INetworkGraphService.class))
63 .andReturn(networkGraphService).once();
64 expect(modContext.getServiceImpl(IControllerRegistryService.class))
65 .andReturn(controllerRegistryService).once();
66 expect(persistIntent.getKey()).andReturn(1L).anyTimes();
67 expect(persistIntent.persistIfLeader(eq(1L),
68 anyObject(IntentOperationList.class))).andReturn(true)
69 .anyTimes();
70
71 expect(networkGraphService.getNetworkGraph()).andReturn(graph)
72 .anyTimes();
73 networkGraphService.registerNetworkGraphListener(
74 anyObject(INetworkGraphListener.class));
75 expectLastCall();
76
77 expect(datagridService.createChannel("onos.pathintent",
78 Long.class, IntentOperationList.class))
79 .andReturn(eventChannel).once();
80
81 expect(datagridService.addListener(
82 eq("onos.pathintent_state"),
83 anyObject(IEventChannelListener.class),
84 eq(Long.class),
85 eq(IntentStateList.class)))
86 .andReturn(eventChannel).once();
87
88 replay(datagridService);
89 replay(networkGraphService);
90 replay(modContext);
91 replay(controllerRegistryService);
92 PowerMock.replay(persistIntent, PersistIntent.class);
93 }
94
95 @After
96 public void tearDown() {
97 verify(datagridService);
98 verify(networkGraphService);
99 verify(modContext);
100 verify(controllerRegistryService);
101 PowerMock.verify(persistIntent, PersistIntent.class);
102 }
103
104 /**
105 * Test the result of executing a path calculation on an
106 * Intent Operation List which contains a path that references a
107 * non-existent switch.
108 * <p/>
109 * A 3 path list is created where one of the paths references a switch
110 * that is not in the topology. The test checks that the resulting
111 * Operation List has entries for the 2 correct paths, and that the
112 * high level intents contain a proper error entry for the bad path.
113 */
114 @Test
115 public void testInvalidSwitchName() throws FloodlightModuleException {
116
117 final Long LOCAL_PORT = 0xFFFEL;
118 final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
119
120 // create shortest path intents
121 final IntentOperationList opList = new IntentOperationList();
122 opList.add(Operator.ADD,
123 new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L,
124 LOCAL_PORT, 2L, 21L, LOCAL_PORT));
125 opList.add(Operator.ADD,
126 new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L,
127 LOCAL_PORT));
128 opList.add(Operator.ADD,
129 new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L,
130 LOCAL_PORT));
131
132 // compile high-level intent operations into low-level intent
133 // operations (calculate paths)
134 final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule();
135 runtime.init(modContext);
136 runtime.startUp(modContext);
137 final IntentOperationList pathIntentOpList =
138 runtime.executeIntentOperations(opList);
139 assertThat(pathIntentOpList, notNullValue());
140
141 final IntentMap highLevelIntents = runtime.getHighLevelIntents();
142 assertThat(highLevelIntents, notNullValue());
143
144 final Collection<Intent> allIntents = highLevelIntents.getAllIntents();
145 assertThat(allIntents, notNullValue());
146
147 // One intent had an error and should not create a path list entry
148 assertThat(pathIntentOpList, hasSize(opList.size() - 1));
149
150 // Should be a high level intent for each operation
151 assertThat(opList, hasSize(allIntents.size()));
152
153 // Check that we got a high level intent for each operation
154 assertThat(allIntents,
155 hasItem(Matchers.<Intent>
156 hasProperty("id", equalTo("3"))));
157 assertThat(allIntents,
158 hasItem(Matchers.<Intent>
159 hasProperty("id", equalTo("2"))));
160 assertThat(allIntents,
161 hasItem(Matchers.<Intent>
162 hasProperty("id", equalTo(BAD_SWITCH_INTENT_NAME))));
163
164 // Check that the non existent switch was NACKed
165 final Intent intentForBadSwitch =
166 highLevelIntents.getIntent(BAD_SWITCH_INTENT_NAME);
167 assertThat(intentForBadSwitch, notNullValue());
168 assertThat(intentForBadSwitch.getState(),
169 is(equalTo(IntentState.INST_NACK)));
170
171 // Check that switch 2 was correctly processed
172 final Intent intentForSwitch2 = highLevelIntents.getIntent("2");
173 assertThat(intentForSwitch2, notNullValue());
174 assertThat(intentForSwitch2.getState(),
175 is(equalTo(IntentState.INST_REQ)));
176
177 // Check that switch 3 was correctly processed
178 final Intent intentForSwitch3 = highLevelIntents.getIntent("3");
179 assertThat(intentForSwitch3, notNullValue());
180 assertThat(intentForSwitch3.getState(),
181 is(equalTo(IntentState.INST_REQ)));
182
183 }
184
185
186}