blob: 4939f90ae19b01925849d6f11917d4e55c5d8981 [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;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -070016import net.onrc.onos.core.intent.Path;
Jonathan Hartaa380972014-04-03 10:24:46 -070017import net.onrc.onos.core.intent.PathIntent;
18import net.onrc.onos.core.intent.PathIntentMap;
19import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hart472062d2014-04-03 10:56:48 -070020import net.onrc.onos.core.topology.NetworkGraph;
Jonathan Hart472062d2014-04-03 10:56:48 -070021import 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 {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 private NetworkGraph graph;
Ray Milkeyec838942014-04-09 11:28:43 -070031 private static final Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080032
Ray Milkey269ffb92014-04-03 14:43:30 -070033 public PathCalcRuntime(NetworkGraph g) {
34 this.graph = g;
35 }
Toshio Koidec87810e2014-02-11 13:03:21 -080036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070038 * calculate shortest-path and constrained-shortest-path intents into low-level path intents.
Ray Milkey269ffb92014-04-03 14:43:30 -070039 *
40 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
41 * @param pathIntents a set of current low-level intents
42 * @return IntentOperationList. PathIntent and/or ErrorIntent instances.
43 */
44 public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, final IntentMap appIntents, final PathIntentMap pathIntents) {
45 IntentOperationList pathIntentOpList = new IntentOperationList();
46 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koide59bc8ea2014-02-21 17:48:46 -080047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 // TODO optimize locking of NetworkGraph
49 graph.acquireReadLock();
50 log.debug("NetworkGraph: {}", graph.getLinks());
Toshio Koidec87810e2014-02-11 13:03:21 -080051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 for (IntentOperation intentOp : intentOpList) {
53 switch (intentOp.operator) {
54 case ADD:
55 if (!(intentOp.intent instanceof ShortestPathIntent)) {
56 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
57 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
58 ErrorType.UNSUPPORTED_INTENT,
59 "Unsupported intent type.",
60 intentOp.intent));
61 continue;
62 }
Toshio Koidec87810e2014-02-11 13:03:21 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
65 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
66 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
67 if (srcSwitch == null || dstSwitch == null) {
68 log.error("Switch not found. src:{}, dst:{}, NetworkGraph:{}",
69 spIntent.getSrcSwitchDpid(),
70 spIntent.getDstSwitchDpid(),
71 graph.getLinks());
72 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
73 ErrorType.SWITCH_NOT_FOUND,
74 "Switch not found.",
75 spIntent));
76 continue;
77 }
Toshio Koidea10c0372014-02-20 17:28:10 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 double bandwidth = 0.0;
80 ConstrainedBFSTree tree = null;
81 if (spIntent instanceof ConstrainedShortestPathIntent) {
82 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
83 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
84 } else {
85 tree = spfTrees.get(srcSwitch);
86 if (tree == null) {
87 tree = new ConstrainedBFSTree(srcSwitch);
88 spfTrees.put(srcSwitch, tree);
89 }
90 }
91 Path path = tree.getPath(dstSwitch);
92 if (path == null) {
93 log.error("Path not found. Intent: {}, NetworkGraph:{}", spIntent.toString(), graph.getLinks());
94 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
95 ErrorType.PATH_NOT_FOUND,
96 "Path not found.",
97 spIntent));
98 continue;
99 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 // generate new path-intent ID
102 String oldPathIntentId = spIntent.getPathIntentId();
103 String newPathIntentId;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 if (oldPathIntentId == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700106 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
Toshio Koidea10c0372014-02-20 17:28:10 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 // Request removal of low-level intent if it exists.
110 pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId));
111 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 // create new path-intent
114 PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
115 pathIntent.setState(IntentState.INST_REQ);
116 spIntent.setPathIntent(pathIntent);
117 pathIntentOpList.add(Operator.ADD, pathIntent);
Toshio Koide59bc8ea2014-02-21 17:48:46 -0800118
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 break;
120 case REMOVE:
121 ShortestPathIntent targetAppIntent = (ShortestPathIntent) appIntents.getIntent(intentOp.intent.getId());
122 if (targetAppIntent != null) {
123 String pathIntentId = targetAppIntent.getPathIntentId();
124 if (pathIntentId != null) {
125 Intent targetPathIntent = pathIntents.getIntent(pathIntentId);
126 if (targetPathIntent != null) {
127 pathIntentOpList.add(Operator.REMOVE, targetPathIntent);
128 }
129 }
130 }
131 break;
132 case ERROR:
133 // just ignore
134 break;
Ray Milkey0b122ed2014-04-14 10:06:03 -0700135 default:
136 log.error("Unknown intent operator {}", intentOp.operator);
137 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 }
139 }
140 // TODO optimize locking of NetworkGraph
141 graph.releaseReadLock();
142
143 return pathIntentOpList;
144 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700145}