7d9278c03f89ae82b327b39681bcae913a595d0f
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / MockPCE.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.util.ArrayList;
11 import java.util.List;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import org.opendaylight.protocol.framework.TerminationReason;
17 import org.opendaylight.protocol.pcep.PCEPErrors;
18 import org.opendaylight.protocol.pcep.PCEPMessage;
19 import org.opendaylight.protocol.pcep.PCEPSession;
20 import org.opendaylight.protocol.pcep.PCEPSessionListener;
21 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
22 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
23 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
24 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
25
26 /**
27  *
28  */
29 public class MockPCE extends PCEPSessionListener {
30
31         private final List<PCEPMessage> listMsg = new ArrayList<PCEPMessage>();
32
33         private PCEPSessionImpl session = null;
34
35         public boolean up = false;
36
37         private static final Logger logger = LoggerFactory.getLogger(MockPCE.class);
38
39         public boolean down = false;
40
41         public MockPCE() {
42         }
43
44         public void sendMessage(PCEPMessage msg) {
45                 this.session.handleMessage(msg);
46         }
47
48         public void sendErrorMessage(PCEPErrors value,
49                         PCEPOpenObject open) {
50                 final PCEPErrorObject error = new PCEPErrorObject(value);
51                 final List<PCEPErrorObject> errors = new ArrayList<PCEPErrorObject>();
52                 errors.add(error);
53                 this.sendMessage(new PCEPErrorMessage(open, errors, null));
54         }
55
56         public List<PCEPMessage> getListMsg() {
57                 return this.listMsg;
58         }
59
60         public void addSession(PCEPSessionImpl l) {
61                 this.session = l;
62         }
63
64         @Override
65         public void onMessage(PCEPSession session, PCEPMessage message) {
66                 this.listMsg.add(message);
67                 logger.debug("Message received:" + message);
68         }
69
70         @Override
71         public void onSessionUp(PCEPSession session, PCEPOpenObject local,
72                         PCEPOpenObject remote) {
73                 logger.debug("Session Up");
74                 this.up = true;
75                 this.notifyAll();
76         }
77
78         @Override
79         public void onSessionDown(PCEPSession session, PCEPCloseObject reason, Exception e) {
80                 logger.debug("Session Down");
81                 this.down = true;
82                 //this.notifyAll();
83         }
84
85         @Override
86         public void onSessionTerminated(PCEPSession session,
87                         TerminationReason cause) {
88                 logger.debug("Session terminated. Cause : " + cause.toString());
89         }
90 }