blob: a062b61e915a41dc7605e6f66fc0960ce95a5d00 [file] [log] [blame]
Toshio Koide5f260652014-01-30 14:41:12 -08001package net.onrc.onos.ofcontroller.app;
2
3import java.util.Collection;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.LinkedList;
7
8/**
9 * This code is valid for the architectural study purpose only.
10 * @author Toshio Koide (t-koide@onlab.us)
11 */
12public class NetworkGraph {
13 protected HashSet<Flow> flows;
14 protected HashMap<String, Switch> switches;
15
16 public NetworkGraph() {
17 flows = new HashSet<Flow>();
18 switches = new HashMap<String, Switch>();
19 }
20
21 // Switch operations
22
23 public Switch addSwitch(String name) {
24 if (switches.containsKey(name)) {
25 return null; // should throw exception
26 }
27 Switch sw = new Switch(this, name);
28 switches.put(sw.getName(), sw);
29 return sw;
30
31 }
32
33 public Switch getSwitch(String switchName) {
34 return switches.get(switchName);
35 }
36
37 // Link operations
38
39 public Link addLink(String srcSwitchName, Integer srcPortNo, String dstSwitchName, Integer dstPortNo) {
40 return new Link(
41 getSwitch(srcSwitchName).getPort(srcPortNo),
42 getSwitch(dstSwitchName).getPort(dstPortNo));
43 }
44
45 public Link[] addBidirectionalLinks(String srcSwitchName, Integer srcPortNo, String dstSwitchName, Integer dstPortNo) {
46 Link[] links = new Link[2];
47 links[0] = addLink(srcSwitchName, srcPortNo, dstSwitchName, dstPortNo);
48 links[1] = addLink(dstSwitchName, dstPortNo, srcSwitchName, srcPortNo);
49
50 return links;
51 }
52
53 public Collection<Link> getLinks() {
54 LinkedList<Link> links = new LinkedList<Link>();
55 for (Switch sw: switches.values()) {
56 for (SwitchPort port: sw.getPorts()) {
57 Link link = port.outgoingLink;
58 if (link != null) {
59 links.add(link);
60 }
61 }
62 }
63 return links;
64 }
65
66 // Flow operations
67
68 public boolean addFlow(Flow flow) {
69 return flows.add(flow);
70 }
71}