blob: 815958fdd1c2b8de9fb1d79401405ee114de1e56 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
3import java.util.ArrayList;
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08004import java.util.Collection;
Jonathan Harta99ec672014-04-03 11:30:34 -07005import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -08006
Jonathan Hart6df90172014-04-03 10:13:11 -07007import net.onrc.onos.core.datastore.DataStoreClient;
8import net.onrc.onos.core.datastore.IKVClient;
9import net.onrc.onos.core.datastore.topology.KVDevice;
10import net.onrc.onos.core.datastore.topology.KVLink;
11import net.onrc.onos.core.datastore.topology.KVPort;
Jonathan Hart6df90172014-04-03 10:13:11 -070012import net.onrc.onos.core.datastore.topology.KVPort.STATUS;
Jonathan Harta99ec672014-04-03 11:30:34 -070013import net.onrc.onos.core.datastore.topology.KVSwitch;
Jonathan Hart6df90172014-04-03 10:13:11 -070014import net.onrc.onos.core.datastore.utils.KVObject;
15import net.onrc.onos.core.datastore.utils.KVObject.WriteOp;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070016import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart062a2e82014-02-03 09:41:57 -080017
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
Jonathan Hart062a2e82014-02-03 09:41:57 -080021/**
Jonathan Harte37e4e22014-05-13 19:12:02 -070022 * Contains methods which write topology events into the key-value data store.
Jonathan Hart062a2e82014-02-03 09:41:57 -080023 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070024public class TopologyDatastore {
25 private static final Logger log = LoggerFactory.getLogger(TopologyDatastore.class);
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 /**
28 * Add a switch to the database.
29 *
30 * @param sw the switch to add.
31 * @param portEvents the corresponding switch ports to add.
32 * @return true on success, otherwise false.
33 */
34 public boolean addSwitch(SwitchEvent sw,
35 Collection<PortEvent> portEvents) {
36 log.debug("Adding switch {}", sw);
37 ArrayList<WriteOp> groupOp = new ArrayList<>();
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080038
Ray Milkey269ffb92014-04-03 14:43:30 -070039 KVSwitch rcSwitch = new KVSwitch(sw.getDpid());
40 rcSwitch.setStatus(KVSwitch.STATUS.ACTIVE);
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 IKVClient client = DataStoreClient.getClient();
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 // XXX Is ForceCreating Switch on DB OK here?
45 // If ForceCreating, who ever is calling this method needs
46 // to assure that DPID is unique cluster-wide, etc.
47 groupOp.add(rcSwitch.forceCreateOp(client));
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 for (PortEvent portEvent : portEvents) {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070050 KVPort rcPort = new KVPort(sw.getDpid(), portEvent.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -070051 rcPort.setStatus(KVPort.STATUS.ACTIVE);
Jonathan Hart69864df2014-02-13 10:44:12 -080052
Ray Milkey269ffb92014-04-03 14:43:30 -070053 groupOp.add(rcPort.forceCreateOp(client));
54 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 boolean failed = KVObject.multiWrite(groupOp);
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 if (failed) {
59 log.error("Adding Switch {} and its ports failed.", sw.getDpid());
60 for (WriteOp op : groupOp) {
Yuta HIGUCHIc66073c2014-04-25 09:58:57 -070061 log.debug("Operation:{} for {} - Result:{}", op.getOperation(), op.getObject(), op.getStatus());
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 // If we changed the operation from ForceCreate to
64 // Conditional operation (Create/Update) then we should retry here.
65 }
66 }
67 return !failed;
68 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 /**
71 * Update a switch as inactive in the database.
72 *
73 * @param sw the switch to update.
74 * @param portEvents the corresponding switch ports to update.
75 * @return true on success, otherwise false.
76 */
77 public boolean deactivateSwitch(SwitchEvent sw,
78 Collection<PortEvent> portEvents) {
79 log.debug("Deactivating switch {}", sw);
80 KVSwitch rcSwitch = new KVSwitch(sw.getDpid());
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 IKVClient client = DataStoreClient.getClient();
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 List<WriteOp> groupOp = new ArrayList<>();
85 rcSwitch.setStatus(KVSwitch.STATUS.INACTIVE);
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 groupOp.add(rcSwitch.forceCreateOp(client));
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 for (PortEvent portEvent : portEvents) {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070090 KVPort rcPort = new KVPort(sw.getDpid(), portEvent.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -070091 rcPort.setStatus(KVPort.STATUS.INACTIVE);
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 groupOp.add(rcPort.forceCreateOp(client));
94 }
Pavlin Radoslavovca529072014-02-19 14:07:52 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 boolean failed = KVObject.multiWrite(groupOp);
Pavlin Radoslavov018d5332014-02-19 23:08:35 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 return !failed;
99 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 /**
102 * Add a port to the database.
103 *
104 * @param port the port to add.
105 * @return true on success, otherwise false.
106 */
107 public boolean addPort(PortEvent port) {
108 log.debug("Adding port {}", port);
Jonathan Hart4c263272014-02-13 17:41:05 -0800109
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700110 KVPort rcPort = new KVPort(port.getDpid(), port.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 rcPort.setStatus(KVPort.STATUS.ACTIVE);
112 rcPort.forceCreate();
113 // TODO add description into KVPort
114 //rcPort.setDescription(port.getDescription());
Jonathan Hart4c263272014-02-13 17:41:05 -0800115
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 return true;
117 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800118
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 /**
120 * Update a port as inactive in the database.
121 *
122 * @param port the port to update.
123 * @return true on success, otherwise false.
124 */
125 public boolean deactivatePort(PortEvent port) {
126 log.debug("Deactivating port {}", port);
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -0800127
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700128 KVPort rcPort = new KVPort(port.getDpid(), port.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 rcPort.setStatus(STATUS.INACTIVE);
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -0800130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 rcPort.forceCreate();
Jonathan Hart4c263272014-02-13 17:41:05 -0800132
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 return true;
134 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800135
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 /**
137 * Add a link to the database.
138 *
139 * @param link the link to add.
140 * @return true on success, otherwise false.
141 */
142 public boolean addLink(LinkEvent link) {
143 log.debug("Adding link {}", link);
Yuta HIGUCHI0a4bd192014-02-17 13:52:34 -0800144
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 KVLink rcLink = new KVLink(link.getSrc().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700146 link.getSrc().getPortNumber(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 link.getDst().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700148 link.getDst().getPortNumber());
Jonathan Hart369875b2014-02-13 10:00:31 -0800149
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 // XXX This method is called only by discovery,
151 // which means what we are trying to write currently is the truth
152 // so we can force write here
153 //
154 // TODO: We need to check for errors
155 rcLink.setStatus(KVLink.STATUS.ACTIVE);
156 rcLink.forceCreate();
Pavlin Radoslavovca529072014-02-19 14:07:52 -0800157
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 return true; // Success
159 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800160
Ray Milkey269ffb92014-04-03 14:43:30 -0700161 public boolean removeLink(LinkEvent linkEvent) {
162 log.debug("Removing link {}", linkEvent);
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -0800163
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700164 KVLink rcLink = new KVLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getPortNumber(),
165 linkEvent.getDst().getDpid(), linkEvent.getDst().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 rcLink.forceDelete();
Jonathan Hart369875b2014-02-13 10:00:31 -0800167
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 return true;
169 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800170
Ray Milkey269ffb92014-04-03 14:43:30 -0700171 /**
172 * Add a device to the database.
173 *
174 * @param device the device to add.
175 * @return true on success, otherwise false.
176 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700177 public boolean addHost(HostEvent device) {
178 log.debug("Adding host into DB. mac {}", device.getMac());
TeruUd1c5b652014-03-24 13:58:46 -0700179
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 KVDevice rcDevice = new KVDevice(device.getMac().toBytes());
TeruUd1c5b652014-03-24 13:58:46 -0700181
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 for (SwitchPort sp : device.getAttachmentPoints()) {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700183 byte[] portId = KVPort.getPortID(sp.getDpid(), sp.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 rcDevice.addPortId(portId);
185 }
Yuta HIGUCHIb4335ad2014-02-05 09:56:07 -0800186
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 rcDevice.forceCreate();
188
189 return true;
190 }
191
192 /**
193 * Remove a device from the database.
194 *
195 * @param device the device to remove.
196 * @return true on success, otherwise false.
197 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700198 public boolean removeHost(HostEvent device) {
199 log.debug("Removing host into DB. mac {}", device.getMac());
Ray Milkey269ffb92014-04-03 14:43:30 -0700200
201 KVDevice rcDevice = new KVDevice(device.getMac().toBytes());
202 rcDevice.forceDelete();
203
204 return true;
205 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800206}