blob: b5521e261f969e3a03778d6b108b56674f3f0f59 [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 Koidedf2eab92014-02-20 11:24:59 -080016import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide4f308732014-02-18 15:19:48 -080017import net.onrc.onos.intent.IntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080018import net.onrc.onos.intent.IntentOperation;
Toshio Koide0c9106d2014-02-19 15:26:38 -080019import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080020import net.onrc.onos.intent.IntentOperationList;
Toshio Koide0c9106d2014-02-19 15:26:38 -080021import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080022import net.onrc.onos.intent.PathIntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080023import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080024import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
25import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080026import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080027import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
28import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
29import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080030import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koide4f308732014-02-18 15:19:48 -080031
Toshio Koide0c9106d2014-02-19 15:26:38 -080032/**
33 * @author Toshio Koide (t-koide@onlab.us)
34 */
35public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener {
Toshio Koide4f308732014-02-18 15:19:48 -080036 private PathCalcRuntime runtime;
37 private IDatagridService datagridService;
Toshio Koide27ffd412014-02-18 19:15:27 -080038 private INetworkGraphService networkGraphService;
Toshio Koide4f308732014-02-18 15:19:48 -080039 private IntentMap highLevelIntents;
Toshio Koide27ffd412014-02-18 19:15:27 -080040 private PathIntentMap pathIntents;
Toshio Koide798bc1b2014-02-20 14:02:40 -080041 private IControllerRegistryService controllerRegistry;
42 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080043
Nick Karanatsios8abe7172014-02-19 20:31:48 -080044 private IEventChannel<Long, IntentOperationList> eventChannel;
Toshio Koide4f308732014-02-18 15:19:48 -080045 private static final String EVENT_CHANNEL_NAME = "onos.pathintent";
46
Toshio Koide798bc1b2014-02-20 14:02:40 -080047 // ================================================================================
48 // private methods
49 // ================================================================================
50
51 private void reroutePaths(Collection<LinkEvent> removedLinkEvents) {
52 HashSet<PathIntent> oldPaths = new HashSet<>();
53 for (LinkEvent linkEvent : removedLinkEvents) {
54 Collection<PathIntent> intents = pathIntents.getIntentsByLink(linkEvent);
55 if (intents == null)
56 continue;
57 oldPaths.addAll(intents);
58 }
59
60 if (oldPaths.isEmpty())
61 return;
Toshio Koidea10c0372014-02-20 17:28:10 -080062
Toshio Koide0c9106d2014-02-19 15:26:38 -080063 IntentOperationList reroutingOperation = new IntentOperationList();
Toshio Koide798bc1b2014-02-20 14:02:40 -080064 for (PathIntent pathIntent : oldPaths) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080065 reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent());
66 }
67 executeIntentOperations(reroutingOperation);
68 }
Toshio Koide27ffd412014-02-18 19:15:27 -080069
Toshio Koide798bc1b2014-02-20 14:02:40 -080070 // ================================================================================
71 // IFloodlightModule implementations
72 // ================================================================================
73
Toshio Koide4f308732014-02-18 15:19:48 -080074 @Override
75 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
76 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1);
Toshio Koideeb90d912014-02-18 21:30:22 -080077 l.add(IPathCalcRuntimeService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080078 return l;
79 }
80
81 @Override
82 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
83 Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(1);
Toshio Koide27ffd412014-02-18 19:15:27 -080084 m.put(IPathCalcRuntimeService.class, this);
Toshio Koide4f308732014-02-18 15:19:48 -080085 return m;
86 }
87
88 @Override
89 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
90 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>();
91 l.add(IDatagridService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080092 l.add(INetworkGraphService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080093 return l;
94 }
95
96 @Override
97 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
98 datagridService = context.getServiceImpl(IDatagridService.class);
Toshio Koided9fa2a82014-02-19 17:35:18 -080099 networkGraphService = context.getServiceImpl(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800100 controllerRegistry = context.getServiceImpl(IControllerRegistryService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800101 }
102
103 @Override
104 public void startUp(FloodlightModuleContext context) {
Toshio Koideeb90d912014-02-18 21:30:22 -0800105 highLevelIntents = new IntentMap();
106 runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph());
Toshio Koide0c9106d2014-02-19 15:26:38 -0800107 pathIntents = new PathIntentMap();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800108 eventChannel = datagridService.createChannel(EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800109 networkGraphService.registerNetworkGraphListener(this);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800110 persistIntent = new PersistIntent(controllerRegistry, networkGraphService);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800111
Toshio Koide4f308732014-02-18 15:19:48 -0800112 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800113
Toshio Koide798bc1b2014-02-20 14:02:40 -0800114 // ================================================================================
115 // IPathCalcRuntimeService implementations
116 // ================================================================================
117
Toshio Koide27ffd412014-02-18 19:15:27 -0800118 @Override
119 public IntentOperationList executeIntentOperations(IntentOperationList list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800120 // update the map of high-level intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800121 highLevelIntents.executeOperations(list);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800122
Toshio Koidea10c0372014-02-20 17:28:10 -0800123 // change states of high-level intents
Toshio Koidedf2eab92014-02-20 11:24:59 -0800124 HashMap<String, IntentState> states = new HashMap<>();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800125 for (IntentOperation op : list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800126 String id = op.intent.getId();
Toshio Koidea10c0372014-02-20 17:28:10 -0800127 if (op.intent.getState().equals(IntentState.INST_ACK))
128 states.put(id, IntentState.REROUTE_REQ);
129 else
130 states.put(id, IntentState.INST_REQ);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800131 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800132 highLevelIntents.changeStates(states);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800133
134 // calculate path-intents (low-level operations)
Toshio Koide27ffd412014-02-18 19:15:27 -0800135 IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800136
Toshio Koidea10c0372014-02-20 17:28:10 -0800137 // persist calculated low-level operations into data store
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800138 long key = persistIntent.getKey();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800139 persistIntent.persistIfLeader(key, pathIntentOperations);
140
141 // remove error-intents and reflect them to high-level intents
Toshio Koidea10c0372014-02-20 17:28:10 -0800142 states.clear();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800143 Iterator<IntentOperation> i = pathIntentOperations.iterator();
144 while (i.hasNext()) {
145 IntentOperation op = i.next();
146 if (op.operator.equals(Operator.ERROR)) {
147 states.put(op.intent.getId(), IntentState.INST_NACK);
148 i.remove();
149 }
150 }
151 highLevelIntents.changeStates(states);
152
Toshio Koide798bc1b2014-02-20 14:02:40 -0800153 // update the map of low-level intents and publish the low-level
154 // operations
Toshio Koide27ffd412014-02-18 19:15:27 -0800155 pathIntents.executeOperations(pathIntentOperations);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800156 eventChannel.addEntry(key, pathIntentOperations);
Toshio Koide27ffd412014-02-18 19:15:27 -0800157 return pathIntentOperations;
158 }
159
160 @Override
161 public IntentMap getHighLevelIntents() {
Toshio Koide4f308732014-02-18 15:19:48 -0800162 return highLevelIntents;
163 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800164
165 @Override
166 public IntentMap getPathIntents() {
167 return pathIntents;
168 }
169
170 @Override
Toshio Koide4f308732014-02-18 15:19:48 -0800171 public void purgeIntents() {
172 highLevelIntents.purge();
Toshio Koide27ffd412014-02-18 19:15:27 -0800173 pathIntents.purge();
Toshio Koide4f308732014-02-18 15:19:48 -0800174 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800175
Toshio Koide798bc1b2014-02-20 14:02:40 -0800176 // ================================================================================
177 // INetworkGraphListener implementations
178 // ================================================================================
179
Toshio Koide0c9106d2014-02-19 15:26:38 -0800180 @Override
Toshio Koide798bc1b2014-02-20 14:02:40 -0800181 public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents,
182 Collection<SwitchEvent> removedSwitchEvents,
183 Collection<PortEvent> addedPortEvents,
184 Collection<PortEvent> removedPortEvents,
185 Collection<LinkEvent> addedLinkEvents,
186 Collection<LinkEvent> removedLinkEvents,
187 Collection<DeviceEvent> addedDeviceEvents,
188 Collection<DeviceEvent> removedDeviceEvents) {
189 // TODO need optimization.
Toshio Koidea10c0372014-02-20 17:28:10 -0800190 // Only high-level intents that affected by link down are rerouted.
Toshio Koide798bc1b2014-02-20 14:02:40 -0800191 reroutePaths(removedLinkEvents);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800192 }
Toshio Koide798bc1b2014-02-20 14:02:40 -0800193}