Bug 5061: Introduce BGP Application Peer deployer 83/43983/1
authorMilos Fabian <milfabia@cisco.com>
Thu, 21 Jul 2016 10:37:33 +0000 (12:37 +0200)
committerClaudio D. Gasparini <cgaspari@cisco.com>
Mon, 15 Aug 2016 13:31:49 +0000 (13:31 +0000)
Enhance BGP deployer service to handle Application Peer
instance creation. Like BGP Peer, Application Peer
configuration is represented as OpenConfig's Neighbor,
however the Application peer is distinguished by specific
peer group.

Change-Id: I078f76a3815c4bd10171b9c97a545479bfd7eca0
Signed-off-by: Milos Fabian <milfabia@cisco.com>
(cherry picked from commit bfe85df5277c15670ef57694778d7f9245782820)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/AppPeer.java [new file with mode: 0644]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpDeployerImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/PeerBean.java [new file with mode: 0644]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/spi/InstanceType.java
bgp/rib-impl/src/main/resources/org/opendaylight/blueprint/bgp-rib.xml

diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/AppPeer.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/AppPeer.java
new file mode 100644 (file)
index 0000000..bd66144
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.rib.impl.config;
+
+import com.google.common.base.Strings;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
+import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
+import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRib;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+
+public class AppPeer implements PeerBean {
+
+    private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
+
+    private ApplicationPeer applicationPeer;
+    private ListenerRegistration<ApplicationPeer> registration;
+
+    @Override
+    public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService) {
+        final ApplicationRibId appRibId = createAppRibId(neighbor);
+        this.applicationPeer = new ApplicationPeer(appRibId, neighbor.getNeighborAddress().getIpv4Address(), rib);
+        final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
+                .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, appRibId.getValue()).node(Tables.QNAME).node(Tables.QNAME).build();
+        this.registration = rib.getService().registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId),
+                this.applicationPeer);
+    }
+
+    @Override
+    public void close() {
+        if (this.applicationPeer != null) {
+            this.registration.close();
+            this.applicationPeer.close();
+        }
+    }
+
+    private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
+        final Config config = neighbor.getConfig();
+        if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
+            return new ApplicationRibId(config.getDescription());
+        }
+        return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
+    }
+
+}
index 49e6bbea76474930f27bb871dfbaf9a5686c4c38..12741051b11451063919dc1ba39ec3441550e4cf 100644 (file)
@@ -66,7 +66,7 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     @GuardedBy("this")
     private final Map<InstanceIdentifier<Bgp>, RibImpl> ribs = new HashMap<>();
     @GuardedBy("this")
-    private final Map<InstanceIdentifier<Neighbor>, BgpPeer> peers = new HashMap<>();
+    private final Map<InstanceIdentifier<Neighbor>, PeerBean> peers = new HashMap<>();
     private final DataBroker dataBroker;
     @GuardedBy("this")
     private boolean closed;
@@ -127,7 +127,7 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     @Override
     public synchronized void close() throws Exception {
         this.registration.close();
-        this.peers.values().forEach(BgpPeer::close);
+        this.peers.values().forEach(PeerBean::close);
         this.peers.clear();
         this.ribs.values().forEach(RibImpl::close);
         this.ribs.clear();
@@ -225,7 +225,7 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     private void onNeighborModified(final InstanceIdentifier<Bgp> rootIdentifier, final Neighbor neighbor) {
         LOG.debug("Modifing Peer instance with configuration: {}", neighbor);
         //restart peer instance with a new configuration
-        final BgpPeer bgpPeer = this.peers.get(getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey()));
+        final PeerBean bgpPeer = this.peers.get(getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey()));
         if (bgpPeer != null) {
             bgpPeer.close();
             final InstanceIdentifier<Neighbor> neighborInstanceIdentifier = getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey());
