2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.elan.internal;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.netvirt.elan.utils.BridgeUtils;
15 import org.opendaylight.netvirt.elan.utils.ElanConstants;
16 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
17 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Listen for new OVSDB nodes and then make sure they have the necessary bridges configured
30 public class ElanOvsdbNodeListener extends AbstractDataChangeListener<Node> {
32 private static final Logger logger = LoggerFactory.getLogger(ElanOvsdbNodeListener.class);
34 private ListenerRegistration<DataChangeListener> listenerRegistration;
35 private BridgeUtils bridgeUtils;
36 private boolean generateIntBridgeMac;
40 * @param db the DataBroker
41 * @param generateIntBridgeMac true if the integration bridge should be given a fixed, random MAC address
43 public ElanOvsdbNodeListener(final DataBroker db, boolean generateIntBridgeMac) {
45 this.bridgeUtils = new BridgeUtils(db);
46 this.generateIntBridgeMac = generateIntBridgeMac;
50 private void registerListener(final DataBroker db) {
52 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
53 getWildCardPath(), ElanOvsdbNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
54 } catch (final Exception e) {
55 logger.error("ElanOvsdbNodeListener: DataChange listener registration fail!", e);
56 throw new IllegalStateException("ElanOvsdbNodeListener: registration Listener failed.", e);
60 private InstanceIdentifier<Node> getWildCardPath() {
61 return InstanceIdentifier
62 .create(NetworkTopology.class)
63 .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
68 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
72 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
76 protected void add(InstanceIdentifier<Node> identifier, Node add) {
77 logger.debug("ElanOvsdbNodeListener.add, new bridge detected{}", add);
78 bridgeUtils.prepareNode(add, generateIntBridgeMac);