2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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;
36 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, DOMDataTreeChangeListener, TransactionChainListener {
38 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
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 private final DOMTransactionChain writerChain;
47 private AdjRibInWriter writer;
49 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIBImpl targetRib) {
50 this.name = applicationRibId.getValue().toString();
51 this.targetRib = Preconditions.checkNotNull(targetRib);
52 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
53 final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
54 this.adjRibsInId = this.targetRib.getYangRibId().node(Peer.QNAME).node(peerId).node(AdjRibIn.QNAME).node(Tables.QNAME);
55 this.chain = this.targetRib.createPeerChain(this);
56 this.writerChain = this.targetRib.createPeerChain(this);
57 this.writer = AdjRibInWriter.create(this.targetRib.getYangRibId(), PeerRole.Ibgp, this.writerChain);
58 // FIXME: set to true, once it's fixed how to skip advertising routes back to AppPeer
59 this.writer = this.writer.transform(RouterIds.createPeerId(ipAddress), this.targetRib.getRibSupportContext(), this.targetRib.getLocalTablesKeys(), false);
63 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
64 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
65 LOG.debug("Received data change to ApplicationRib {}", changes);
66 for (final DataTreeCandidate tc : changes) {
67 final YangInstanceIdentifier path = tc.getRootPath();
68 final PathArgument lastArg = path.getLastPathArgument();
69 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
70 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
71 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
72 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(child.getIdentifier());
73 if (child.getDataAfter().isPresent()) {
74 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
75 LOG.trace("App peer -> AdjRibsIn data : {}", child.getDataAfter().get());
76 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, child.getDataAfter().get());
84 public String getName() {
90 this.writer.cleanTables(this.targetRib.getLocalTablesKeys());
92 this.writerChain.close();
96 public byte[] getRawIdentifier() {
97 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
101 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
102 final Throwable cause) {
103 LOG.error("Transaction chain failed.", cause);
107 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
108 LOG.debug("Transaction chain {} successfull.", chain);