blob: ad6b1a5f9decd291950781ca471fe1a12f23d96b [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) {
44 if (!(intent instanceof PathIntent)) return; // TODO throw exception
45 super.putIntent(intent);
Toshio Koidea9078af2014-02-21 16:57:04 -080046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 PathIntent pathIntent = (PathIntent) intent;
48 for (LinkEvent linkEvent : pathIntent.getPath()) {
49 put(linkEvent.getSrc(), (PathIntent) intent);
50 put(linkEvent.getDst(), (PathIntent) intent);
51 }
52 }
Toshio Koidec406e792014-02-14 16:52:42 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 @Override
55 protected void removeIntent(String intentId) {
56 PathIntent intent = (PathIntent) getIntent(intentId);
57 for (LinkEvent linkEvent : intent.getPath()) {
58 get(linkEvent.getSrc()).remove(intent);
59 get(linkEvent.getDst()).remove(intent);
60 }
61 super.removeIntent(intentId);
62 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) {
65 return getIntentsByPort(
66 linkEvent.getSrc().getDpid(),
67 linkEvent.getSrc().getNumber());
68 }
Toshio Koidea9078af2014-02-21 16:57:04 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 public Collection<PathIntent> getIntentsByPort(Long dpid, Long port) {
71 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
72 if (portToIntents != null) {
73 HashSet<PathIntent> targetIntents = portToIntents.get(port);
74 if (targetIntents != null) {
75 return Collections.unmodifiableCollection(targetIntents);
76 }
77 }
78 return new HashSet<>();
79 }
Toshio Koidea9078af2014-02-21 16:57:04 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 public Collection<PathIntent> getIntentsByDpid(Long dpid) {
82 HashSet<PathIntent> result = new HashSet<>();
83 HashMap<Long, HashSet<PathIntent>> portToIntents = intents.get(dpid);
84 if (portToIntents != null) {
85 for (HashSet<PathIntent> targetIntents : portToIntents.values()) {
86 result.addAll(targetIntents);
87 }
88 }
89 return result;
90 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080091
Ray Milkey269ffb92014-04-03 14:43:30 -070092 /**
93 * calculate available bandwidth of specified link
94 *
95 * @param link
96 * @return
97 */
98 public Double getAvailableBandwidth(Link link) {
99 if (link == null) return null;
100 Double bandwidth = link.getCapacity();
101 LinkEvent linkEvent = new LinkEvent(link);
102 if (!bandwidth.isInfinite()) {
103 for (PathIntent intent : getIntentsByLink(linkEvent)) {
104 Double intentBandwidth = intent.getBandwidth();
105 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
106 continue;
107 bandwidth -= intentBandwidth;
108 }
109 }
110 return bandwidth;
111 }
Toshio Koide5526c4f2014-02-11 12:39:03 -0800112}