9b482968640f8bb5dc23c13f31e30796f2eaf827
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
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  */
16 package org.onosproject.ovsdb.controller.impl;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 import java.math.BigInteger;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.concurrent.ExecutionException;
29
30 import org.apache.felix.scr.annotations.Activate;
31 import org.apache.felix.scr.annotations.Component;
32 import org.apache.felix.scr.annotations.Deactivate;
33 import org.apache.felix.scr.annotations.Service;
34 import org.onlab.packet.IpAddress;
35 import org.onlab.packet.MacAddress;
36 import org.onosproject.ovsdb.controller.DefaultEventSubject;
37 import org.onosproject.ovsdb.controller.EventSubject;
38 import org.onosproject.ovsdb.controller.OvsdbClientService;
39 import org.onosproject.ovsdb.controller.OvsdbConstant;
40 import org.onosproject.ovsdb.controller.OvsdbController;
41 import org.onosproject.ovsdb.controller.OvsdbDatapathId;
42 import org.onosproject.ovsdb.controller.OvsdbEvent;
43 import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
44 import org.onosproject.ovsdb.controller.OvsdbEventListener;
45 import org.onosproject.ovsdb.controller.OvsdbIfaceId;
46 import org.onosproject.ovsdb.controller.OvsdbNodeId;
47 import org.onosproject.ovsdb.controller.OvsdbNodeListener;
48 import org.onosproject.ovsdb.controller.OvsdbPortName;
49 import org.onosproject.ovsdb.controller.OvsdbPortNumber;
50 import org.onosproject.ovsdb.controller.OvsdbPortType;
51 import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
52 import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
53 import org.onosproject.ovsdb.rfc.message.TableUpdate;
54 import org.onosproject.ovsdb.rfc.message.TableUpdates;
55 import org.onosproject.ovsdb.rfc.message.UpdateNotification;
56 import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
57 import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
58 import org.onosproject.ovsdb.rfc.notation.Row;
59 import org.onosproject.ovsdb.rfc.notation.UUID;
60 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
61 import org.onosproject.ovsdb.rfc.table.Bridge;
62 import org.onosproject.ovsdb.rfc.table.Interface;
63 import org.onosproject.ovsdb.rfc.table.OvsdbTable;
64 import org.onosproject.ovsdb.rfc.table.TableGenerator;
65 import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
66 import org.osgi.service.component.ComponentContext;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 import com.fasterxml.jackson.databind.JsonNode;
71
72 /**
73  * The implementation of OvsdbController.
74  */
75 @Component(immediate = true)
76 @Service
77 public class OvsdbControllerImpl implements OvsdbController {
78
79     public static final Logger log = LoggerFactory
80             .getLogger(OvsdbControllerImpl.class);
81
82     protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
83             new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
84
85     protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
86     protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
87
88     protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
89     protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
90
91     protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
92             new ConcurrentHashMap<String, OvsdbClientService>();
93
94     protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
95
96     private final Controller controller = new Controller();
97
98     @Activate
99     public void activate(ComponentContext context) {
100         controller.start(agent, updateCallback);
101         log.info("Started");
102     }
103
104     @Deactivate
105     public void deactivate() {
106         controller.stop();
107         log.info("Stoped");
108     }
109
110     @Override
111     public void addNodeListener(OvsdbNodeListener listener) {
112         if (!ovsdbNodeListener.contains(listener)) {
113             this.ovsdbNodeListener.add(listener);
114         }
115     }
116
117     @Override
118     public void removeNodeListener(OvsdbNodeListener listener) {
119         this.ovsdbNodeListener.remove(listener);
120     }
121
122     @Override
123     public void addOvsdbEventListener(OvsdbEventListener listener) {
124         if (!ovsdbEventListener.contains(listener)) {
125             this.ovsdbEventListener.add(listener);
126         }
127     }
128
129     @Override
130     public void removeOvsdbEventListener(OvsdbEventListener listener) {
131         this.ovsdbEventListener.remove(listener);
132     }
133
134     @Override
135     public List<OvsdbNodeId> getNodeIds() {
136         // TODO Auto-generated method stub
137         return null;
138     }
139
140     @Override
141     public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
142         return ovsdbClients.get(nodeId);
143     }
144
145     /**
146      * Implementation of an Ovsdb Agent which is responsible for keeping track
147      * of connected node and the state in which they are.
148      */
149     private class InternalOvsdbNodeAgent implements OvsdbAgent {
150         @Override
151         public void addConnectedNode(OvsdbNodeId nodeId,
152                                      OvsdbClientService ovsdbClient) {
153
154             if (ovsdbClients.get(nodeId) != null) {
155                 return;
156             } else {
157                 ovsdbClients.put(nodeId, ovsdbClient);
158
159                 try {
160                     List<String> dbNames = ovsdbClient.listDbs().get();
161                     for (String dbName : dbNames) {
162                         DatabaseSchema dbSchema;
163                         dbSchema = ovsdbClient.getOvsdbSchema(dbName).get();
164
165                         log.debug("Begin to monitor tables");
166                         String id = java.util.UUID.randomUUID().toString();
167                         TableUpdates updates = ovsdbClient
168                                 .monitorTables(dbName, id).get();
169
170                         requestDbName.put(id, dbName);
171                         requestNotification.put(id, ovsdbClient);
172
173                         if (updates != null) {
174                             processTableUpdates(ovsdbClient, updates,
175                                                 dbSchema.name());
176                         }
177                     }
178                 } catch (InterruptedException e) {
179                     log.warn("Interrupted while waiting to get message from ovsdb");
180                     Thread.currentThread().interrupt();
181                 } catch (ExecutionException e) {
182                     log.error("Exception thrown while to get message from ovsdb");
183                 }
184
185                 log.debug("Add node to north");
186                 for (OvsdbNodeListener l : ovsdbNodeListener) {
187                     l.nodeAdded(nodeId);
188                 }
189                 return;
190             }
191         }
192
193         @Override
194         public void removeConnectedNode(OvsdbNodeId nodeId) {
195             ovsdbClients.remove(nodeId);
196             log.debug("Node connection is removed");
197             for (OvsdbNodeListener l : ovsdbNodeListener) {
198                 l.nodeRemoved(nodeId);
199             }
200         }
201     }
202
203     /**
204      * Processes table updates.
205      *
206      * @param clientService OvsdbClientService instance
207      * @param updates TableUpdates instance
208      * @param dbName ovsdb database name
209      */
210     private void processTableUpdates(OvsdbClientService clientService,
211                                      TableUpdates updates, String dbName)
212             throws InterruptedException {
213         checkNotNull(clientService, "OvsdbClientService is not null");
214
215         DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
216
217         for (String tableName : updates.result().keySet()) {
218             TableUpdate update = updates.result().get(tableName);
219             for (UUID uuid : (Set<UUID>) update.rows().keySet()) {
220                 log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
221                           uuid.value(), dbName, tableName);
222
223                 Row newRow = update.getNew(uuid);
224                 if (newRow != null) {
225                     clientService.updateOvsdbStore(dbName, tableName,
226                                                    uuid.value(), newRow);
227
228                     if (OvsdbConstant.INTERFACE.equals(tableName)) {
229                         dispatchInterfaceEvent(clientService,
230                                                newRow,
231                                                OvsdbEvent.Type.PORT_ADDED,
232                                                dbSchema);
233                     }
234                 } else if (update.getOld(uuid) != null) {
235                     if (OvsdbConstant.INTERFACE.equals(tableName)) {
236                         Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value());
237                         dispatchInterfaceEvent(clientService,
238                                                row,
239                                           OvsdbEvent.Type.PORT_REMOVED,
240                                           dbSchema);
241                     }
242                     clientService.removeRow(dbName, tableName, uuid.value());
243                 }
244             }
245         }
246     }
247
248     /**
249      * Dispatches event to the north.
250      *
251      * @param clientService OvsdbClientService instance
252      * @param newRow a new row
253      * @param oldRow an old row
254      * @param eventType type of event
255      * @param dbSchema ovsdb database schema
256      */
257     private void dispatchInterfaceEvent(OvsdbClientService clientService,
258                                         Row row,
259                                         Type eventType,
260                                         DatabaseSchema dbSchema) {
261
262         long dpid = getDataPathid(clientService, dbSchema);
263         Interface intf = (Interface) TableGenerator
264                 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
265         if (intf == null) {
266             return;
267         }
268
269         String portType = (String) intf.getTypeColumn().data();
270         long localPort = getOfPort(intf);
271         if (localPort < 0) {
272             return;
273         }
274         String[] macAndIfaceId = getMacAndIfaceid(intf);
275         if (macAndIfaceId == null) {
276             return;
277         }
278
279         EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
280                                                                                macAndIfaceId[0]),
281                                                             new HashSet<IpAddress>(),
282                                                             new OvsdbPortName(intf
283                                                                     .getName()),
284                                                             new OvsdbPortNumber(localPort),
285                                                             new OvsdbDatapathId(Long
286                                                                     .toString(dpid)),
287                                                             new OvsdbPortType(portType),
288                                                             new OvsdbIfaceId(macAndIfaceId[1]));
289         for (OvsdbEventListener listener : ovsdbEventListener) {
290             listener.handle(new OvsdbEvent<EventSubject>(eventType,
291                                                          eventSubject));
292         }
293     }
294
295     /**
296      * Gets mac and iface from the table Interface.
297      *
298      * @param intf Interface instance
299      * @return attachedMac, ifaceid
300      */
301     private String[] getMacAndIfaceid(Interface intf) {
302         OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
303         @SuppressWarnings("unchecked")
304         Map<String, String> externalIds = ovsdbMap.map();
305         if (externalIds == null) {
306             log.warn("The external_ids is null");
307             return null;
308         }
309
310         String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
311         if (attachedMac == null) {
312             log.warn("The attachedMac is null");
313             return null;
314         }
315         String ifaceid = externalIds
316                 .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
317         if (ifaceid == null) {
318             log.warn("The ifaceid is null");
319             return null;
320         }
321         return new String[] {attachedMac, ifaceid};
322     }
323
324     /**
325      * Gets ofPorts number from table Interface.
326      *
327      * @param intf Interface instance
328      * @return ofport the ofport number
329      */
330     private long getOfPort(Interface intf) {
331         OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
332         @SuppressWarnings("unchecked")
333         Set<Integer> ofPorts = ofPortSet.set();
334         while (ofPorts == null || ofPorts.size() <= 0) {
335             log.debug("The ofport is null in {}", intf.getName());
336             return -1;
337         }
338         Iterator<Integer> it = ofPorts.iterator();
339         return Long.parseLong(it.next().toString());
340     }
341
342     /**
343      * Gets datapathid from table bridge.
344      *
345      * @param clientService OvsdbClientService instance
346      * @param dbSchema ovsdb database schema
347      * @return datapathid the bridge datapathid
348      */
349     private long getDataPathid(OvsdbClientService clientService,
350                                DatabaseSchema dbSchema) {
351         String bridgeUuid = clientService
352                 .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
353         if (bridgeUuid == null) {
354             log.debug("Unable to spot bridge uuid for {} in {}",
355                       OvsdbConstant.INTEGRATION_BRIDGE, clientService);
356             return 0;
357         }
358
359         Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
360                                              "Bridge", bridgeUuid);
361         Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
362                                                          OvsdbTable.BRIDGE);
363         OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
364         @SuppressWarnings("unchecked")
365         Set<String> dpids = dpidSet.set();
366         if (dpids == null || dpids.size() == 0) {
367             return 0;
368         }
369         return stringToLong((String) dpids.toArray()[0]);
370     }
371
372     private long stringToLong(String values) {
373         long value = (new BigInteger(values.replaceAll(":", ""), 16))
374                 .longValue();
375         return value;
376     }
377
378     /**
379      * Implementation of an Callback which is responsible for receiving request
380      * infomation from ovsdb.
381      */
382     private class InternalMonitorCallBack implements Callback {
383         @Override
384         public void update(UpdateNotification updateNotification) {
385             Object key = updateNotification.jsonValue();
386             OvsdbClientService ovsdbClient = requestNotification.get(key);
387
388             String dbName = requestDbName.get(key);
389             JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
390             DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
391             TableUpdates updates = FromJsonUtil
392                     .jsonNodeToTableUpdates(updatesJson, dbSchema);
393             try {
394                 processTableUpdates(ovsdbClient, updates, dbName);
395             } catch (InterruptedException e) {
396                 log.warn("Interrupted while processing table updates");
397                 Thread.currentThread().interrupt();
398             }
399         }
400
401         @Override
402         public void locked(List<String> ids) {
403             // TODO Auto-generated method stub
404         }
405
406         @Override
407         public void stolen(List<String> ids) {
408             // TODO Auto-generated method stub
409         }
410
411     }
412
413 }