MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AbstractPeer.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Arrays;
16 import java.util.Set;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
26 import org.opendaylight.protocol.bgp.rib.impl.state.BGPPeerStateImpl;
27 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
28 import org.opendaylight.protocol.bgp.rib.spi.Peer;
29 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
30 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
31 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 abstract class AbstractPeer extends BGPPeerStateImpl implements BGPRouteEntryImportParameters, TransactionChainListener,
43         Peer {
44     private static final Logger LOG = LoggerFactory.getLogger(AbstractPeer.class);
45     protected final RIB rib;
46     final String name;
47     final PeerRole peerRole;
48     private final ClusterIdentifier clusterId;
49     private final AsNumber localAs;
50     byte[] rawIdentifier;
51     @GuardedBy("this")
52     PeerId peerId;
53
54     AbstractPeer(
55             final RIB rib,
56             final String peerName,
57             final String groupId,
58             final PeerRole role,
59             @Nullable final ClusterIdentifier clusterId,
60             @Nullable final AsNumber localAs,
61             final IpAddress neighborAddress,
62             final Set<TablesKey> afiSafisAdvertized,
63             final Set<TablesKey> afiSafisGracefulAdvertized) {
64         super(rib.getInstanceIdentifier(), groupId, neighborAddress, afiSafisAdvertized, afiSafisGracefulAdvertized);
65         this.name = peerName;
66         this.peerRole = role;
67         this.clusterId = clusterId;
68         this.localAs = localAs;
69         this.rib = rib;
70     }
71
72     AbstractPeer(
73             final RIB rib,
74             final String peerName,
75             final String groupId,
76             final PeerRole role,
77             final IpAddress neighborAddress,
78             final Set<TablesKey> afiSafisGracefulAdvertized) {
79         this(rib, peerName, groupId, role, null, null, neighborAddress,
80                 rib.getLocalTablesKeys(), afiSafisGracefulAdvertized);
81     }
82
83     @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Unrecognised NullableDecl")
84     final synchronized ListenableFuture<Void> removePeer(
85             @Nonnull final DOMTransactionChain chain,
86             @Nullable final YangInstanceIdentifier peerPath) {
87         if (peerPath != null) {
88             LOG.info("AdjRibInWriter closed per Peer {} removed", peerPath);
89             final DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
90             tx.delete(LogicalDatastoreType.OPERATIONAL, peerPath);
91             final ListenableFuture<Void> future = tx.submit();
92             Futures.addCallback(future, new FutureCallback<Void>() {
93                 @Override
94                 public void onSuccess(final Void result) {
95                     LOG.debug("Peer {} removed", peerPath);
96                 }
97
98                 @Override
99                 public void onFailure(final Throwable t) {
100                     LOG.error("Failed to remove Peer {}", peerPath, t);
101                 }
102             }, MoreExecutors.directExecutor());
103             return future;
104         }
105         return Futures.immediateFuture(null);
106     }
107
108     synchronized YangInstanceIdentifier createPeerPath() {
109         return this.rib.getYangRibId().node(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib
110                 .rev180329.bgp.rib.rib.Peer.QNAME).node(IdentifierUtils.domPeerId(this.peerId));
111     }
112
113     @Override
114     public synchronized final PeerId getPeerId() {
115         return this.peerId;
116     }
117
118     @Override
119     public final PeerRole getRole() {
120         return this.peerRole;
121     }
122
123     @Override
124     public final synchronized byte[] getRawIdentifier() {
125         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
126     }
127
128     @Override
129     public final PeerRole getFromPeerRole() {
130         return getRole();
131     }
132
133     @Override
134     public final PeerId getFromPeerId() {
135         return getPeerId();
136     }
137
138     @Override
139     public final ClusterIdentifier getFromClusterId() {
140         return getClusterId();
141     }
142
143     @Override
144     public final void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
145         LOG.debug("Transaction chain {} successful.", chain);
146     }
147
148     @Override
149     public final BGPErrorHandlingState getBGPErrorHandlingState() {
150         return this;
151     }
152
153     @Override
154     public final BGPAfiSafiState getBGPAfiSafiState() {
155         return this;
156     }
157
158     @Override
159     public final AsNumber getFromPeerLocalAs() {
160         return getLocalAs();
161     }
162
163     @Override
164     public final String getName() {
165         return this.name;
166     }
167
168     @Override
169     public final ClusterIdentifier getClusterId() {
170         return this.clusterId;
171     }
172
173     @Override
174     public final AsNumber getLocalAs() {
175         return this.localAs;
176     }
177 }