BUG-58: refactor to take advantage of netty
[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.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.protocol.concepts.IPv4Address;
14 import org.opendaylight.protocol.concepts.IPv4Prefix;
15 import org.opendaylight.protocol.concepts.Prefix;
16 import org.opendaylight.protocol.pcep.PCEPMessage;
17 import org.opendaylight.protocol.pcep.PCEPSession;
18 import org.opendaylight.protocol.pcep.PCEPSessionListener;
19 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
20 import org.opendaylight.protocol.pcep.message.PCEPXRAddTunnelMessage;
21 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
22 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
23 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
24 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
25 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class TestingSessionListener implements PCEPSessionListener {
30
31         public List<PCEPMessage> messages = new ArrayList<PCEPMessage>();
32
33         private static final Logger logger = LoggerFactory.getLogger(TestingSessionListener.class);
34
35         public TestingSessionListener() {
36         }
37
38         @Override
39         public void onMessage(final PCEPSession session, final PCEPMessage message) {
40                 logger.debug("Received message: {}", message);
41                 this.messages.add(message);
42         }
43
44         @Override
45         public void onSessionUp(final PCEPSession session) {
46                 logger.debug("Session up.");
47                 final List<ExplicitRouteSubobject> subs = new ArrayList<ExplicitRouteSubobject>();
48                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 10, 1, 1, 2 }), 32), false));
49                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 2, 2, 2, 2 }), 32), false));
50                 session.sendMessage(new PCEPXRAddTunnelMessage(new PCEPLspObject(23, false, false, false, false), new PCEPEndPointsObject<IPv4Address>(new IPv4Address(new byte[] {
51                                 1, 1, 1, 1 }), new IPv4Address(new byte[] { 2, 2, 2, 2 })), new PCEPExplicitRouteObject(subs, false)));
52         }
53
54         @Override
55         public void onSessionDown(final PCEPSession session, final Exception e) {
56                 logger.debug("Session down with cause : {} or exception: {}", e);
57                 session.close();
58         }
59
60         @Override
61         public void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason cause) {
62                 logger.debug("Session terminated. Cause : {}", cause.toString());
63         }
64 }