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