Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / app / BmpRouterImpl.java
index 0594bef2c5fdcbbaded1ab472989ca2bf9f2fb34..fe95c515ded5d85856b5bfdffe1d74830f247b73 100644 (file)
@@ -8,13 +8,15 @@
 
 package org.opendaylight.protocol.bmp.impl.app;
 
-import com.google.common.base.Optional;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.Preconditions;
 import com.google.common.net.InetAddresses;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -24,6 +26,7 @@ import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFaile
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
 import org.opendaylight.protocol.bmp.api.BmpSession;
 import org.opendaylight.protocol.bmp.impl.spi.BmpRouter;
@@ -39,7 +42,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.mess
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.RouterId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.peers.Peer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.routers.Router;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTree;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -75,7 +77,7 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
     private YangInstanceIdentifier peersYangIId;
 
     public BmpRouterImpl(final RouterSessionManager sessionManager) {
-        this.sessionManager = Preconditions.checkNotNull(sessionManager);
+        this.sessionManager = requireNonNull(sessionManager);
         this.domDataBroker = sessionManager.getDomDataBroker();
         this.domTxChain = this.domDataBroker.createTransactionChain(this);
         this.extensions = sessionManager.getExtensions();
@@ -89,12 +91,13 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
         this.routerId = new RouterId(Ipv4Util.getIpAddress(this.session.getRemoteAddress()));
         // check if this session is redundant
         if (!this.sessionManager.addSessionListener(this)) {
-            LOG.warn("Redundant BMP session with remote router {} ({}) detected. This BMP session will be abandoned.", this.routerIp, this.session);
+            LOG.warn("Redundant BMP session with remote router {} ({}) detected. This BMP session will be abandoned.",
+                this.routerIp, this.session);
             this.close();
         } else {
-            this.routerYangIId = YangInstanceIdentifier.builder(this.sessionManager.getRoutersYangIId()).nodeWithKey(Router.QNAME,
-                ROUTER_ID_QNAME, this.routerIp).build();
-            this.peersYangIId = YangInstanceIdentifier.builder(routerYangIId).node(Peer.QNAME).build();
+            this.routerYangIId = YangInstanceIdentifier.builder(this.sessionManager.getRoutersYangIId())
+                .nodeWithKey(Router.QNAME, ROUTER_ID_QNAME, this.routerIp).build();
+            this.peersYangIId = YangInstanceIdentifier.builder(this.routerYangIId).node(Peer.QNAME).build();
             createRouterEntry();
             LOG.info("BMP session with remote router {} ({}) is up now.", this.routerIp, this.session);
         }
@@ -120,7 +123,7 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
 
     @Override
     public RouterId getRouterId() {
-        return routerId;
+        return this.routerId;
     }
 
     @Override
@@ -128,7 +131,7 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
         if (this.session != null) {
             try {
                 this.session.close();
-            } catch (Exception e) {
+            } catch (final Exception e) {
                 LOG.error("Fail to close session.", e);
             }
         }
@@ -136,7 +139,8 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
 
     @GuardedBy("this")
     private synchronized void tearDown() {
-        if (this.session == null) {   // the session has been teared down before
+        // the session has been teared down before
+        if (this.session == null) {
             return;
         }
         // we want to display remote router's IP here, as sometimes this.session.close() is already
@@ -172,7 +176,8 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
     }
 
     @Override
-    public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
+    public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
+        final Throwable cause) {
         LOG.error("Transaction chain failed.", cause);
     }
 
@@ -185,19 +190,19 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
         return (this.routerYangIId != null);
     }
 
-    private void createRouterEntry() {
+    private synchronized void createRouterEntry() {
         Preconditions.checkState(isDatastoreWritable());
         final DOMDataWriteTransaction wTx = this.domTxChain.newWriteOnlyTransaction();
         wTx.put(LogicalDatastoreType.OPERATIONAL, this.routerYangIId,
                 Builders.mapEntryBuilder()
                 .withNodeIdentifier(new NodeIdentifierWithPredicates(Router.QNAME, ROUTER_ID_QNAME, this.routerIp))
-                .withChild(ImmutableNodes.leafNode(ROUTER_ID_QNAME, routerIp))
+                .withChild(ImmutableNodes.leafNode(ROUTER_ID_QNAME, this.routerIp))
                 .withChild(ImmutableNodes.leafNode(ROUTER_STATUS_QNAME, DOWN))
                 .withChild(ImmutableNodes.mapNodeBuilder(Peer.QNAME).build()).build());
         wTx.submit();
     }
 
-    private void onInitiate(final InitiationMessage initiation) {
+    private synchronized void onInitiate(final InitiationMessage initiation) {
         Preconditions.checkState(isDatastoreWritable());
         final DOMDataWriteTransaction wTx = this.domTxChain.newWriteOnlyTransaction();
         wTx.merge(LogicalDatastoreType.OPERATIONAL, this.routerYangIId,
@@ -213,7 +218,8 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
     private void onPeerUp(final PeerUpNotification peerUp) {
         final PeerId peerId = getPeerIdFromOpen(peerUp.getReceivedOpen());
         if (!getPeer(peerId).isPresent()) {
-            final BmpRouterPeer peer = BmpRouterPeerImpl.createRouterPeer(this.domTxChain, this.peersYangIId, peerUp, this.extensions, this.tree, peerId);
+            final BmpRouterPeer peer = BmpRouterPeerImpl.createRouterPeer(this.domTxChain, this.peersYangIId, peerUp,
+                this.extensions, this.tree, peerId);
             this.peers.put(peerId, peer);
             LOG.debug("Router {}: Peer {} goes up.", this.routerIp, peerId.getValue());
         } else {
@@ -236,7 +242,7 @@ public class BmpRouterImpl implements BmpRouter, TransactionChainListener {
     }
 
     private Optional<BmpRouterPeer> getPeer(final PeerId peerId) {
-        return Optional.fromNullable(this.peers.get(peerId));
+        return Optional.ofNullable(this.peers.get(peerId));
     }
 
     private static PeerId getPeerId(final PeerHeader peerHeader) {