blob: 12c81c549a423f9eb3cf67799a7442ef005f48c1 [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;
10import net.onrc.onos.core.topology.PortEvent.SwitchPort;
Toshio Koide5526c4f2014-02-11 12:39:03 -080011
12/**
Brian O'Connora581b9d2014-06-15 23:32:36 -070013 * In addition to maintaining the Intent ID to Intent mapping of its
14 * superclass, this class maintains a mapping from switch port to
15 * PathIntent. It is used to quickly identify Intents that are affected
16 * when a network event involves a particular switch port.
Toshio Koide5526c4f2014-02-11 12:39:03 -080017 */
Toshio Koide4f308732014-02-18 15:19:48 -080018public class PathIntentMap extends IntentMap {
Brian O'Connora581b9d2014-06-15 23:32:36 -070019 private final HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents;
Toshio Koidea9078af2014-02-21 16:57:04 -080020
Brian O'Connora581b9d2014-06-15 23:32:36 -070021 /**
22 * Constructor.
23 */
Ray Milkey269ffb92014-04-03 14:43:30 -070024 public PathIntentMap() {
25 intents = new HashMap<>();
26 }
Toshio Koidea9078af2014-02-21 16:57:04 -080027
Brian O'Connora581b9d2014-06-15 23:32:36 -070028 /**
29 * Retrieve all PathIntents that contain the specified switch port.
30 *
31 * @param swPort the switch port to retrieve Intents for.
32 * @return a set of all intents that contain swPort
33 */
Ray Milkey269ffb92014-04-03 14:43:30 -070034 private HashSet<PathIntent> get(SwitchPort swPort) {
35 Long dpid = swPort.getDpid();
36 Long port = swPort.getNumber();
37 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
38 if (portToIntents == null) {
39 portToIntents = new HashMap<>();
40 intents.put(dpid, portToIntents);
41 }
42 HashSet<PathIntent> targetIntents = portToIntents.get(port);
43 if (targetIntents == null) {
44 targetIntents = new HashSet<>();
45 portToIntents.put(port, targetIntents);
46 }
47 return targetIntents;
48 }
Toshio Koidea9078af2014-02-21 16:57:04 -080049
Brian O'Connora581b9d2014-06-15 23:32:36 -070050 /**
51 * Add a PathIntent to a particular switch port.
52 *
53 * @param swPort switch port
54 * @param intent Path Intent
55 */
Ray Milkey269ffb92014-04-03 14:43:30 -070056 private void put(SwitchPort swPort, PathIntent intent) {
57 get(swPort).add(intent);
58 }
Toshio Koidec406e792014-02-14 16:52:42 -080059
Brian O'Connora581b9d2014-06-15 23:32:36 -070060 /**
61 * Add a PathIntent to the map. The function will automatically
62 * update the map with all switch ports contained in the Intent.
63 *
64 * @param intent the PathIntent
65 */
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @Override
67 protected void putIntent(Intent intent) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070068 if (!(intent instanceof PathIntent)) {
69 return; // TODO throw exception
70 }
Ray Milkey269ffb92014-04-03 14:43:30 -070071 super.putIntent(intent);
Toshio Koidea9078af2014-02-21 16:57:04 -080072
Ray Milkey269ffb92014-04-03 14:43:30 -070073 PathIntent pathIntent = (PathIntent) intent;
74 for (LinkEvent linkEvent : pathIntent.getPath()) {
75 put(linkEvent.getSrc(), (PathIntent) intent);
76 put(linkEvent.getDst(), (PathIntent) intent);
77 }
78 }
Toshio Koidec406e792014-02-14 16:52:42 -080079
Brian O'Connora581b9d2014-06-15 23:32:36 -070080 /**
81 * Removes a PathIntent from the map, including all switch ports.
82 *
83 * @param intentId the ID of the PathIntent to be removed
84 */
Ray Milkey269ffb92014-04-03 14:43:30 -070085 @Override
86 protected void removeIntent(String intentId) {
87 PathIntent intent = (PathIntent) getIntent(intentId);
88 for (LinkEvent linkEvent : intent.getPath()) {
89 get(linkEvent.getSrc()).remove(intent);
90 get(linkEvent.getDst()).remove(intent);
91 }
92 super.removeIntent(intentId);
93 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080094
Brian O'Connora581b9d2014-06-15 23:32:36 -070095 /**
96 * Retrieve all intents that use a particular link.
97 *
98 * @param linkEvent the link to look up
99 * @return a collection of PathIntents that use the link
100 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) {
102 return getIntentsByPort(
103 linkEvent.getSrc().getDpid(),
104 linkEvent.getSrc().getNumber());
105 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800106
Brian O'Connora581b9d2014-06-15 23:32:36 -0700107 /**
108 * Retrieve all intents that use a particular port.
109 *
110 * @param dpid the switch's DPID
111 * @param port the switch's port
112 * @return a collection of PathIntents that use the port
113 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) {
115 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
116 if (portToIntents != null) {
117 HashSet<PathIntent> targetIntents = portToIntents.get(port);
118 if (targetIntents != null) {
119 return Collections.unmodifiableCollection(targetIntents);
120 }
121 }
122 return new HashSet<>();
123 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800124
Brian O'Connora581b9d2014-06-15 23:32:36 -0700125 /**
126 * Retrieve all intents that use a particular switch.
127 *
128 * @param dpid the switch's DPID
129 * @return a collection of PathIntents that use the switch
130 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 public Collection<PathIntent> getIntentsByDpid(Long dpid) {
132 HashSet<PathIntent> result = new HashSet<>();
133 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
134 if (portToIntents != null) {
135 for (HashSet<PathIntent> targetIntents : portToIntents.values()) {
136 result.addAll(targetIntents);
137 }
138 }
139 return result;
140 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800141
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 /**
Brian O'Connora581b9d2014-06-15 23:32:36 -0700143 * Calculate available bandwidth of specified link.
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 *
Brian O'Connora581b9d2014-06-15 23:32:36 -0700145 * @param link the Link
146 * @return the available bandwidth
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 */
148 public Double getAvailableBandwidth(Link link) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700149 if (link == null) {
150 return null;
151 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 Double bandwidth = link.getCapacity();
153 LinkEvent linkEvent = new LinkEvent(link);
154 if (!bandwidth.isInfinite()) {
155 for (PathIntent intent : getIntentsByLink(linkEvent)) {
156 Double intentBandwidth = intent.getBandwidth();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700157 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 bandwidth -= intentBandwidth;
161 }
162 }
163 return bandwidth;
164 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800165}