blob: 85ea16c68b3e92cb33305ba061f45598a72b21f8 [file] [log] [blame]
Toshio Koide5526c4f2014-02-11 12:39:03 -08001package net.onrc.onos.intent;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
Toshio Koide5526c4f2014-02-11 12:39:03 -08007
8import net.onrc.onos.ofcontroller.networkgraph.Link;
Toshio Koide0c9106d2014-02-19 15:26:38 -08009import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidea9078af2014-02-21 16:57:04 -080010import net.onrc.onos.ofcontroller.networkgraph.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 {
Toshio Koidea9078af2014-02-21 16:57:04 -080016 private HashMap<Long, HashMap<Long, HashSet<PathIntent>>> intents;
17
18 public PathIntentMap() {
19 intents = new HashMap<>();
20 }
21
22 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 }
37
38 private void put(SwitchPort swPort, PathIntent intent) {
39 get(swPort).add(intent);
40 }
Toshio Koidec406e792014-02-14 16:52:42 -080041
Toshio Koide0c9106d2014-02-19 15:26:38 -080042 @Override
43 protected void putIntent(Intent intent) {
Toshio Koidea9078af2014-02-21 16:57:04 -080044 if (!(intent instanceof PathIntent)) return; // TODO throw exception
Toshio Koide0c9106d2014-02-19 15:26:38 -080045 super.putIntent(intent);
Toshio Koidea9078af2014-02-21 16:57:04 -080046
47 PathIntent pathIntent = (PathIntent) intent;
48 for (LinkEvent linkEvent: pathIntent.getPath()) {
49 put(linkEvent.getSrc(), (PathIntent) intent);
50 put(linkEvent.getDst(), (PathIntent) intent);
Toshio Koide5526c4f2014-02-11 12:39:03 -080051 }
52 }
Toshio Koidec406e792014-02-14 16:52:42 -080053
Toshio Koide0c9106d2014-02-19 15:26:38 -080054 @Override
55 protected void removeIntent(String intentId) {
56 PathIntent intent = (PathIntent) getIntent(intentId);
Toshio Koided9fa2a82014-02-19 17:35:18 -080057 for (LinkEvent linkEvent: intent.getPath()) {
Toshio Koidea9078af2014-02-21 16:57:04 -080058 get(linkEvent.getSrc()).remove(intent);
59 get(linkEvent.getDst()).remove(intent);
Toshio Koideba291c52014-02-11 20:26:51 -080060 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080061 super.removeIntent(intentId);
Toshio Koideba291c52014-02-11 20:26:51 -080062 }
Toshio Koide5526c4f2014-02-11 12:39:03 -080063
Toshio Koide0c9106d2014-02-19 15:26:38 -080064 public Collection<PathIntent> getIntentsByLink(LinkEvent linkEvent) {
Toshio Koidea9078af2014-02-21 16:57:04 -080065 return getIntentsByPort(
66 linkEvent.getSrc().getDpid(),
67 linkEvent.getSrc().getNumber());
68 }
69
70 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 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080077 }
Toshio Koidea9078af2014-02-21 16:57:04 -080078 return new HashSet<>();
79 }
80
81 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 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080088 }
Toshio Koidea9078af2014-02-21 16:57:04 -080089 return result;
Toshio Koide5526c4f2014-02-11 12:39:03 -080090 }
91
Toshio Koide5526c4f2014-02-11 12:39:03 -080092 /**
93 * calculate available bandwidth of specified link
94 * @param link
95 * @return
96 */
97 public Double getAvailableBandwidth(Link link) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080098 if (link == null) return null;
Toshio Koide5526c4f2014-02-11 12:39:03 -080099 Double bandwidth = link.getCapacity();
Toshio Koide0c9106d2014-02-19 15:26:38 -0800100 LinkEvent linkEvent = new LinkEvent(link);
Toshio Koidea9078af2014-02-21 16:57:04 -0800101 if (!bandwidth.isInfinite()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -0800102 for (PathIntent intent: getIntentsByLink(linkEvent)) {
Toshio Koide5526c4f2014-02-11 12:39:03 -0800103 Double intentBandwidth = intent.getBandwidth();
104 if (intentBandwidth == null || intentBandwidth.isInfinite() || intentBandwidth.isNaN())
105 continue;
106 bandwidth -= intentBandwidth;
107 }
108 }
109 return bandwidth;
110 }
111}