01079fec6154c37118938b930a56c85779b67368
[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.bgp.rib.impl.spi.BGPSessionProposalChecker;
20 import org.opendaylight.protocol.concepts.ListenerRegistration;
21 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
22 import org.opendaylight.protocol.framework.ReconnectStrategy;
23
24 import com.google.common.base.Preconditions;
25
26 /**
27  * Implementation of {@link BGP}.
28  */
29 public class BGPImpl implements BGP, Closeable {
30         /**
31          * Wrapper class to give listener a close method.
32          */
33         public class BGPListenerRegistration implements ListenerRegistration<BGPSessionListener> {
34
35                 private final BGPSessionListener listener;
36
37                 private final BGPSession session;
38
39                 public BGPListenerRegistration(final BGPSessionListener l, final BGPSession session) {
40                         this.listener = l;
41                         this.session = session;
42                 }
43
44                 @Override
45                 public void close() {
46                         this.session.close();
47                 }
48
49                 @Override
50                 public BGPSessionListener getListener() {
51                         return this.listener;
52                 }
53         }
54
55         private final BGPDispatcher dispatcher;
56
57         private final ProtocolMessageFactory parser;
58
59         private final InetSocketAddress address;
60
61         private final BGPSessionProposal proposal;
62
63         private final BGPSessionProposalChecker checker;
64
65         public BGPImpl(final BGPDispatcher dispatcher, final ProtocolMessageFactory parser, final InetSocketAddress address,
66                         final BGPSessionProposal proposal, final BGPSessionProposalChecker checker) throws IOException {
67                 this.dispatcher = Preconditions.checkNotNull(dispatcher);
68                 this.parser = Preconditions.checkNotNull(parser);
69                 this.address = Preconditions.checkNotNull(address);
70                 this.proposal = Preconditions.checkNotNull(proposal);
71                 this.checker = checker;
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public BGPListenerRegistration registerUpdateListener(final BGPSessionListener listener, final ReconnectStrategy strategy) throws IOException {
79                 final BGPSession session;
80                 try {
81                         session = this.dispatcher.createClient(
82                                         new BGPConnectionImpl(this.address, listener, this.proposal.getProposal(), this.checker), this.parser, strategy).get();
83                 } catch (InterruptedException | ExecutionException e) {
84                         throw new IOException("Failed to connect to peer", e);
85                 }
86                 return new BGPListenerRegistration(listener, session);
87         }
88
89         @Override
90         public void close() {
91
92         }
93 }