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