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