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