BUG-58: refactor to take advantage of netty
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPImpl.java
1 /*
2  * Copyright (c) 2013 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 java.io.Closeable;
11 import java.io.IOException;
12 import java.net.InetSocketAddress;
13 import java.util.concurrent.ExecutionException;
14
15 import org.opendaylight.protocol.bgp.parser.BGPSession;
16 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionProposal;
19 import org.opendaylight.protocol.concepts.ListenerRegistration;
20 import org.opendaylight.protocol.framework.ReconnectStrategy;
21
22 import com.google.common.base.Preconditions;
23
24 /**
25  * Implementation of {@link BGP}.
26  */
27 public class BGPImpl implements BGP, Closeable {
28         /**
29          * Wrapper class to give listener a close method.
30          */
31         public class BGPListenerRegistration implements ListenerRegistration<BGPSessionListener> {
32
33                 private final BGPSessionListener listener;
34
35                 private final BGPSession session;
36
37                 public BGPListenerRegistration(final BGPSessionListener l, final BGPSession session) {
38                         this.listener = l;
39                         this.session = session;
40                 }
41
42                 @Override
43                 public void close() {
44                         this.session.close();
45                 }
46
47                 @Override
48                 public BGPSessionListener getListener() {
49                         return this.listener;
50                 }
51         }
52
53         private final BGPDispatcher dispatcher;
54
55         private final InetSocketAddress address;
56
57         private final BGPSessionProposal proposal;
58
59         public BGPImpl(final BGPDispatcher dispatcher, final InetSocketAddress address, final BGPSessionProposal proposal) {
60                 this.dispatcher = Preconditions.checkNotNull(dispatcher);
61                 this.address = Preconditions.checkNotNull(address);
62                 this.proposal = Preconditions.checkNotNull(proposal);
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         public BGPListenerRegistration registerUpdateListener(final BGPSessionListener listener, final ReconnectStrategy strategy) throws IOException {
70                 final BGPSession session;
71                 try {
72                         session = this.dispatcher.createClient(this.address, this.proposal.getProposal(), listener, strategy).get();
73                 } catch (InterruptedException | ExecutionException e) {
74                         throw new IOException("Failed to connect to peer", e);
75                 }
76                 return new BGPListenerRegistration(listener, session);
77         }
78
79         @Override
80         public void close() {
81
82         }
83 }