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;
46 private AdjRibInWriter writer;
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 this.writer = this.writer.transform(RouterIds.createPeerId(ipAddress), this.targetRib.getRibSupportContext(), this.targetRib.getLocalTablesKeys(), true);
60 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
61 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
62 LOG.debug("Received data change to ApplicationRib {}", changes);
63 for (final DataTreeCandidate tc : changes) {
64 final YangInstanceIdentifier path = tc.getRootPath();
65 final PathArgument lastArg = path.getLastPathArgument();
66 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
67 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
68 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
69 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(child.getIdentifier());
70 if (child.getDataAfter().isPresent()) {
71 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
72 LOG.trace("App peer -> AdjRibsIn data : {}", child.getDataAfter().get());
73 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, child.getDataAfter().get());
81 public String getName() {
87 this.writer.cleanTables(this.targetRib.getLocalTablesKeys());
92 public byte[] getRawIdentifier() {
93 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
97 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
98 final Throwable cause) {
99 LOG.error("Transaction chain failed.", cause);
103 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
104 LOG.debug("Transaction chain {} successfull.", chain);