Initial framework migration to netty.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / ServerSessionMock.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.net.InetAddress;
11 import java.net.InetSocketAddress;
12 import java.net.UnknownHostException;
13 import java.util.Timer;
14
15 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
16 import org.opendaylight.protocol.pcep.PCEPConnection;
17 import org.opendaylight.protocol.pcep.PCEPMessage;
18 import org.opendaylight.protocol.pcep.PCEPSessionListener;
19 import org.opendaylight.protocol.pcep.PCEPSessionPreferences;
20 import org.opendaylight.protocol.pcep.PCEPSessionProposalChecker;
21 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
22 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
23
24 public class ServerSessionMock extends PCEPSessionImpl {
25
26         private final MockPCE client;
27
28         public ServerSessionMock(final PCEPSessionListener listener, final PCEPSessionListener client) {
29                 super(new MockDispatcher(), new Timer(), new PCEPConnection() {
30                         @Override
31                         public InetSocketAddress getPeerAddress() {
32                                 try {
33                                         return new InetSocketAddress(InetAddress.getByName("localhost"), 4189);
34                                 } catch (final UnknownHostException e) {
35                                         e.printStackTrace();
36                                 }
37                                 return null;
38                         }
39
40                         @Override
41                         public PCEPSessionListener getListener() {
42                                 return listener;
43                         }
44
45                         @Override
46                         public PCEPSessionPreferences getProposal() {
47                                 return new PCEPSessionPreferences(new PCEPOpenObject(4, 9, 2));
48                         }
49
50                         @Override
51                         public PCEPSessionProposalChecker getProposalChecker() {
52                                 return new SimpleSessionProposalChecker();
53                         }
54                 }, new PCEPMessageFactory(), 5, 30, null);
55                 this.client = (MockPCE) client;
56         }
57
58         @Override
59         public void sendMessage(final PCEPMessage msg) {
60                 this.lastMessageSentAt = System.nanoTime();
61                 this.client.onMessage(this, msg);
62         }
63
64         @Override
65         public void close() {
66                 this.client.onSessionTerminated(this, new PCEPCloseTermination(Reason.UNKNOWN));
67         }
68 }