BUG-2383 : wire AdjRibOutListener to BGPPeer
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ApplicationPeer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.net.InetAddresses;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, DOMDataTreeChangeListener, TransactionChainListener {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
39
40     private final byte[] rawIdentifier;
41     private final RIBImpl targetRib;
42     private final String name;
43     private final YangInstanceIdentifier adjRibsInId;
44     private final DOMTransactionChain chain;
45
46     private AdjRibInWriter writer;
47
48     public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIBImpl targetRib) {
49         this.name = applicationRibId.getValue().toString();
50         this.targetRib = Preconditions.checkNotNull(targetRib);
51         this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
52         final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
53         this.adjRibsInId = this.targetRib.getYangRibId().node(Peer.QNAME).node(peerId).node(AdjRibIn.QNAME).node(Tables.QNAME);
54         this.chain = this.targetRib.createPeerChain(this);
55         this.writer = AdjRibInWriter.create(this.targetRib.getYangRibId(), PeerRole.Ibgp, this.targetRib.createPeerChain(this));
56         // FIXME: set to true, once it's fixed how to skip advertising routes back to AppPeer
57         this.writer = this.writer.transform(RouterIds.createPeerId(ipAddress), this.targetRib.getRibSupportContext(), this.targetRib.getLocalTablesKeys(), false);
58     }
59
60     @Override
61     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
62         final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
63         LOG.debug("Received data change to ApplicationRib {}", changes);
64         for (final DataTreeCandidate tc : changes) {
65             final YangInstanceIdentifier path = tc.getRootPath();
66             final PathArgument lastArg = path.getLastPathArgument();
67             Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
68             final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
69             for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
70                 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(child.getIdentifier());
71                 if (child.getDataAfter().isPresent()) {
72                     LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
73                     LOG.trace("App peer -> AdjRibsIn data : {}", child.getDataAfter().get());
74                     tx.put(LogicalDatastoreType.OPERATIONAL, tableId, child.getDataAfter().get());
75                 }
76             }
77         }
78         tx.submit();
79     }
80
81     @Override
82     public String getName() {
83         return this.name;
84     }
85
86     @Override
87     public void close() {
88         this.writer.cleanTables(this.targetRib.getLocalTablesKeys());
89         this.chain.close();
90     }
91
92     @Override
93     public byte[] getRawIdentifier() {
94         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
95     }
96
97     @Override
98     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
99         final Throwable cause) {
100         LOG.error("Transaction chain failed.", cause);
101     }
102
103     @Override
104     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
105         LOG.debug("Transaction chain {} successfull.", chain);
106     }
107 }