@@ -240,7 +240,12 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     private void onNeighborCreated(final InstanceIdentifier<Bgp> rootIdentifier, final Neighbor neighbor) {
         //create, start and register peer instance
         LOG.debug("Creating Peer instance with configuration: {}", neighbor);
-        final BgpPeer bgpPeer = (BgpPeer) this.container.getComponentInstance(InstanceType.PEER.getBeanName());
+        final PeerBean bgpPeer;
+        if (this.mappingService.isApplicationPeer(neighbor)) {
+            bgpPeer = (PeerBean) this.container.getComponentInstance(InstanceType.APP_PEER.getBeanName());
+        } else {
+            bgpPeer = (PeerBean) this.container.getComponentInstance(InstanceType.PEER.getBeanName());
+        }
         final InstanceIdentifier<Neighbor> neighborInstanceIdentifier = getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey());
         initiatePeerInstance(rootIdentifier, neighborInstanceIdentifier, neighbor, bgpPeer);
         this.peers.put(neighborInstanceIdentifier, bgpPeer);
@@ -250,7 +255,7 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     private void onNeighborRemoved(final InstanceIdentifier<Bgp> rootIdentifier, final Neighbor neighbor) {
         //destroy peer instance
         LOG.debug("Removing Peer instance: {}", rootIdentifier);
-        final BgpPeer bgpPeer = this.peers.remove(getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey()));
+        final PeerBean bgpPeer = this.peers.remove(getNeighborInstanceIdentifier(rootIdentifier, neighbor.getKey()));
         if (bgpPeer != null) {
             bgpPeer.close();
             LOG.debug("Peer instance removed {}", bgpPeer);
@@ -265,12 +270,14 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
     }
 
     private void initiatePeerInstance(final InstanceIdentifier<Bgp> rootIdentifier, final InstanceIdentifier<Neighbor> neighborIdentifier, final Neighbor neighbor,
-        final BgpPeer bgpPeer) {
+            final PeerBean bgpPeer) {
         final String peerInstanceName = getNeighborInstanceName(neighborIdentifier);
         final RibImpl rib = this.ribs.get(rootIdentifier);
         if (rib != null) {
             bgpPeer.start(rib, neighbor, this.mappingService);
-            registerPeerInstance(bgpPeer, peerInstanceName);
+            if (bgpPeer instanceof BgpPeer) {
+                registerPeerInstance((BgpPeer) bgpPeer, peerInstanceName);
+            }
         }
     }
 
index 4e51e63a2e8c874cce320309518b4be6c18b5667..a80b5de4b944710459b2ab024bc1263e2d1a0bac 100644 (file)
@@ -50,7 +50,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 
-public class BgpPeer implements AutoCloseable, BGPPeerRuntimeMXBean {
+public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
 
     //FIXME make configurable
     private static final PortNumber PORT = new PortNumber(179);
@@ -72,6 +72,7 @@ public class BgpPeer implements AutoCloseable, BGPPeerRuntimeMXBean {
         this.peerRegistry = peerRegistry;
     }
 
+    @Override
     public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService) {
         Preconditions.checkState(this.bgpPeer == null, "Previous peer instance {} was not closed.");
         this.neighborAddress = neighbor.getNeighborAddress();
diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/PeerBean.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/PeerBean.java
new file mode 100644 (file)
index 0000000..64034e0
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.rib.impl.config;
+
+import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
+import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
+
+/**
+ * Common interface for BgpPeer and AppPeer beans
+ *
+ */
+public interface PeerBean extends AutoCloseable {
+
+    void start(RIB rib, Neighbor neighbor, BGPOpenConfigMappingService mappingService);
+
+    @Override
+    void close();
+
+}
index 21bc763c53098c3ba3e5d07b02969def41a37e8e..1b67d2afa35385c829153b80b51d4cd9496d498a 100644 (file)
@@ -19,7 +19,9 @@ public enum InstanceType {
 
     RIB("ribImpl", Lists.newArrayList(RIB.class, RibReference.class)),
 
-    PEER("bgpPeer", Collections.singletonList(BGPPeerRuntimeMXBean.class));
+    PEER("bgpPeer", Collections.singletonList(BGPPeerRuntimeMXBean.class)),
+
+    APP_PEER("appPeer", Collections.emptyList());
 
     private final String beanName;
     private final String[] services;
index d193a9819b0e17d428450dea5b7fd5ee9fdb16c6..383f57d4e707cfaf83980ca6fbd67906f331d131 100644 (file)
@@ -73,4 +73,6 @@
     <argument ref="BGPPeerRegistry"/>
   </bean>
 
+  <bean id="appPeer" class="org.opendaylight.protocol.bgp.rib.impl.config.AppPeer" scope="prototype"/>
+
 </blueprint>
\ No newline at end of file