Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / testtool / src / main / java / org / opendaylight / protocol / pcep / testtool / SimpleSessionListener.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.IPv4;
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.pcep.PCEPSession;
18 import org.opendaylight.protocol.pcep.PCEPSessionListener;
19 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
20 import org.opendaylight.protocol.pcep.message.PCCreateMessage;
21 import org.opendaylight.protocol.pcep.object.CompositeInstantiationObject;
22 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
23 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
24 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
25 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
26 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.collect.Lists;
32
33 /**
34  * Simple Session Listener that is notified about messages and changes in the session.
35  */
36 public class SimpleSessionListener implements PCEPSessionListener {
37
38         public List<Message> messages = Lists.newArrayList();
39
40         private static final Logger logger = LoggerFactory.getLogger(SimpleSessionListener.class);
41
42         public SimpleSessionListener() {
43         }
44
45         @Override
46         public void onMessage(final PCEPSession session, final Message message) {
47                 logger.debug("Received message: {}", message);
48                 this.messages.add(message);
49         }
50
51         @Override
52         public void onSessionUp(final PCEPSession session) {
53                 logger.debug("Session up.");
54                 final List<ExplicitRouteSubobject> subs = new ArrayList<ExplicitRouteSubobject>();
55                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 10, 1, 1, 2 }), 32), false));
56                 subs.add(new EROIPPrefixSubobject<Prefix<?>>(new IPv4Prefix(new IPv4Address(new byte[] { 2, 2, 2, 2 }), 32), false));
57                 final CompositeInstantiationObject cpo = new CompositeInstantiationObject(new PCEPEndPointsObject<IPv4Address>(IPv4.FAMILY.addressForBytes(new byte[] {
58                                 1, 1, 1, 1 }), IPv4.FAMILY.addressForBytes(new byte[] { 2, 2, 2, 2 })), new PCEPLspaObject(0, 0, 0, (short) 0, (short) 0, false, false, false, false), new PCEPExplicitRouteObject(subs, false), null, null);
59
60                 session.sendMessage(new PCCreateMessage(Lists.newArrayList(cpo)));
61         }
62
63         @Override
64         public void onSessionDown(final PCEPSession session, final Exception e) {
65                 logger.debug("Session down with cause : {} or exception: {}", e);
66                 session.close();
67         }
68
69         @Override
70         public void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason cause) {
71                 logger.debug("Session terminated. Cause : {}", cause.toString());
72         }
73 }