blob: 3dc52526eb85e1559407df83f3f01563b0b5fc3f [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.Switch;
Jonathan Harte37e4e22014-05-13 19:12:02 -070021import net.onrc.onos.core.topology.Topology;
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/**
Toshio Koidefdb75932014-06-16 17:59:24 -070027 * The runtime used by PathCalcRuntimeModule class.
28 * <p>
29 * It calculates shortest-path and constrained-shortest-path.
Toshio Koidec87810e2014-02-11 13:03:21 -080030 */
Toshio Koide4f308732014-02-18 15:19:48 -080031public class PathCalcRuntime implements IFloodlightService {
Jonathan Harte37e4e22014-05-13 19:12:02 -070032 private Topology topology;
Ray Milkeyec838942014-04-09 11:28:43 -070033 private static final Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080034
Toshio Koidefdb75932014-06-16 17:59:24 -070035 /**
36 * Constructor.
37 *
38 * @param topology a topology object to use for the path calculation.
39 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070040 public PathCalcRuntime(Topology topology) {
41 this.topology = topology;
Ray Milkey269ffb92014-04-03 14:43:30 -070042 }
Toshio Koidec87810e2014-02-11 13:03:21 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 /**
Toshio Koidefdb75932014-06-16 17:59:24 -070045 * Calculates shortest-path and constrained-shortest-path intents into low-level path intents.
Ray Milkey269ffb92014-04-03 14:43:30 -070046 *
47 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
48 * @param pathIntents a set of current low-level intents
49 * @return IntentOperationList. PathIntent and/or ErrorIntent instances.
50 */
Jonathan Hartc00f5c22014-06-10 15:14:40 -070051 public IntentOperationList calcPathIntents(final IntentOperationList intentOpList,
52 final IntentMap appIntents, final PathIntentMap pathIntents) {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 IntentOperationList pathIntentOpList = new IntentOperationList();
54 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koide59bc8ea2014-02-21 17:48:46 -080055
Jonathan Harte37e4e22014-05-13 19:12:02 -070056 // TODO optimize locking of Topology
57 topology.acquireReadLock();
Toshio Koidec87810e2014-02-11 13:03:21 -080058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 for (IntentOperation intentOp : intentOpList) {
60 switch (intentOp.operator) {
61 case ADD:
62 if (!(intentOp.intent instanceof ShortestPathIntent)) {
63 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
64 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
65 ErrorType.UNSUPPORTED_INTENT,
66 "Unsupported intent type.",
67 intentOp.intent));
68 continue;
69 }
Toshio Koidec87810e2014-02-11 13:03:21 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
Jonathan Harte37e4e22014-05-13 19:12:02 -070072 Switch srcSwitch = topology.getSwitch(spIntent.getSrcSwitchDpid());
73 Switch dstSwitch = topology.getSwitch(spIntent.getDstSwitchDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070074 if (srcSwitch == null || dstSwitch == null) {
Jonathan Hartd6108c52014-06-18 11:50:26 -070075 log.debug("Switch not found. src:{}, dst:{}",
Ray Milkey269ffb92014-04-03 14:43:30 -070076 spIntent.getSrcSwitchDpid(),
Jonathan Hartd6108c52014-06-18 11:50:26 -070077 spIntent.getDstSwitchDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070078 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
79 ErrorType.SWITCH_NOT_FOUND,
80 "Switch not found.",
81 spIntent));
82 continue;
83 }
Toshio Koidea10c0372014-02-20 17:28:10 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 double bandwidth = 0.0;
86 ConstrainedBFSTree tree = null;
87 if (spIntent instanceof ConstrainedShortestPathIntent) {
88 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
89 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
90 } else {
91 tree = spfTrees.get(srcSwitch);
92 if (tree == null) {
93 tree = new ConstrainedBFSTree(srcSwitch);
94 spfTrees.put(srcSwitch, tree);
95 }
96 }
97 Path path = tree.getPath(dstSwitch);
98 if (path == null) {
Jonathan Hartd6108c52014-06-18 11:50:26 -070099 log.debug("Path not found. Intent: {}", spIntent.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
101 ErrorType.PATH_NOT_FOUND,
102 "Path not found.",
103 spIntent));
104 continue;
105 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800106
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 // generate new path-intent ID
108 String oldPathIntentId = spIntent.getPathIntentId();
109 String newPathIntentId;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700110 if (oldPathIntentId == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700112 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800115
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 // create new path-intent
Toshio Koide7d3cee02014-06-05 18:56:19 -0700117 PathIntent newPathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
118 newPathIntent.setState(IntentState.INST_REQ);
119
120 // create and add operation(s)
121 if (oldPathIntentId == null) {
122 // operation for new path-intent
Brian O'Connora581b9d2014-06-15 23:32:36 -0700123 spIntent.setPathIntentId(newPathIntent);
Toshio Koide7d3cee02014-06-05 18:56:19 -0700124 pathIntentOpList.add(Operator.ADD, newPathIntent);
125 log.debug("new intent:{}", newPathIntent);
126 } else {
127 PathIntent oldPathIntent = (PathIntent) pathIntents.getIntent(oldPathIntentId);
128 if (newPathIntent.hasSameFields(oldPathIntent)) {
129 // skip the same operation (reroute)
130 spIntent.setState(IntentState.INST_ACK);
131 log.debug("skip intent:{}", newPathIntent);
132 } else {
133 // update existing path-intent (reroute)
Brian O'Connora581b9d2014-06-15 23:32:36 -0700134 spIntent.setPathIntentId(newPathIntent);
Toshio Koide7d3cee02014-06-05 18:56:19 -0700135 pathIntentOpList.add(Operator.REMOVE, oldPathIntent);
136 pathIntentOpList.add(Operator.ADD, newPathIntent);
137 log.debug("update intent:{} -> {}", oldPathIntent, newPathIntent);
138 }
139 }
Toshio Koide59bc8ea2014-02-21 17:48:46 -0800140
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 break;
142 case REMOVE:
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700143 ShortestPathIntent targetAppIntent =
144 (ShortestPathIntent) appIntents.getIntent(
145 intentOp.intent.getId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 if (targetAppIntent != null) {
147 String pathIntentId = targetAppIntent.getPathIntentId();
148 if (pathIntentId != null) {
149 Intent targetPathIntent = pathIntents.getIntent(pathIntentId);
150 if (targetPathIntent != null) {
151 pathIntentOpList.add(Operator.REMOVE, targetPathIntent);
152 }
153 }
154 }
155 break;
156 case ERROR:
157 // just ignore
158 break;
Ray Milkey0b122ed2014-04-14 10:06:03 -0700159 default:
160 log.error("Unknown intent operator {}", intentOp.operator);
161 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 }
163 }
Jonathan Harte37e4e22014-05-13 19:12:02 -0700164 // TODO optimize locking of Topology
165 topology.releaseReadLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700166
167 return pathIntentOpList;
168 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700169}