BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / bgp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / app / BmpRouterPeerImpl.java
index 2a88aa0b678bcc2023488c5522be1f61871cd9fa..89f30d943b4314fc757847a524b4aab30af167d9 100644 (file)
@@ -16,10 +16,12 @@ import java.util.Set;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 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.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
 import org.opendaylight.protocol.bmp.impl.spi.BmpRouterPeer;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Timestamp;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timestamp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
@@ -52,8 +54,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.moni
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.peers.peer.PrePolicyRib;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev150512.peers.peer.Stats;
 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.binding.data.codec.api.BindingCodecTreeNode;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -104,20 +104,15 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
 
     private static final TablesKey DEFAULT_TABLE = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
 
-    private static final InstanceIdentifier<SentOpen> SENT_OPEN_IID = InstanceIdentifier.builder(BmpMonitor.class)
+    private static final InstanceIdentifier<PeerSession> PEER_SESSION_ID = InstanceIdentifier.builder(BmpMonitor.class)
             .child(Monitor.class)
             .child(Router.class)
             .child(Peer.class)
-            .child(PeerSession.class)
-            .child(SentOpen.class)
-            .build();
-    private static final InstanceIdentifier<ReceivedOpen> RECEIVED_OPEN_IID = InstanceIdentifier.builder(BmpMonitor.class)
-            .child(Monitor.class)
-            .child(Router.class)
-            .child(Peer.class)
-            .child(PeerSession.class)
-            .child(ReceivedOpen.class)
-            .build();
+            .child(PeerSession.class).build();
+
+    private static final InstanceIdentifier<SentOpen> SENT_OPEN_IID = PEER_SESSION_ID.child(SentOpen.class);
+
+    private static final InstanceIdentifier<ReceivedOpen> RECEIVED_OPEN_IID = PEER_SESSION_ID.child(ReceivedOpen.class);
 
     private final DOMTransactionChain domTxChain;
     private final PeerId peerId;
@@ -134,6 +129,8 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
         this.domTxChain = Preconditions.checkNotNull(domTxChain);
         this.peerId = peerId;
         this.peerYangIId = YangInstanceIdentifier.builder(peersYangIId).nodeWithKey(Peer.QNAME, PEER_ID_QNAME, this.peerId.getValue()).build();
+        this.sentOpenCodec = tree.getSubtreeCodec(SENT_OPEN_IID);
+        this.receivedOpenCodec = tree.getSubtreeCodec(RECEIVED_OPEN_IID);
 
         final Set<TablesKey> peerTables = setPeerTables(peerUp.getReceivedOpen());
         final DOMDataWriteTransaction wTx = this.domTxChain.newWriteOnlyTransaction();
@@ -143,8 +140,6 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
                 this.domTxChain, extensions, peerTables, tree);
         this.postPolicyWriter = BmpRibInWriter.create(this.peerYangIId.node(PostPolicyRib.QNAME).node(BMP_TABLES_QNAME),
                 this.domTxChain, extensions, peerTables, tree);
-        this.sentOpenCodec = tree.getSubtreeCodec(SENT_OPEN_IID);
-        this.receivedOpenCodec = tree.getSubtreeCodec(RECEIVED_OPEN_IID);
     }
 
     public static BmpRouterPeer createRouterPeer(final DOMTransactionChain domTxChain,
@@ -171,14 +166,14 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
         if (this.up) {
             final AdjRibInType ribType = routeMonitoring.getPeerHeader().getAdjRibInType();
             switch (ribType) {
-            case PrePolicy:
-                this.prePolicyWriter.onMessage(routeMonitoring.getUpdate());
-                break;
-            case PostPolicy:
-                this.postPolicyWriter.onMessage(routeMonitoring.getUpdate());
-                break;
-            default:
-                break;
+                case PrePolicy:
+                    this.prePolicyWriter.onMessage(routeMonitoring.getUpdate());
+                    break;
+                case PostPolicy:
+                    this.postPolicyWriter.onMessage(routeMonitoring.getUpdate());
+                    break;
+                default:
+                    break;
             }
         }
     }
@@ -207,11 +202,6 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
         close();
     }
 
-    @Override
-    public PeerId getPeerId() {
-        return this.peerId;
-    }
-
     @Override
     public void close() {
         Preconditions.checkState(this.up, "Already closed.");
@@ -243,25 +233,21 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
                 .withChild(
                         ImmutableNodes.leafNode(PEER_LOCAL_ADDRESS_QNAME,
                                 getStringIpAddress(peerUp.getLocalAddress())))
-                .withChild(
-                        ImmutableNodes.leafNode(PEER_LOCAL_PORT_QNAME, peerUp
-                                .getLocalPort().getValue()))
-                .withChild(
-                        ImmutableNodes.leafNode(PEER_REMOTE_PORT_QNAME, peerUp
-                                .getRemotePort().getValue()))
-                .withChild(ImmutableNodes.leafNode(PEER_STATUS_QNAME, "up"))
-                .withChild(
-                        ImmutableNodes.leafNode(PEER_UP_TIMESTAMP_QNAME,
-                                timestamp.getValue()));
+                                .withChild(
+                                        ImmutableNodes.leafNode(PEER_LOCAL_PORT_QNAME, peerUp
+                                                .getLocalPort().getValue()))
+                                                .withChild(
+                                                        ImmutableNodes.leafNode(PEER_REMOTE_PORT_QNAME, peerUp
+                                                                .getRemotePort().getValue()))
+                                                                .withChild(ImmutableNodes.leafNode(PEER_STATUS_QNAME, "up"))
+                                                                .withChild(
+                                                                        ImmutableNodes.leafNode(PEER_UP_TIMESTAMP_QNAME,
+                                                                                timestamp.getValue()));
         if (this.receivedOpenCodec != null) {
-            builder.withChild(Builders.containerBuilder(
-                    (ContainerNode) this.receivedOpenCodec.serialize(peerUp
-                            .getReceivedOpen())).build());
+            builder.withChild((ContainerNode) this.receivedOpenCodec.serialize(peerUp.getReceivedOpen()));
         }
         if (this.sentOpenCodec != null) {
-            builder.withChild(Builders.containerBuilder(
-                    (ContainerNode) this.sentOpenCodec.serialize(peerUp
-                            .getSentOpen())).build());
+            builder.withChild((ContainerNode) this.sentOpenCodec.serialize(peerUp.getSentOpen()));
         }
         return builder.build();
     }
