blob: 8258a747826b89f6590a849b39c72806d8bbd1ea [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.controller.impl;
17
andreaed976a42015-10-05 14:38:25 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.collect.ImmutableList;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Service;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
Hyunsun Moon5fb20a52015-09-25 17:02:33 -070026import org.onlab.packet.TpPort;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070027import org.onosproject.ovsdb.controller.DefaultEventSubject;
28import org.onosproject.ovsdb.controller.EventSubject;
29import org.onosproject.ovsdb.controller.OvsdbClientService;
30import org.onosproject.ovsdb.controller.OvsdbConstant;
31import org.onosproject.ovsdb.controller.OvsdbController;
32import org.onosproject.ovsdb.controller.OvsdbDatapathId;
33import org.onosproject.ovsdb.controller.OvsdbEvent;
34import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
35import org.onosproject.ovsdb.controller.OvsdbEventListener;
36import org.onosproject.ovsdb.controller.OvsdbIfaceId;
37import org.onosproject.ovsdb.controller.OvsdbNodeId;
38import org.onosproject.ovsdb.controller.OvsdbNodeListener;
39import org.onosproject.ovsdb.controller.OvsdbPortName;
40import org.onosproject.ovsdb.controller.OvsdbPortNumber;
41import org.onosproject.ovsdb.controller.OvsdbPortType;
42import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
43import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
44import org.onosproject.ovsdb.rfc.message.TableUpdate;
45import org.onosproject.ovsdb.rfc.message.TableUpdates;
46import org.onosproject.ovsdb.rfc.message.UpdateNotification;
47import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
48import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
49import org.onosproject.ovsdb.rfc.notation.Row;
Jonathan Hart51539b82015-10-29 09:53:04 -070050import org.onosproject.ovsdb.rfc.notation.Uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070051import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
52import org.onosproject.ovsdb.rfc.table.Bridge;
53import org.onosproject.ovsdb.rfc.table.Interface;
54import org.onosproject.ovsdb.rfc.table.OvsdbTable;
55import org.onosproject.ovsdb.rfc.table.TableGenerator;
56import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
57import org.osgi.service.component.ComponentContext;
58import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
andreaed976a42015-10-05 14:38:25 -070061import java.math.BigInteger;
62import java.util.HashSet;
63import java.util.Iterator;
64import java.util.List;
65import java.util.Map;
66import java.util.Set;
67import java.util.concurrent.ConcurrentHashMap;
68import java.util.concurrent.CopyOnWriteArraySet;
69import java.util.concurrent.ExecutionException;
jaegonkim1af0ae52017-01-01 10:46:55 +090070import java.util.function.Consumer;
andreaed976a42015-10-05 14:38:25 -070071
72import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070073
74/**
75 * The implementation of OvsdbController.
76 */
77@Component(immediate = true)
78@Service
79public class OvsdbControllerImpl implements OvsdbController {
80
81 public static final Logger log = LoggerFactory
82 .getLogger(OvsdbControllerImpl.class);
83
84 protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
85 new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
86
87 protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
88 protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
89
90 protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
91 protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
92
93 protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
94 new ConcurrentHashMap<String, OvsdbClientService>();
95
96 protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
97
BitOhenryf006ddd2015-11-17 13:25:41 +080098 private final Controller controller = new Controller();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070099
100 @Activate
101 public void activate(ComponentContext context) {
102 controller.start(agent, updateCallback);
103 log.info("Started");
104 }
105
106 @Deactivate
107 public void deactivate() {
108 controller.stop();
109 log.info("Stoped");
110 }
111
112 @Override
113 public void addNodeListener(OvsdbNodeListener listener) {
114 if (!ovsdbNodeListener.contains(listener)) {
115 this.ovsdbNodeListener.add(listener);
116 }
117 }
118
119 @Override
120 public void removeNodeListener(OvsdbNodeListener listener) {
121 this.ovsdbNodeListener.remove(listener);
122 }
123
124 @Override
125 public void addOvsdbEventListener(OvsdbEventListener listener) {
126 if (!ovsdbEventListener.contains(listener)) {
127 this.ovsdbEventListener.add(listener);
128 }
129 }
130
131 @Override
132 public void removeOvsdbEventListener(OvsdbEventListener listener) {
133 this.ovsdbEventListener.remove(listener);
134 }
135
136 @Override
137 public List<OvsdbNodeId> getNodeIds() {
andreaed976a42015-10-05 14:38:25 -0700138 return ImmutableList.copyOf(ovsdbClients.keySet());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700139 }
140
141 @Override
142 public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
143 return ovsdbClients.get(nodeId);
144 }
145
Hyunsun Moon5fb20a52015-09-25 17:02:33 -0700146 @Override
147 public void connect(IpAddress ip, TpPort port) {
148 controller.connect(ip, port);
149 }
150
jaegonkim1af0ae52017-01-01 10:46:55 +0900151 @Override
152 public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) {
153 controller.connect(ip, port, failhandler);
154 }
155
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700156 /**
157 * Implementation of an Ovsdb Agent which is responsible for keeping track
158 * of connected node and the state in which they are.
159 */
160 private class InternalOvsdbNodeAgent implements OvsdbAgent {
161 @Override
162 public void addConnectedNode(OvsdbNodeId nodeId,
163 OvsdbClientService ovsdbClient) {
164
165 if (ovsdbClients.get(nodeId) != null) {
166 return;
167 } else {
168 ovsdbClients.put(nodeId, ovsdbClient);
169
170 try {
171 List<String> dbNames = ovsdbClient.listDbs().get();
172 for (String dbName : dbNames) {
173 DatabaseSchema dbSchema;
174 dbSchema = ovsdbClient.getOvsdbSchema(dbName).get();
175
176 log.debug("Begin to monitor tables");
177 String id = java.util.UUID.randomUUID().toString();
178 TableUpdates updates = ovsdbClient
179 .monitorTables(dbName, id).get();
180
181 requestDbName.put(id, dbName);
182 requestNotification.put(id, ovsdbClient);
183
184 if (updates != null) {
185 processTableUpdates(ovsdbClient, updates,
186 dbSchema.name());
187 }
188 }
189 } catch (InterruptedException e) {
190 log.warn("Interrupted while waiting to get message from ovsdb");
191 Thread.currentThread().interrupt();
192 } catch (ExecutionException e) {
193 log.error("Exception thrown while to get message from ovsdb");
194 }
195
196 log.debug("Add node to north");
197 for (OvsdbNodeListener l : ovsdbNodeListener) {
198 l.nodeAdded(nodeId);
199 }
200 return;
201 }
202 }
203
204 @Override
205 public void removeConnectedNode(OvsdbNodeId nodeId) {
206 ovsdbClients.remove(nodeId);
207 log.debug("Node connection is removed");
208 for (OvsdbNodeListener l : ovsdbNodeListener) {
209 l.nodeRemoved(nodeId);
210 }
211 }
212 }
213
214 /**
215 * Processes table updates.
216 *
217 * @param clientService OvsdbClientService instance
andreaed976a42015-10-05 14:38:25 -0700218 * @param updates TableUpdates instance
219 * @param dbName ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700220 */
221 private void processTableUpdates(OvsdbClientService clientService,
222 TableUpdates updates, String dbName)
223 throws InterruptedException {
224 checkNotNull(clientService, "OvsdbClientService is not null");
225
226 DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
227
228 for (String tableName : updates.result().keySet()) {
229 TableUpdate update = updates.result().get(tableName);
Jonathan Hart51539b82015-10-29 09:53:04 -0700230 for (Uuid uuid : (Set<Uuid>) update.rows().keySet()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700231 log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
232 uuid.value(), dbName, tableName);
233
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700234 Row newRow = update.getNew(uuid);
235 if (newRow != null) {
236 clientService.updateOvsdbStore(dbName, tableName,
237 uuid.value(), newRow);
238
239 if (OvsdbConstant.INTERFACE.equals(tableName)) {
240 dispatchInterfaceEvent(clientService,
CNluciusa66c3972015-09-06 20:31:29 +0800241 newRow,
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700242 OvsdbEvent.Type.PORT_ADDED,
243 dbSchema);
244 }
245 } else if (update.getOld(uuid) != null) {
CNluciusa66c3972015-09-06 20:31:29 +0800246 if (OvsdbConstant.INTERFACE.equals(tableName)) {
247 Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value());
248 dispatchInterfaceEvent(clientService,
249 row,
andreaed976a42015-10-05 14:38:25 -0700250 OvsdbEvent.Type.PORT_REMOVED,
251 dbSchema);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700252 }
CNluciusa66c3972015-09-06 20:31:29 +0800253 clientService.removeRow(dbName, tableName, uuid.value());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700254 }
255 }
256 }
257 }
258
259 /**
260 * Dispatches event to the north.
261 *
262 * @param clientService OvsdbClientService instance
andreaed976a42015-10-05 14:38:25 -0700263 * @param newRow a new row
264 * @param oldRow an old row
265 * @param eventType type of event
266 * @param dbSchema ovsdb database schema
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700267 */
268 private void dispatchInterfaceEvent(OvsdbClientService clientService,
CNluciusa66c3972015-09-06 20:31:29 +0800269 Row row,
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700270 Type eventType,
271 DatabaseSchema dbSchema) {
272
273 long dpid = getDataPathid(clientService, dbSchema);
274 Interface intf = (Interface) TableGenerator
CNluciusa66c3972015-09-06 20:31:29 +0800275 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700276 if (intf == null) {
277 return;
278 }
279
280 String portType = (String) intf.getTypeColumn().data();
281 long localPort = getOfPort(intf);
282 if (localPort < 0) {
283 return;
284 }
285 String[] macAndIfaceId = getMacAndIfaceid(intf);
286 if (macAndIfaceId == null) {
287 return;
288 }
289
290 EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
andreaed976a42015-10-05 14:38:25 -0700291 macAndIfaceId[0]),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700292 new HashSet<IpAddress>(),
293 new OvsdbPortName(intf
andreaed976a42015-10-05 14:38:25 -0700294 .getName()),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700295 new OvsdbPortNumber(localPort),
296 new OvsdbDatapathId(Long
andreaed976a42015-10-05 14:38:25 -0700297 .toString(dpid)),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700298 new OvsdbPortType(portType),
299 new OvsdbIfaceId(macAndIfaceId[1]));
300 for (OvsdbEventListener listener : ovsdbEventListener) {
301 listener.handle(new OvsdbEvent<EventSubject>(eventType,
302 eventSubject));
303 }
304 }
305
306 /**
307 * Gets mac and iface from the table Interface.
308 *
309 * @param intf Interface instance
310 * @return attachedMac, ifaceid
311 */
312 private String[] getMacAndIfaceid(Interface intf) {
313 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
314 @SuppressWarnings("unchecked")
315 Map<String, String> externalIds = ovsdbMap.map();
316 if (externalIds == null) {
317 log.warn("The external_ids is null");
318 return null;
319 }
320
321 String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
322 if (attachedMac == null) {
andreaed976a42015-10-05 14:38:25 -0700323 log.debug("The attachedMac is null"); //FIXME why always null?
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700324 return null;
325 }
326 String ifaceid = externalIds
327 .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
328 if (ifaceid == null) {
329 log.warn("The ifaceid is null");
330 return null;
331 }
andreaed976a42015-10-05 14:38:25 -0700332 return new String[]{attachedMac, ifaceid};
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700333 }
334
335 /**
336 * Gets ofPorts number from table Interface.
337 *
338 * @param intf Interface instance
339 * @return ofport the ofport number
340 */
341 private long getOfPort(Interface intf) {
342 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
343 @SuppressWarnings("unchecked")
344 Set<Integer> ofPorts = ofPortSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -0800345 while (ofPorts == null || ofPorts.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700346 log.debug("The ofport is null in {}", intf.getName());
347 return -1;
348 }
349 Iterator<Integer> it = ofPorts.iterator();
350 return Long.parseLong(it.next().toString());
351 }
352
353 /**
354 * Gets datapathid from table bridge.
355 *
356 * @param clientService OvsdbClientService instance
andreaed976a42015-10-05 14:38:25 -0700357 * @param dbSchema ovsdb database schema
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700358 * @return datapathid the bridge datapathid
359 */
360 private long getDataPathid(OvsdbClientService clientService,
361 DatabaseSchema dbSchema) {
362 String bridgeUuid = clientService
363 .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
364 if (bridgeUuid == null) {
365 log.debug("Unable to spot bridge uuid for {} in {}",
366 OvsdbConstant.INTEGRATION_BRIDGE, clientService);
367 return 0;
368 }
369
370 Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
371 "Bridge", bridgeUuid);
372 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
373 OvsdbTable.BRIDGE);
374 OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
375 @SuppressWarnings("unchecked")
376 Set<String> dpids = dpidSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -0800377 if (dpids == null || dpids.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700378 return 0;
379 }
380 return stringToLong((String) dpids.toArray()[0]);
381 }
382
383 private long stringToLong(String values) {
384 long value = (new BigInteger(values.replaceAll(":", ""), 16))
385 .longValue();
386 return value;
387 }
388
389 /**
390 * Implementation of an Callback which is responsible for receiving request
391 * infomation from ovsdb.
392 */
393 private class InternalMonitorCallBack implements Callback {
394 @Override
395 public void update(UpdateNotification updateNotification) {
396 Object key = updateNotification.jsonValue();
397 OvsdbClientService ovsdbClient = requestNotification.get(key);
398
399 String dbName = requestDbName.get(key);
400 JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
401 DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
402 TableUpdates updates = FromJsonUtil
403 .jsonNodeToTableUpdates(updatesJson, dbSchema);
404 try {
405 processTableUpdates(ovsdbClient, updates, dbName);
406 } catch (InterruptedException e) {
407 log.warn("Interrupted while processing table updates");
408 Thread.currentThread().interrupt();
409 }
410 }
411
412 @Override
413 public void locked(List<String> ids) {
414 // TODO Auto-generated method stub
415 }
416
417 @Override
418 public void stolen(List<String> ids) {
419 // TODO Auto-generated method stub
420 }
421
422 }
423
424}