BUG-3362 : improved tests for application peer
[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 /**
37  * Application Peer is a special case of BGP peer. It serves as an interface
38  * for user to advertise user routes to ODL and through ODL to other BGP peers.
39  *
40  * This peer has it's own RIB, where it stores all user routes. This RIB is
41  * located in configurational datastore. Routes are added through RESTCONF.
42  *
43  * They are then processed as routes from any other peer, through AdjRib,
44  * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
45  *
46  * For purposed of import policies such as Best Path Selection, application
47  * peer needs to have a BGP-ID that is configurable.
48  */
49 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, DOMDataTreeChangeListener, TransactionChainListener {
50
51     private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
52
53     private final byte[] rawIdentifier;
54     private final RIBImpl targetRib;
55     private final String name;
56     private final YangInstanceIdentifier adjRibsInId;
57     private final DOMTransactionChain chain;
58     private final DOMTransactionChain writerChain;
59
60     private AdjRibInWriter writer;
61
62     public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIBImpl targetRib) {
63         this.name = applicationRibId.getValue().toString();
64         this.targetRib = Preconditions.checkNotNull(targetRib);
65         this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
66         final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
67         this.adjRibsInId = this.targetRib.getYangRibId().node(Peer.QNAME).node(peerId).node(AdjRibIn.QNAME).node(Tables.QNAME);
68         this.chain = this.targetRib.createPeerChain(this);
69         this.writerChain = this.targetRib.createPeerChain(this);
70         this.writer = AdjRibInWriter.create(this.targetRib.getYangRibId(), PeerRole.Ibgp, this.writerChain);
71         // FIXME: set to true, once it's fixed how to skip advertising routes back to AppPeer
72         this.writer = this.writer.transform(RouterIds.createPeerId(ipAddress), this.targetRib.getRibSupportContext(), this.targetRib.getLocalTablesKeys(), false);
73     }
74
75     /**
76      * Routes come from application RIB that is identified by (configurable) name.
77      * Each route is pushed into AdjRibsInWriter with it's whole context. In this
78      * method, it doesn't matter if the routes are removed or added, this will
79      * be determined in LocRib.
80      */
81     @Override
82     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
83         final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
84         LOG.debug("Received data change to ApplicationRib {}", changes);
85         for (final DataTreeCandidate tc : changes) {
86             LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
87             final YangInstanceIdentifier path = tc.getRootPath();
88             final PathArgument lastArg = path.getLastPathArgument();
89             Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
90             final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
91             for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
92                 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(child.getIdentifier());
93                 if (child.getDataAfter().isPresent()) {
94                     LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
95                     LOG.trace("App peer -> AdjRibsIn data : {}", child.getDataAfter().get());
96                     tx.put(LogicalDatastoreType.OPERATIONAL, tableId, child.getDataAfter().get());
97                 }
98             }
99         }
100         tx.submit();
101     }
102
103     @Override
104     public String getName() {
105         return this.name;
106     }
107
108     @Override
109     public void close() {
110         this.writer.cleanTables(this.targetRib.getLocalTablesKeys());
111         this.chain.close();
112         this.writerChain.close();
113     }
114
115     @Override
116     public byte[] getRawIdentifier() {
117         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
118     }
119
120     @Override
121     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
122         final Throwable cause) {
123         LOG.error("Transaction chain failed.", cause);
124     }
125
126     @Override
127     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
128         LOG.debug("Transaction chain {} successfull.", chain);
129     }
130 }