Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 6 | import java.util.Iterator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 7 | import java.util.Map; |
| 8 | |
| 9 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 11 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 13 | import net.onrc.onos.datagrid.IDatagridService; |
| 14 | import net.onrc.onos.datagrid.IEventChannel; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 15 | import net.onrc.onos.intent.Intent; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 16 | import net.onrc.onos.intent.Intent.IntentState; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 17 | import net.onrc.onos.intent.IntentMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 18 | import net.onrc.onos.intent.IntentOperation; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 19 | import net.onrc.onos.intent.IntentOperation.Operator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 20 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 21 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 22 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 23 | import net.onrc.onos.intent.persist.PersistIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent; |
| 25 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
| 28 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 29 | import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 30 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 31 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @author Toshio Koide (t-koide@onlab.us) |
| 34 | */ |
| 35 | public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 36 | private PathCalcRuntime runtime; |
| 37 | private IDatagridService datagridService; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 38 | private INetworkGraphService networkGraphService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 39 | private IntentMap highLevelIntents; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 40 | private PathIntentMap pathIntents; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 41 | private IControllerRegistryService controllerRegistry; |
| 42 | private PersistIntent persistIntent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 43 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 44 | private IEventChannel<Long, IntentOperationList> eventChannel; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 45 | private static final String EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 46 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 47 | private void reroutePaths(LinkEvent linkEvent) { |
| 48 | Collection<PathIntent> oldPaths = pathIntents.getIntentsByLink(linkEvent); |
| 49 | if (oldPaths == null) return; |
| 50 | IntentOperationList reroutingOperation = new IntentOperationList(); |
| 51 | for (PathIntent pathIntent: oldPaths) { |
| 52 | // TODO use Operator.UPDATE instead of REMOVE and ADD in order to optimize |
| 53 | reroutingOperation.add(Operator.REMOVE, new Intent(pathIntent.getParentIntent().getId())); |
| 54 | reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent()); |
| 55 | } |
| 56 | executeIntentOperations(reroutingOperation); |
| 57 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 58 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 59 | @Override |
| 60 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 61 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 62 | l.add(IPathCalcRuntimeService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 63 | return l; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 68 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(1); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 69 | m.put(IPathCalcRuntimeService.class, this); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 70 | return m; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 75 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
| 76 | l.add(IDatagridService.class); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 77 | l.add(INetworkGraphService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 78 | return l; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 83 | datagridService = context.getServiceImpl(IDatagridService.class); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 84 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 85 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void startUp(FloodlightModuleContext context) { |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 90 | highLevelIntents = new IntentMap(); |
| 91 | runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph()); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 92 | pathIntents = new PathIntentMap(); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 93 | eventChannel = datagridService.createChannel( |
| 94 | EVENT_CHANNEL_NAME, |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 95 | Long.class, |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 96 | IntentOperationList.class); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 97 | networkGraphService.registerNetworkGraphListener(this); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 98 | persistIntent = new PersistIntent(controllerRegistry, networkGraphService); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 99 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 100 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 101 | |
| 102 | @Override |
| 103 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 104 | // update the map of high-level intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 105 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 106 | |
| 107 | // prepare high-level intents' state changes |
| 108 | HashMap<String, IntentState> states = new HashMap<>(); |
| 109 | for (IntentOperation op: list) { |
| 110 | String id = op.intent.getId(); |
| 111 | states.put(id, IntentState.INST_REQ); |
| 112 | } |
| 113 | |
| 114 | // calculate path-intents (low-level operations) |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 115 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 116 | |
| 117 | // persist calculated low-level operations |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 118 | long key = persistIntent.getKey(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 119 | persistIntent.persistIfLeader(key, pathIntentOperations); |
| 120 | |
| 121 | // remove error-intents and reflect them to high-level intents |
| 122 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 123 | while (i.hasNext()) { |
| 124 | IntentOperation op = i.next(); |
| 125 | if (op.operator.equals(Operator.ERROR)) { |
| 126 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 127 | i.remove(); |
| 128 | } |
| 129 | } |
| 130 | highLevelIntents.changeStates(states); |
| 131 | |
| 132 | // update the map of low-level intents and publish the low-level operations |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 133 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 134 | eventChannel.addEntry(key, pathIntentOperations); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 135 | return pathIntentOperations; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public IntentMap getHighLevelIntents() { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 140 | return highLevelIntents; |
| 141 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 142 | |
| 143 | @Override |
| 144 | public IntentMap getPathIntents() { |
| 145 | return pathIntents; |
| 146 | } |
| 147 | |
| 148 | @Override |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 149 | public void purgeIntents() { |
| 150 | highLevelIntents.purge(); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 151 | pathIntents.purge(); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 152 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 153 | |
| 154 | @Override |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 155 | public void networkGraphEvents( |
| 156 | Collection<SwitchEvent> addedSwitchEvents, |
| 157 | Collection<SwitchEvent> removedSwitchEvents, |
| 158 | Collection<PortEvent> addedPortEvents, |
| 159 | Collection<PortEvent> removedPortEvents, |
| 160 | Collection<LinkEvent> addedLinkEvents, |
| 161 | Collection<LinkEvent> removedLinkEvents, |
| 162 | Collection<DeviceEvent> addedDeviceEvents, |
| 163 | Collection<DeviceEvent> removedDeviceEvents) { |
| 164 | // TODO: The implementation below is incomplete |
| 165 | for (LinkEvent linkEvent : removedLinkEvents) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 166 | reroutePaths(linkEvent); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 167 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 168 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 169 | } |