2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.pcep.pcc.mock.protocol;
11 import java.util.Random;
12 import org.opendaylight.protocol.pcep.PCEPSession;
13 import org.opendaylight.protocol.pcep.PCEPSessionListener;
14 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
15 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCSession;
16 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
17 import org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Pcinitiate;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Pcrpt;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Pcupd;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.Updates;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.Srp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class PCCSessionListener implements PCEPSessionListener, PCCSession {
32 private static final Logger LOG = LoggerFactory.getLogger(PCCSessionListener.class);
34 private final boolean errorMode;
35 private final PCCTunnelManager tunnelManager;
36 private final int sessionId;
37 private PCEPSession session;
39 public PCCSessionListener(final int sessionId, final PCCTunnelManager tunnelManager, final boolean errorMode) {
40 this.errorMode = errorMode;
41 this.tunnelManager = tunnelManager;
42 this.sessionId = sessionId;
46 public void onMessage(final PCEPSession psession, final Message message) {
47 LOG.trace("Received message: {}", message);
49 //random error message
50 psession.sendMessage(createErrorMessage(message));
53 if (message instanceof Pcupd) {
54 final Updates upd = ((Pcupd) message).getPcupdMessage().getUpdates().get(0);
55 this.tunnelManager.onMessagePcupd(upd, this);
56 } else if (message instanceof Pcinitiate) {
57 this.tunnelManager.onMessagePcInitiate(((Pcinitiate) message)
58 .getPcinitiateMessage().getRequests().get(0), this);
63 public void onSessionUp(final PCEPSession psession) {
64 LOG.debug("Session up.");
65 this.session = psession;
66 this.tunnelManager.onSessionUp(this);
70 @SuppressWarnings("checkstyle:IllegalCatch")
71 public void onSessionDown(final PCEPSession psession, final Exception exception) {
72 LOG.info("Session down with cause : {} or exception: {}", exception.getCause(), exception, exception);
73 this.tunnelManager.onSessionDown(this);
76 } catch (Exception ie) {
77 LOG.warn("Error closing session", ie);
82 public void onSessionTerminated(final PCEPSession psession, final PCEPTerminationReason cause) {
83 LOG.info("Session terminated. Cause : {}", cause.toString());
87 public void sendReport(final Pcrpt reportMessage) {
88 this.session.sendMessage(reportMessage);
92 public void sendError(final Pcerr errorMessage) {
93 this.session.sendMessage(errorMessage);
98 return this.sessionId;
102 public Tlvs getRemoteTlvs() {
103 return this.session.getRemoteTlvs();
107 public Tlvs localSessionCharacteristics() {
108 return this.session.getLocalTlvs();
111 private final Random rnd = new Random();
113 private PCEPErrors getRandomError() {
114 return PCEPErrors.values()[this.rnd.nextInt(PCEPErrors.values().length)];
117 private Pcerr createErrorMessage(final Message message) {
119 if (message instanceof Pcupd) {
120 srp = ((Pcupd) message).getPcupdMessage().getUpdates().get(0).getSrp();
122 srp = ((Pcinitiate) message).getPcinitiateMessage().getRequests().get(0).getSrp();
124 return MsgBuilderUtil.createErrorMsg(getRandomError(), srp.getOperationId().getValue());