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