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