blob: 93149595417b5c0d6529ecd57cc74ba80b464220 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent.runtime;
Toshio Koidec87810e2014-02-11 13:03:21 -08002
Toshio Koide27ffd412014-02-18 19:15:27 -08003import java.util.HashMap;
Toshio Koidec87810e2014-02-11 13:03:21 -08004
Toshio Koide4f308732014-02-18 15:19:48 -08005import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hartaa380972014-04-03 10:24:46 -07006import net.onrc.onos.core.intent.ConstrainedBFSTree;
7import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
8import net.onrc.onos.core.intent.ErrorIntent;
Jonathan Harta99ec672014-04-03 11:30:34 -07009import net.onrc.onos.core.intent.ErrorIntent.ErrorType;
Jonathan Hartaa380972014-04-03 10:24:46 -070010import net.onrc.onos.core.intent.Intent;
Jonathan Harta99ec672014-04-03 11:30:34 -070011import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hartaa380972014-04-03 10:24:46 -070012import net.onrc.onos.core.intent.IntentMap;
13import net.onrc.onos.core.intent.IntentOperation;
Jonathan Harta99ec672014-04-03 11:30:34 -070014import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hartaa380972014-04-03 10:24:46 -070015import net.onrc.onos.core.intent.IntentOperationList;
16import net.onrc.onos.core.intent.PathIntent;
17import net.onrc.onos.core.intent.PathIntentMap;
18import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hart472062d2014-04-03 10:56:48 -070019import net.onrc.onos.core.topology.NetworkGraph;
20import net.onrc.onos.core.topology.Path;
21import net.onrc.onos.core.topology.Switch;
Toshio Koidec87810e2014-02-11 13:03:21 -080022
Toshio Koideb39c9d32014-02-20 01:21:47 -080023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Toshio Koidec87810e2014-02-11 13:03:21 -080026/**
27 * @author Toshio Koide (t-koide@onlab.us)
28 */
Toshio Koide4f308732014-02-18 15:19:48 -080029public class PathCalcRuntime implements IFloodlightService {
30 private NetworkGraph graph;
Toshio Koideb39c9d32014-02-20 01:21:47 -080031 private final static Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080032 public PathCalcRuntime(NetworkGraph g) {
33 this.graph = g;
34 }
35
Toshio Koide27ffd412014-02-18 19:15:27 -080036 /**
37 * calculate shortest-path and constrained-shortest-path intents into low-level path intents
38 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
39 * @param pathIntents a set of current low-level intents
Toshio Koidedf2eab92014-02-20 11:24:59 -080040 * @return IntentOperationList. PathIntent and/or ErrorIntent instances.
Toshio Koide27ffd412014-02-18 19:15:27 -080041 */
Toshio Koidedba6fef2014-02-23 15:36:21 -080042 public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, final IntentMap appIntents, final PathIntentMap pathIntents) {
Toshio Koide27ffd412014-02-18 19:15:27 -080043 IntentOperationList pathIntentOpList = new IntentOperationList();
44 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koidec87810e2014-02-11 13:03:21 -080045
Toshio Koide59bc8ea2014-02-21 17:48:46 -080046 // TODO optimize locking of NetworkGraph
47 graph.acquireReadLock();
Toshio Koide240b1302014-02-26 20:08:41 -080048 log.debug("NetworkGraph: {}", graph.getLinks());
Toshio Koide59bc8ea2014-02-21 17:48:46 -080049
Toshio Koide27ffd412014-02-18 19:15:27 -080050 for (IntentOperation intentOp: intentOpList) {
51 switch (intentOp.operator) {
52 case ADD:
53 if (!(intentOp.intent instanceof ShortestPathIntent)) {
Toshio Koidedf2eab92014-02-20 11:24:59 -080054 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
55 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
56 ErrorType.UNSUPPORTED_INTENT,
57 "Unsupported intent type.",
58 intentOp.intent));
Toshio Koide27ffd412014-02-18 19:15:27 -080059 continue;
60 }
Toshio Koidec87810e2014-02-11 13:03:21 -080061
Toshio Koide27ffd412014-02-18 19:15:27 -080062 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
63 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
64 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
65 if (srcSwitch == null || dstSwitch == null) {
Toshio Koide240b1302014-02-26 20:08:41 -080066 log.error("Switch not found. src:{}, dst:{}, NetworkGraph:{}",
Toshio Koideb39c9d32014-02-20 01:21:47 -080067 spIntent.getSrcSwitchDpid(),
Toshio Koide240b1302014-02-26 20:08:41 -080068 spIntent.getDstSwitchDpid(),
69 graph.getLinks());
Toshio Koidedf2eab92014-02-20 11:24:59 -080070 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
71 ErrorType.SWITCH_NOT_FOUND,
72 "Switch not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080073 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080074 continue;
75 }
Toshio Koidec87810e2014-02-11 13:03:21 -080076
Toshio Koide27ffd412014-02-18 19:15:27 -080077 double bandwidth = 0.0;
78 ConstrainedBFSTree tree = null;
Toshio Koidea10c0372014-02-20 17:28:10 -080079 if (spIntent instanceof ConstrainedShortestPathIntent) {
Toshio Koide27ffd412014-02-18 19:15:27 -080080 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
81 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
82 }
83 else {
84 tree = spfTrees.get(srcSwitch);
85 if (tree == null) {
86 tree = new ConstrainedBFSTree(srcSwitch);
87 spfTrees.put(srcSwitch, tree);
88 }
89 }
90 Path path = tree.getPath(dstSwitch);
91 if (path == null) {
Toshio Koide240b1302014-02-26 20:08:41 -080092 log.error("Path not found. Intent: {}, NetworkGraph:{}", spIntent.toString(), graph.getLinks());
Toshio Koidedf2eab92014-02-20 11:24:59 -080093 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
94 ErrorType.PATH_NOT_FOUND,
95 "Path not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080096 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080097 continue;
98 }
Toshio Koidea10c0372014-02-20 17:28:10 -080099
100 // generate new path-intent ID
101 String oldPathIntentId = spIntent.getPathIntentId();
102 String newPathIntentId;
103 if (oldPathIntentId == null)
104 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
105 else {
106 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
107
108 // Request removal of low-level intent if it exists.
109 pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId));
110 }
111
112 // create new path-intent
113 PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
114 pathIntent.setState(IntentState.INST_REQ);
115 spIntent.setPathIntent(pathIntent);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800116 pathIntentOpList.add(Operator.ADD, pathIntent);
Toshio Koidea10c0372014-02-20 17:28:10 -0800117
Toshio Koide27ffd412014-02-18 19:15:27 -0800118 break;
119 case REMOVE:
Toshio Koidedba6fef2014-02-23 15:36:21 -0800120 ShortestPathIntent targetAppIntent = (ShortestPathIntent) appIntents.getIntent(intentOp.intent.getId());
121 if (targetAppIntent != null) {
122 String pathIntentId = targetAppIntent.getPathIntentId();
123 if (pathIntentId != null) {
124 Intent targetPathIntent = pathIntents.getIntent(pathIntentId);
125 if (targetPathIntent != null) {
126 pathIntentOpList.add(Operator.REMOVE, targetPathIntent);
127 }
128 }
129 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800130 break;
Toshio Koidedf2eab92014-02-20 11:24:59 -0800131 case ERROR:
132 // just ignore
133 break;
Toshio Koidec87810e2014-02-11 13:03:21 -0800134 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800135 }
Toshio Koide59bc8ea2014-02-21 17:48:46 -0800136 // TODO optimize locking of NetworkGraph
137 graph.releaseReadLock();
138
Toshio Koide27ffd412014-02-18 19:15:27 -0800139 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -0800140 }
141}