blob: ea4b6a9d15577c00b691f0add172d13dadab0e43 [file] [log] [blame]
Toshio Koide2f570c12014-02-06 16:55:32 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
3import java.net.InetAddress;
4import java.util.Collection;
5import java.util.HashMap;
Toshio Koide6c9b7e82014-02-07 18:01:32 -08006import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -08007import java.util.LinkedList;
8
9import net.floodlightcontroller.util.MACAddress;
10
11/**
12 * @author Toshio Koide (t-koide@onlab.us)
13 */
14public class MutableNetworkGraph implements NetworkGraph {
15 protected HashMap<Long, SwitchImpl> switches;
Toshio Koide6c9b7e82014-02-07 18:01:32 -080016 protected HashMap<MACAddress, Device> devices;
Toshio Koide2f570c12014-02-06 16:55:32 -080017
18 public MutableNetworkGraph() {
19 switches = new HashMap<Long, SwitchImpl>();
Toshio Koide6c9b7e82014-02-07 18:01:32 -080020 devices = new HashMap<MACAddress, Device>();
Toshio Koide2f570c12014-02-06 16:55:32 -080021 }
22
23 // Switch operations
24
25 public Switch addSwitch(Long switchId) {
26 if (switches.containsKey(switchId)) {
27 return null; // should throw exception
28 }
29 SwitchImpl sw = new SwitchImpl(this, switchId);
30 switches.put(sw.getDpid(), sw);
31 return sw;
32
33 }
34
35 @Override
36 public Switch getSwitch(Long dpid) {
37 return switches.get(dpid);
38 }
39
40 @Override
41 public Iterable<? extends Switch> getSwitches() {
42 return switches.values();
43 }
44
45 // Link operations
46
47 public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
48 return new LinkImpl(
49 this,
50 getSwitch(srcDpid).getPort(srcPortNo),
51 getSwitch(dstDpid).getPort(dstPortNo));
52 }
53
54 public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
55 Link[] links = new Link[2];
56 links[0] = addLink(srcDpid, srcPortNo, dstDpid, dstPortNo);
57 links[1] = addLink(dstDpid, dstPortNo, srcDpid, srcPortNo);
58
59 return links;
60 }
61
62 @Override
63 public Collection<Link> getLinks() {
64 LinkedList<Link> links = new LinkedList<Link>();
65 for (Switch sw: switches.values()) {
66 for (Port port: sw.getPorts()) {
67 Link link = port.getOutgoingLink();
68 if (link != null) {
69 links.add(link);
70 }
71 }
72 }
73 return links;
74 }
75
76 @Override
77 public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) {
78 LinkedList<Link> links = new LinkedList<Link>();
79 for (Port port: getSwitch(dpid).getPorts()) {
80 Link link = port.getOutgoingLink();
81 if (link != null) {
82 links.add(link);
83 }
84 }
85 return links;
86 }
87
88 @Override
89 public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) {
90 LinkedList<Link> links = new LinkedList<Link>();
91 for (Port port: getSwitch(dpid).getPorts()) {
92 Link link = port.getIncomingLink();
93 if (link != null) {
94 links.add(link);
95 }
96 }
97 return links;
98 }
99
100 // Device operations
101
102 @Override
103 public Iterable<Device> getDeviceByIp(InetAddress ipAddress) {
Toshio Koide6c9b7e82014-02-07 18:01:32 -0800104 // TODO optimize it
105 HashSet<Device> result = new HashSet<Device>();
106 for (Device d: devices.values()) {
107 Collection<InetAddress> addrs = d.getIpAddress();
108 for (InetAddress addr: addrs) {
109 if (addr.equals(ipAddress)) {
110 result.add(d);
111 break;
112 }
113 }
114 }
115 return result;
Toshio Koide2f570c12014-02-06 16:55:32 -0800116 }
117
118 @Override
Toshio Koide6c9b7e82014-02-07 18:01:32 -0800119 public Device getDeviceByMac(MACAddress address) {
120 return devices.get(address);
121 }
122
123 public void addDevice(Device device) {
124 devices.put(device.getMacAddress(), device);
Toshio Koide2f570c12014-02-06 16:55:32 -0800125 }
126}