@@ -279,9 +265,9 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
                 .withChild(
                         Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(PrePolicyRib.QNAME))
                         .withChild(ImmutableNodes.mapNodeBuilder(BMP_TABLES_QNAME).build()).build())
-                .withChild(
-                        Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(PostPolicyRib.QNAME))
-                        .withChild(ImmutableNodes.mapNodeBuilder(BMP_TABLES_QNAME).build()).build());
+                        .withChild(
+                                Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(PostPolicyRib.QNAME))
+                                .withChild(ImmutableNodes.mapNodeBuilder(BMP_TABLES_QNAME).build()).build());
         if (peerHeader.getDistinguisher() != null) {
             mapEntryBuilder.withChild(ImmutableNodes.leafNode(PEER_DISTINGUISHER_QNAME, peerHeader.getDistinguisher().getDistinguisher()));
         }
@@ -329,19 +315,19 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
         }
         if (tlvs.getPerAfiSafiAdjRibInTlv() != null) {
             builder.withChild(Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(STAT9_QNAME))
-                .withChild(ImmutableNodes.mapNodeBuilder(AF_QNAME)
-                    .withChild(Builders.mapEntryBuilder()
-                        .withChild(ImmutableNodes.leafNode(COUNT_QNAME, tlvs.getPerAfiSafiAdjRibInTlv().getCount().getValue()))
-                        .withNodeIdentifier(TablesUtil.toYangTablesKey(AF_QNAME, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))
-                        .build()).build()).build());
+                    .withChild(ImmutableNodes.mapNodeBuilder(AF_QNAME)
+                            .withChild(Builders.mapEntryBuilder()
+                                    .withChild(ImmutableNodes.leafNode(COUNT_QNAME, tlvs.getPerAfiSafiAdjRibInTlv().getCount().getValue()))
+                                    .withNodeIdentifier(TablesUtil.toYangTablesKey(AF_QNAME, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))
+                                    .build()).build()).build());
         }
         if (tlvs.getPerAfiSafiLocRibTlv() != null) {
             builder.withChild(Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(STAT10_QNAME))
-                .withChild(ImmutableNodes.mapNodeBuilder(AF_QNAME)
-                    .withChild(Builders.mapEntryBuilder()
-                        .withChild(ImmutableNodes.leafNode(COUNT_QNAME, tlvs.getPerAfiSafiLocRibTlv().getCount().getValue()))
-                        .withNodeIdentifier(TablesUtil.toYangTablesKey(AF_QNAME, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))
-                        .build()).build()).build());
+                    .withChild(ImmutableNodes.mapNodeBuilder(AF_QNAME)
+                            .withChild(Builders.mapEntryBuilder()
+                                    .withChild(ImmutableNodes.leafNode(COUNT_QNAME, tlvs.getPerAfiSafiLocRibTlv().getCount().getValue()))
+                                    .withNodeIdentifier(TablesUtil.toYangTablesKey(AF_QNAME, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))
+                                    .build()).build()).build());
         }
         if (tlvs.getUpdatesTreatedAsWithdrawTlv() != null) {
             builder.withChild(ImmutableNodes.leafNode(STAT11_QNAME, tlvs.getUpdatesTreatedAsWithdrawTlv().getCount().getValue()));
@@ -357,11 +343,23 @@ public final class BmpRouterPeerImpl implements BmpRouterPeer {
     private ContainerNode createMirrors(final Mirror mirror, final Timestamp timestamp) {
         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder =
                 Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(Mirrors.QNAME));
-        builder.withChild(ImmutableNodes.leafNode(PEER_MIRROR_INFORMATION_QNAME, MirrorInformationCode.forValue(mirror.getTlvs().getMirrorInformationTlv().getCode().getIntValue())));
+        builder.withChild(ImmutableNodes.leafNode(PEER_MIRROR_INFORMATION_QNAME, toDom(MirrorInformationCode.forValue(
+                mirror.getTlvs().getMirrorInformationTlv().getCode().getIntValue()))));
         builder.withChild(ImmutableNodes.leafNode(PEER_MIRROR_TIMESTAMP_QNAME, timestamp.getValue()));
         return builder.build();
     }
 
+    private static String toDom(final MirrorInformationCode informationCode) {
+        switch (informationCode) {
+            case ErroredPdu:
+                return "errored-pdu";
+            case MessageLost:
+                return "message-lost";
+            default:
+                return null;
+        }
+    }
+
 
     private static String getStringIpAddress(final IpAddress ipAddress) {
         if (ipAddress.getIpv4Address() != null) {