8befe95711f7b697a810dc37ed341cceac0d444a
[bgpcep.git] / pcep / testtool / src / main / java / org / opendaylight / protocol / pcep / testtool / TestingSessionListener.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.testtool;
9
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.protocol.concepts.IPv4Address;
15 import org.opendaylight.protocol.concepts.IPv4Prefix;
16 import org.opendaylight.protocol.concepts.Prefix;
17 import org.opendaylight.protocol.framework.TerminationReason;
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.PCEPXRAddTunnelMessage;
22 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
23 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
24 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
25 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
26 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
27 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class TestingSessionListener extends PCEPSessionListener {
32
33         public List<PCEPMessage> messages = new ArrayList<PCEPMessage>();
34
35         private static final Logger logger = LoggerFactory.getLogger(TestingSessionListener.class);
36
37         public TestingSessionListener() {
38         }
39
40         @Override
41         public void onMessage(final PCEPSession session, final PCEPMessage message) {
42                 logger.debug("Received message: {}", message);
43                 this.messages.add(message);
44         }
45
46         @Override
47         public void onSessionUp(final PCEPSession session, final PCEPOpenObject local, final PCEPOpenObject remote) {
48                 logger.debug("Session up.");
49                 final List<ExplicitRouteSubobject> subs = new ArrayList<ExplicitRouteSubobject>();
50                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 10, 1, 1, 2 }), 32), false));
51                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 2, 2, 2, 2 }), 32), false));
52                 session.sendMessage(new PCEPXRAddTunnelMessage(new PCEPLspObject(23, false, false, false, false), new PCEPEndPointsObject<IPv4Address>(new IPv4Address(new byte[] {
53                                 1, 1, 1, 1 }), new IPv4Address(new byte[] { 2, 2, 2, 2 })), new PCEPExplicitRouteObject(subs, false)));
54         }
55
56         @Override
57         public void onSessionDown(final PCEPSession session, final TerminationReason cause, final Exception e) {
58                 logger.debug("Session down with cause : {} or exception: {}", cause, e);
59                 try {
60                         session.close();
61                 } catch (final IOException e1) {
62                         logger.debug("Could not close session, because {}", e1.getMessage(), e1);
63                 }
64         }
65
66         @Override
67         public void onSessionTerminated(final PCEPSession session, final TerminationReason cause) {
68                 logger.debug("Session terminated. Cause : {}", cause.toString());
69         }
70 }