blob: d784439d75321558d8e0b90a55af7bb5325c4995 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koide5526c4f2014-02-11 12:39:03 -08002
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
Toshio Koide5526c4f2014-02-11 12:39:03 -08007
Jonathan Hart472062d2014-04-03 10:56:48 -07008import net.onrc.onos.core.topology.Link;
9import net.onrc.onos.core.topology.LinkEvent;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070010import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070011import net.onrc.onos.core.util.LinkTuple;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070012import net.onrc.onos.core.util.PortNumber;
13import net.onrc.onos.core.util.SwitchPort;
Toshio Koide5526c4f2014-02-11 12:39:03 -080014
15/**
Brian O'Connora581b9d2014-06-15 23:32:36 -070016 * In addition to maintaining the Intent ID to Intent mapping of its
17 * superclass, this class maintains a mapping from switch port to
18 * PathIntent. It is used to quickly identify Intents that are affected
19 * when a network event involves a particular switch port.
Toshio Koide5526c4f2014-02-11 12:39:03 -080020 */
Toshio Koide4f308732014-02-18 15:19:48 -080021public class PathIntentMap extends IntentMap {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070022 private final HashMap<Dpid, HashMap<PortNumber, HashSet<PathIntent>>> intents;
Toshio Koidea9078af2014-02-21 16:57:04 -080023
Brian O'Connora581b9d2014-06-15 23:32:36 -070024 /**
25 * Constructor.
26 */
Ray Milkey269ffb92014-04-03 14:43:30 -070027 public PathIntentMap() {
28 intents = new HashMap<>();
29 }
Toshio Koidea9078af2014-02-21 16:57:04 -080030
Brian O'Connora581b9d2014-06-15 23:32:36 -070031 /**
32 * Retrieve all PathIntents that contain the specified switch port.
33 *
34 * @param swPort the switch port to retrieve Intents for.
35 * @return a set of all intents that contain swPort
36 */
Ray Milkey269ffb92014-04-03 14:43:30 -070037 private HashSet<PathIntent> get(SwitchPort swPort) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070038 Dpid dpid = swPort.getDpid();
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070039 PortNumber port = swPort.getPortNumber();
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070040 HashMap<PortNumber, HashSet<PathIntent>> portToIntents = intents.get(dpid);
Ray Milkey269ffb92014-04-03 14:43:30 -070041 if (portToIntents == null) {
42 portToIntents = new HashMap<>();
43 intents.put(dpid, portToIntents);
44 }
45 HashSet<PathIntent> targetIntents = portToIntents.get(port);
46 if (targetIntents == null) {
47 targetIntents = new HashSet<>();
48 portToIntents.put(port, targetIntents);
49 }
50 return targetIntents;
51 }
Toshio Koidea9078af2014-02-21 16:57:04 -080052
Brian O'Connora581b9d2014-06-15 23:32:36 -070053 /**
54 * Add a PathIntent to a particular switch port.
55 *
56 * @param swPort switch port
57 * @param intent Path Intent
58 */
Ray Milkey269ffb92014-04-03 14:43:30 -070059 private void put(SwitchPort swPort, PathIntent intent) {
60 get(swPort).add(intent);
61 }
Toshio Koidec406e792014-02-14 16:52:42 -080062
Brian O'Connora581b9d2014-06-15 23:32:36 -070063 /**
64 * Add a PathIntent to the map. The function will automatically
65 * update the map with all switch ports contained in the Intent.
66 *
67 * @param intent the PathIntent
68 */
Ray Milkey269ffb92014-04-03 14:43:30 -070069 @Override
70 protected void putIntent(Intent intent) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070071 if (!(intent instanceof PathIntent)) {
72 return; // TODO throw exception
73 }
Ray Milkey269ffb92014-04-03 14:43:30 -070074 super.putIntent(intent);
Toshio Koidea9078af2014-02-21 16:57:04 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 PathIntent pathIntent = (PathIntent) intent;
77 for (LinkEvent linkEvent : pathIntent.getPath()) {
78 put(linkEvent.getSrc(), (PathIntent) intent);
79 put(linkEvent.getDst(), (PathIntent) intent);
80 }
81 }
Toshio Koidec406e792014-02-14 16:52:42 -080082
Brian O'Connora581b9d2014-06-15 23:32:36 -070083 /**
84 * Removes a PathIntent from the map, including all switch ports.
85 *
86 * @param intentId the ID of the PathIntent to be removed
87 */
Ray Milkey269ffb92014-04-03 14:43:30 -070088 @Override
89 protected void removeIntent(String intentId) {
90 PathIntent intent = (PathIntent) getIntent(intentId);
91 for (LinkEvent linkEvent : intent.getPath()) {
92 get(linkEvent.getSrc()).remove(intent);
93 get(linkEvent.getDst()).remove(intent);
94 }
95 super.removeIntent(intentId);
96 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080097
Brian O'Connora581b9d2014-06-15 23:32:36 -070098 /**
99 * Retrieve all intents that use a particular link.
100 *
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700101 * @param linkTuple the link tuple to look up
Brian O'Connora581b9d2014-06-15 23:32:36 -0700102 * @return a collection of PathIntents that use the link
103 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700104 public Collection<PathIntent> getIntentsByLink(LinkTuple linkTuple) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 return getIntentsByPort(
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700106 linkTuple.getSrc().getDpid(),
107 linkTuple.getSrc().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800109
Brian O'Connora581b9d2014-06-15 23:32:36 -0700110 /**
111 * Retrieve all intents that use a particular port.
112 *
113 * @param dpid the switch's DPID
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700114 * @param portNumber the switch's port
Brian O'Connora581b9d2014-06-15 23:32:36 -0700115 * @return a collection of PathIntents that use the port
116 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700117 public Collection<PathIntent> getIntentsByPort(Dpid dpid, PortNumber portNumber) {
118 HashMap<PortNumber, HashSet<PathIntent>> portToIntents = intents.get(dpid);
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 if (portToIntents != null) {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700120 HashSet<PathIntent> targetIntents = portToIntents.get(portNumber);
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 if (targetIntents != null) {
122 return Collections.unmodifiableCollection(targetIntents);
123 }
124 }
125 return new HashSet<>();
126 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800127
Brian O'Connora581b9d2014-06-15 23:32:36 -0700128 /**
129 * Retrieve all intents that use a particular switch.
130 *
131 * @param dpid the switch's DPID
132 * @return a collection of PathIntents that use the switch
133 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700134 public Collection<PathIntent> getIntentsByDpid(Dpid dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 HashSet<PathIntent> result = new HashSet<>();
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700136 HashMap<PortNumber, HashSet<PathIntent>> portToIntents = intents.get(dpid);
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 if (portToIntents != null) {
138 for (HashSet<PathIntent> targetIntents : portToIntents.values()) {
139 result.addAll(targetIntents);
140 }
141 }
142 return result;
143 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800144
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 /**
Brian O'Connora581b9d2014-06-15 23:32:36 -0700146 * Calculate available bandwidth of specified link.
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 *
Brian O'Connora581b9d2014-06-15 23:32:36 -0700148 * @param link the Link
149 * @return the available bandwidth
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 */
151 public Double getAvailableBandwidth(Link link) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700152 if (link == null) {
153 return null;
154 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 Double bandwidth = link.getCapacity();
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 if (!bandwidth.isInfinite()) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700157 for (PathIntent intent : getIntentsByLink(link.getLinkTuple())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 Double intentBandwidth = intent.getBandwidth();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 bandwidth -= intentBandwidth;
163 }
164 }
165 return bandwidth;
166 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800167}