Remove unused BGP OpenConfig provider implementation
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BgpPeerRpc.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import io.netty.channel.ChannelFuture;
15 import java.util.Set;
16 import java.util.concurrent.Future;
17 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefreshBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.BgpPeerRpcService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.RouteRefreshRequestInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class BgpPeerRpc implements BgpPeerRpcService {
30
31     private static final Logger LOG = LoggerFactory.getLogger(BgpPeerRpc.class);
32     private static final String FAILURE_MSG = "Failed to send Route Refresh message";
33
34     private final BGPSession session;
35     private final Set<TablesKey> supportedFamilies;
36
37     BgpPeerRpc(final BGPSession session, final Set<TablesKey> supportedFamilies) {
38         this.session = Preconditions.checkNotNull(session);
39         this.supportedFamilies = Preconditions.checkNotNull(supportedFamilies);
40     }
41
42     @Override
43     public Future<RpcResult<Void>> routeRefreshRequest(final RouteRefreshRequestInput input) {
44         final ChannelFuture f = sendRRMessage(input);
45         if (f != null) {
46             return Futures.transform(JdkFutureAdapters.listenInPoolThread(f), new Function<Void, RpcResult<Void>>() {
47                 @Override
48                 public RpcResult<Void> apply(final Void input) {
49                     if (f.isSuccess()) {
50                         return RpcResultBuilder.<Void>success().build();
51                     } else {
52                         return RpcResultBuilder.<Void>failed().withError(ErrorType.RPC, FAILURE_MSG).build();
53                     }
54                 }
55             });
56         }
57         return RpcResultBuilder.<Void>failed().withError(ErrorType.RPC, FAILURE_MSG + " due to unsupported address families.").buildFuture();
58     }
59
60     private ChannelFuture sendRRMessage(final RouteRefreshRequestInput input) {
61         if (!this.supportedFamilies.contains(new TablesKey(input.getAfi(), input.getSafi()))) {
62             LOG.info("Unsupported afi/safi: {}, {}.", input.getAfi(), input.getSafi());
63             return null;
64         }
65         final RouteRefresh msg = new RouteRefreshBuilder().setAfi(input.getAfi()).setSafi(input.getSafi()).build();
66         return ((BGPSessionImpl) this.session).getLimiter().writeAndFlush(msg);
67     }
68
69 }