blob: de1f6e2784e4bd0ad047fe7613ba705cb152ed48 [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/**
13 * @author Toshio Koide (t-koide@onlab.us)
14 */
Toshio Koide4f308732014-02-18 15:19:48 -080015public class PathIntentMap extends IntentMap {
Ray Milkey269ffb92014-04-03 14:43:30 -070016 private HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents;
Toshio Koidea9078af2014-02-21 16:57:04 -080017
Ray Milkey269ffb92014-04-03 14:43:30 -070018 public PathIntentMap() {
19 intents = new HashMap<>();
20 }
Toshio Koidea9078af2014-02-21 16:57:04 -080021
Ray Milkey269ffb92014-04-03 14:43:30 -070022 private HashSet<PathIntent> get(SwitchPort swPort) {
23 Long dpid = swPort.getDpid();
24 Long port = swPort.getNumber();
25 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
26 if (portToIntents == null) {
27 portToIntents = new HashMap<>();
28 intents.put(dpid, portToIntents);
29 }
30 HashSet<PathIntent> targetIntents = portToIntents.get(port);
31 if (targetIntents == null) {
32 targetIntents = new HashSet<>();
33 portToIntents.put(port, targetIntents);
34 }
35 return targetIntents;
36 }
Toshio Koidea9078af2014-02-21 16:57:04 -080037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 private void put(SwitchPort swPort, PathIntent intent) {
39 get(swPort).add(intent);
40 }
Toshio Koidec406e792014-02-14 16:52:42 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
43 protected void putIntent(Intent intent) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070044 if (!(intent instanceof PathIntent)) {
45 return; // TODO throw exception
46 }
Ray Milkey269ffb92014-04-03 14:43:30 -070047 super.putIntent(intent);
Toshio Koidea9078af2014-02-21 16:57:04 -080048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 PathIntent pathIntent = (PathIntent) intent;
50 for (LinkEvent linkEvent : pathIntent.getPath()) {
51 put(linkEvent.getSrc(), (PathIntent) intent);
52 put(linkEvent.getDst(), (PathIntent) intent);
53 }
54 }
Toshio Koidec406e792014-02-14 16:52:42 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @Override
57 protected void removeIntent(String intentId) {
58 PathIntent intent = (PathIntent) getIntent(intentId);
59 for (LinkEvent linkEvent : intent.getPath()) {
60 get(linkEvent.getSrc()).remove(intent);
61 get(linkEvent.getDst()).remove(intent);
62 }
63 super.removeIntent(intentId);
64 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) {
67 return getIntentsByPort(
68 linkEvent.getSrc().getDpid(),
69 linkEvent.getSrc().getNumber());
70 }
Toshio Koidea9078af2014-02-21 16:57:04 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) {
73 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
74 if (portToIntents != null) {
75 HashSet<PathIntent> targetIntents = portToIntents.get(port);
76 if (targetIntents != null) {
77 return Collections.unmodifiableCollection(targetIntents);
78 }
79 }
80 return new HashSet<>();
81 }
Toshio Koidea9078af2014-02-21 16:57:04 -080082
Ray Milkey269ffb92014-04-03 14:43:30 -070083 public Collection<PathIntent> getIntentsByDpid(Long dpid) {
84 HashSet<PathIntent> result = new HashSet<>();
85 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
86 if (portToIntents != null) {
87 for (HashSet<PathIntent> targetIntents : portToIntents.values()) {
88 result.addAll(targetIntents);
89 }
90 }
91 return result;
92 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070095 * calculate available bandwidth of specified link.
Ray Milkey269ffb92014-04-03 14:43:30 -070096 *
97 * @param link
98 * @return
99 */
100 public Double getAvailableBandwidth(Link link) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 if (link == null) {
102 return null;
103 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 Double bandwidth = link.getCapacity();
105 LinkEvent linkEvent = new LinkEvent(link);
106 if (!bandwidth.isInfinite()) {
107 for (PathIntent intent : getIntentsByLink(linkEvent)) {
108 Double intentBandwidth = intent.getBandwidth();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700109 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700111 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 bandwidth -= intentBandwidth;
113 }
114 }
115 return bandwidth;
116 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800117}