Add new revision for pcep types model
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / protocol / PCCSessionListener.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.pcc.mock.protocol;
10
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;
29
30 public final class PCCSessionListener implements PCEPSessionListener, PCCSession {
31
32     private static final Logger LOG = LoggerFactory.getLogger(PCCSessionListener.class);
33
34     private final boolean errorMode;
35     private final PCCTunnelManager tunnelManager;
36     private final int sessionId;
37     private PCEPSession session;
38
39     public PCCSessionListener(final int sessionId, final PCCTunnelManager tunnelManager, final boolean errorMode) {
40         this.errorMode = errorMode;
41         this.tunnelManager = tunnelManager;
42         this.sessionId = sessionId;
43     }
44
45     @Override
46     public void onMessage(final PCEPSession psession, final Message message) {
47         LOG.trace("Received message: {}", message);
48         if (this.errorMode) {
49             //random error message
50             psession.sendMessage(createErrorMessage(message));
51             return;
52         }
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);
59         }
60     }
61
62     @Override
63     public void onSessionUp(final PCEPSession psession) {
64         LOG.debug("Session up.");
65         this.session = psession;
66         this.tunnelManager.onSessionUp(this);
67     }
68
69     @Override
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);
74         try {
75             psession.close();
76         } catch (Exception ie) {
77             LOG.warn("Error closing session", ie);
78         }
79     }
80
81     @Override
82     public void onSessionTerminated(final PCEPSession psession, final PCEPTerminationReason cause) {
83         LOG.info("Session terminated. Cause : {}", cause.toString());
84     }
85
86     @Override
87     public void sendReport(final Pcrpt reportMessage) {
88         this.session.sendMessage(reportMessage);
89     }
90
91     @Override
92     public void sendError(final Pcerr errorMessage) {
93         this.session.sendMessage(errorMessage);
94     }
95
96     @Override
97     public int getId() {
98         return this.sessionId;
99     }
100
101     @Override
102     public Tlvs getRemoteTlvs() {
103         return this.session.getRemoteTlvs();
104     }
105
106     @Override
107     public Tlvs localSessionCharacteristics() {
108         return this.session.getLocalTlvs();
109     }
110
111     private final Random rnd = new Random();
112
113     private PCEPErrors getRandomError() {
114         return PCEPErrors.values()[this.rnd.nextInt(PCEPErrors.values().length)];
115     }
116
117     private Pcerr createErrorMessage(final Message message) {
118         final Srp srp;
119         if (message instanceof Pcupd) {
120             srp = ((Pcupd) message).getPcupdMessage().getUpdates().get(0).getSrp();
121         } else {
122             srp = ((Pcinitiate) message).getPcinitiateMessage().getRequests().get(0).getSrp();
123         }
124         return MsgBuilderUtil.createErrorMsg(getRandomError(), srp.getOperationId().getValue());
125     }
126
127 }