bb1c0ea072a7f9e0f0afe71acb1c0ba9f55d8428
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImpl.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.pcep.impl;
9
10 import io.netty.util.concurrent.Future;
11
12 import java.io.IOException;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.protocol.framework.Dispatcher;
17 import org.opendaylight.protocol.framework.ProtocolServer;
18 import org.opendaylight.protocol.framework.ReconnectStrategy;
19 import org.opendaylight.protocol.pcep.PCEPConnection;
20 import org.opendaylight.protocol.pcep.PCEPConnectionFactory;
21 import org.opendaylight.protocol.pcep.PCEPDispatcher;
22 import org.opendaylight.protocol.pcep.PCEPSession;
23 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
24
25 /**
26  * Implementation of PCEPDispatcher.
27  */
28 public class PCEPDispatcherImpl implements PCEPDispatcher {
29
30         public static final int DEFAULT_MAX_UNKNOWN_MSG = 5;
31
32         private int maxUnknownMessages = DEFAULT_MAX_UNKNOWN_MSG;
33
34         private final Dispatcher dispatcher;
35
36         private final PCEPSessionProposalFactory proposalFactory;
37
38         /**
39          * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
40          * 
41          * @throws IOException if some error occurred during opening the selector
42          */
43         public PCEPDispatcherImpl(final Dispatcher dispatcher, final PCEPSessionProposalFactory proposalFactory) {
44                 this.dispatcher = dispatcher;
45                 this.proposalFactory = proposalFactory;
46         }
47
48         @Override
49         public Future<ProtocolServer> createServer(final InetSocketAddress address, final PCEPConnectionFactory connectionFactory) throws IOException {
50                 connectionFactory.setProposal(this.proposalFactory, address, 0);
51                 return this.dispatcher.createServer(address, connectionFactory, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
52         }
53
54         /**
55          * Create client is used for mock purposes only.
56          * 
57          * @throws ExecutionException
58          * @throws InterruptedException
59          */
60         @Override
61         public Future<? extends PCEPSession> createClient(final PCEPConnection connection, final ReconnectStrategy strategy) throws IOException {
62                 return this.dispatcher.createClient(connection, new PCEPSessionFactoryImpl(this.maxUnknownMessages), strategy);
63         }
64
65         @Override
66         public void setMaxUnknownMessages(final int limit) {
67                 this.maxUnknownMessages = limit;
68         }
69
70         public int getMaxUnknownMessages() {
71                 return this.maxUnknownMessages;
72         }
73
74         public Dispatcher getDispatcher() {
75                 return this.dispatcher;
76         }
77 }