blob: 6bdca51780aebdd5c1f13823216e0a762e975394 [file] [log] [blame]
Toshio Koide4f308732014-02-18 15:19:48 -08001package net.onrc.onos.intent.runtime;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
Toshio Koide798bc1b2014-02-20 14:02:40 -08006import java.util.HashSet;
Toshio Koidedf2eab92014-02-20 11:24:59 -08007import java.util.Iterator;
Toshio Koide4f308732014-02-18 15:19:48 -08008import java.util.Map;
9
10import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.core.module.FloodlightModuleException;
12import net.floodlightcontroller.core.module.IFloodlightModule;
13import net.floodlightcontroller.core.module.IFloodlightService;
14import net.onrc.onos.datagrid.IDatagridService;
15import net.onrc.onos.datagrid.IEventChannel;
Toshio Koide0c9106d2014-02-19 15:26:38 -080016import net.onrc.onos.intent.Intent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080017import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide4f308732014-02-18 15:19:48 -080018import net.onrc.onos.intent.IntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080019import net.onrc.onos.intent.IntentOperation;
Toshio Koide0c9106d2014-02-19 15:26:38 -080020import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080021import net.onrc.onos.intent.IntentOperationList;
Toshio Koide0c9106d2014-02-19 15:26:38 -080022import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080023import net.onrc.onos.intent.PathIntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080024import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080025import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
26import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080027import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080028import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
29import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
30import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080031import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koide4f308732014-02-18 15:19:48 -080032
Toshio Koide0c9106d2014-02-19 15:26:38 -080033/**
34 * @author Toshio Koide (t-koide@onlab.us)
35 */
36public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener {
Toshio Koide4f308732014-02-18 15:19:48 -080037 private PathCalcRuntime runtime;
38 private IDatagridService datagridService;
Toshio Koide27ffd412014-02-18 19:15:27 -080039 private INetworkGraphService networkGraphService;
Toshio Koide4f308732014-02-18 15:19:48 -080040 private IntentMap highLevelIntents;
Toshio Koide27ffd412014-02-18 19:15:27 -080041 private PathIntentMap pathIntents;
Toshio Koide798bc1b2014-02-20 14:02:40 -080042 private IControllerRegistryService controllerRegistry;
43 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080044
Nick Karanatsios8abe7172014-02-19 20:31:48 -080045 private IEventChannel<Long, IntentOperationList> eventChannel;
Toshio Koide4f308732014-02-18 15:19:48 -080046 private static final String EVENT_CHANNEL_NAME = "onos.pathintent";
47
Toshio Koide798bc1b2014-02-20 14:02:40 -080048 // ================================================================================
49 // private methods
50 // ================================================================================
51
52 private void reroutePaths(Collection<LinkEvent> removedLinkEvents) {
53 HashSet<PathIntent> oldPaths = new HashSet<>();
54 for (LinkEvent linkEvent : removedLinkEvents) {
55 Collection<PathIntent> intents = pathIntents.getIntentsByLink(linkEvent);
56 if (intents == null)
57 continue;
58 oldPaths.addAll(intents);
59 }
60
61 if (oldPaths.isEmpty())
62 return;
Toshio Koide0c9106d2014-02-19 15:26:38 -080063 IntentOperationList reroutingOperation = new IntentOperationList();
Toshio Koide798bc1b2014-02-20 14:02:40 -080064 for (PathIntent pathIntent : oldPaths) {
65 // TODO use Operator.UPDATE instead of REMOVE and ADD in order to
66 // optimize
Toshio Koide0c9106d2014-02-19 15:26:38 -080067 reroutingOperation.add(Operator.REMOVE, new Intent(pathIntent.getParentIntent().getId()));
68 reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent());
69 }
70 executeIntentOperations(reroutingOperation);
71 }
Toshio Koide27ffd412014-02-18 19:15:27 -080072
Toshio Koide798bc1b2014-02-20 14:02:40 -080073 // ================================================================================
74 // IFloodlightModule implementations
75 // ================================================================================
76
Toshio Koide4f308732014-02-18 15:19:48 -080077 @Override
78 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
79 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1);
Toshio Koideeb90d912014-02-18 21:30:22 -080080 l.add(IPathCalcRuntimeService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080081 return l;
82 }
83
84 @Override
85 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
86 Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(1);
Toshio Koide27ffd412014-02-18 19:15:27 -080087 m.put(IPathCalcRuntimeService.class, this);
Toshio Koide4f308732014-02-18 15:19:48 -080088 return m;
89 }
90
91 @Override
92 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
93 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>();
94 l.add(IDatagridService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080095 l.add(INetworkGraphService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080096 return l;
97 }
98
99 @Override
100 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
101 datagridService = context.getServiceImpl(IDatagridService.class);
Toshio Koided9fa2a82014-02-19 17:35:18 -0800102 networkGraphService = context.getServiceImpl(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800103 controllerRegistry = context.getServiceImpl(IControllerRegistryService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800104 }
105
106 @Override
107 public void startUp(FloodlightModuleContext context) {
Toshio Koideeb90d912014-02-18 21:30:22 -0800108 highLevelIntents = new IntentMap();
109 runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph());
Toshio Koide0c9106d2014-02-19 15:26:38 -0800110 pathIntents = new PathIntentMap();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800111 eventChannel = datagridService.createChannel(EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800112 networkGraphService.registerNetworkGraphListener(this);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800113 persistIntent = new PersistIntent(controllerRegistry, networkGraphService);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800114
Toshio Koide4f308732014-02-18 15:19:48 -0800115 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800116
Toshio Koide798bc1b2014-02-20 14:02:40 -0800117 // ================================================================================
118 // IPathCalcRuntimeService implementations
119 // ================================================================================
120
Toshio Koide27ffd412014-02-18 19:15:27 -0800121 @Override
122 public IntentOperationList executeIntentOperations(IntentOperationList list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800123 // update the map of high-level intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800124 highLevelIntents.executeOperations(list);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800125
126 // prepare high-level intents' state changes
127 HashMap<String, IntentState> states = new HashMap<>();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800128 for (IntentOperation op : list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800129 String id = op.intent.getId();
130 states.put(id, IntentState.INST_REQ);
131 }
132
133 // calculate path-intents (low-level operations)
Toshio Koide27ffd412014-02-18 19:15:27 -0800134 IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800135
136 // persist calculated low-level operations
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800137 long key = persistIntent.getKey();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800138 persistIntent.persistIfLeader(key, pathIntentOperations);
139
140 // remove error-intents and reflect them to high-level intents
141 Iterator<IntentOperation> i = pathIntentOperations.iterator();
142 while (i.hasNext()) {
143 IntentOperation op = i.next();
144 if (op.operator.equals(Operator.ERROR)) {
145 states.put(op.intent.getId(), IntentState.INST_NACK);
146 i.remove();
147 }
148 }
149 highLevelIntents.changeStates(states);
150
Toshio Koide798bc1b2014-02-20 14:02:40 -0800151 // update the map of low-level intents and publish the low-level
152 // operations
Toshio Koide27ffd412014-02-18 19:15:27 -0800153 pathIntents.executeOperations(pathIntentOperations);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800154 eventChannel.addEntry(key, pathIntentOperations);
Toshio Koide27ffd412014-02-18 19:15:27 -0800155 return pathIntentOperations;
156 }
157
158 @Override
159 public IntentMap getHighLevelIntents() {
Toshio Koide4f308732014-02-18 15:19:48 -0800160 return highLevelIntents;
161 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800162
163 @Override
164 public IntentMap getPathIntents() {
165 return pathIntents;
166 }
167
168 @Override
Toshio Koide4f308732014-02-18 15:19:48 -0800169 public void purgeIntents() {
170 highLevelIntents.purge();
Toshio Koide27ffd412014-02-18 19:15:27 -0800171 pathIntents.purge();
Toshio Koide4f308732014-02-18 15:19:48 -0800172 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800173
Toshio Koide798bc1b2014-02-20 14:02:40 -0800174 // ================================================================================
175 // INetworkGraphListener implementations
176 // ================================================================================
177
Toshio Koide0c9106d2014-02-19 15:26:38 -0800178 @Override
Toshio Koide798bc1b2014-02-20 14:02:40 -0800179 public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents,
180 Collection<SwitchEvent> removedSwitchEvents,
181 Collection<PortEvent> addedPortEvents,
182 Collection<PortEvent> removedPortEvents,
183 Collection<LinkEvent> addedLinkEvents,
184 Collection<LinkEvent> removedLinkEvents,
185 Collection<DeviceEvent> addedDeviceEvents,
186 Collection<DeviceEvent> removedDeviceEvents) {
187 // TODO need optimization.
188 // This reroutes only if when some links are removed for now.
189 reroutePaths(removedLinkEvents);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800190 }
Toshio Koide798bc1b2014-02-20 14:02:40 -0800191}