2 * Copyright (c) 2015 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.Optional;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.BindingMapping;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Maintains the mapping of PeerId -> Role. Subclasses get notified of changes and can do their
34 abstract class AbstractPeerRoleTracker implements AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(AbstractPeerRoleTracker.class);
39 * We are subscribed to our target leaf, but that is a wildcard:
40 * /bgp-rib/rib/peer/peer-role
42 * MD-SAL assumption: we are getting one {@link DataTreeCandidate} for each expanded
43 * wildcard path, so are searching for a particular key.
45 private final class PeerRoleListener implements DOMDataTreeChangeListener {
48 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
49 LOG.debug("Data Changed for Peer role {}", changes);
50 for (final DataTreeCandidate tc : changes) {
51 // Obtain the peer's path
52 final YangInstanceIdentifier peerPath = IdentifierUtils.peerPath(tc.getRootPath());
55 final Optional<NormalizedNode<?, ?>> maybePeerRole = tc.getRootNode().getDataAfter();
57 if (maybePeerRole.isPresent()) {
58 final LeafNode<?> peerRoleLeaf = (LeafNode<?>) maybePeerRole.get();
59 // We could go for a coded, but his is simpler and faster
60 role = PeerRole.valueOf(BindingMapping.getClassName((String) peerRoleLeaf.getValue()));
65 peerRoleChanged(peerPath, role);
70 private static final QName PEER_ROLE = QName.cachedReference(QName.create(Peer.QNAME, "peer-role"));
71 private final ListenerRegistration<?> registration;
73 protected AbstractPeerRoleTracker(final @Nonnull DOMDataTreeChangeService service, @Nonnull final YangInstanceIdentifier ribId) {
74 // Slightly evil, but our users should be fine with this
75 final YangInstanceIdentifier roleId = ribId.node(Peer.QNAME).node(Peer.QNAME).node(PEER_ROLE);
76 this.registration = service.registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, roleId), new PeerRoleListener());
81 this.registration.close();
85 * Invoked whenever a peer role changes.
87 * @param peerPath Peer's path
88 * @param role Peer's new role, null indicates the peer has disappeared.
90 protected abstract void peerRoleChanged(@Nonnull YangInstanceIdentifier peerPath, @Nullable PeerRole role);