Cleanup of state managers' interfaces (constructor and init).
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpdp / COPSPdpReqStateMan.java
1 /*
2  * Copyright (c) 2004 University of Murcia.  All rights reserved.
3  * --------------------------------------------------------------
4  * For more information, please see <http://www.umu.euro6ix.org/>.
5  */
6
7 package org.umu.cops.prpdp;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.COPSStateMan;
12 import org.umu.cops.stack.*;
13
14 import java.net.Socket;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /**
19  * State manager class for provisioning requests, at the PDP side.
20  */
21 public class COPSPdpReqStateMan extends COPSStateMan {
22
23     private final static Logger logger = LoggerFactory.getLogger(COPSPdpReqStateMan.class);
24
25     /**
26      * Object for performing policy data processing
27      */
28     private final COPSPdpDataProcess _process;
29
30     /** COPS message transceiver used to send COPS messages */
31     protected transient COPSPdpMsgSender _sender;
32
33     /**
34      * Creates a request state manager
35      * @param clientType    Client-type
36      * @param clientHandle  Client handle
37      */
38     public COPSPdpReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPdpDataProcess process,
39                               final Socket socket) {
40         super(clientType, clientHandle, socket);
41         this._process = process;
42         _sender = new COPSPdpMsgSender(_clientType, _handle, _socket);
43         _status = Status.ST_INIT;
44     }
45
46     /**
47      * Processes a COPS request
48      * @param msg   COPS request received from the PEP
49      * @throws COPSException
50      */
51     protected void processRequest(final COPSReqMsg msg) throws COPSException {
52         // TODO - Implement me - see commented out code from history prior to May 4, 2015...
53     }
54
55     /**
56      * Processes a report
57      * @param msg   Report message from the PEP
58      * @throws COPSException
59      */
60     protected void processReport(final COPSReportMsg msg) throws COPSException {
61         if (msg.getClientSI() != null) {
62             // Report Type
63             final COPSReportType rtypemsg = msg.getReport();
64             final Map<String, String> repSIs = new HashMap<>();
65             String strobjprid = "";
66             final COPSPrObjBase obj = new COPSPrObjBase(msg.getClientSI().getData().getData());
67             switch (obj.getSNum()) {
68                 case COPSPrObjBase.PR_PRID:
69                     strobjprid = obj.getData().str();
70                     break;
71                 case COPSPrObjBase.PR_EPD:
72                     // TODO FIXME - strobjprid is always empty here???
73                     repSIs.put(strobjprid, obj.getData().str());
74                     logger.info("PRID: " + strobjprid);
75                     logger.info("EPD: " + obj.getData().str());
76                     break;
77                 default:
78                     break;
79             }
80
81             //** Here we must act in accordance with
82             //** the report received
83             switch (rtypemsg.getReportType()) {
84                 case SUCCESS:
85                     _status = Status.ST_REPORT;
86                     _process.successReport(this, repSIs);
87                     break;
88                 case FAILURE:
89                     _status = Status.ST_REPORT;
90                     _process.failReport(this, repSIs);
91                     break;
92                 case ACCOUNTING:
93                     _status = Status.ST_ACCT;
94                     _process.acctReport(this, repSIs);
95                     break;
96             }
97         }
98     }
99
100     /**
101      * Called when connection is closed
102      * @param error Reason
103      * @throws COPSPdpException
104      */
105     public void processClosedConnection(final COPSError error) throws COPSException {
106         if (_process != null)
107             _process.notifyClosedConnection(this, error);
108
109         _status = Status.ST_CCONN;
110     }
111
112     /**
113      * Called when no keep-alive is received
114      * @throws COPSPdpException
115      */
116     public void processNoKAConnection() throws COPSException {
117         if (_process != null)
118             _process.notifyNoKAliveReceived(this);
119
120         _status = Status.ST_NOKA;
121     }
122
123     /**
124      * Deletes the request state
125      * @throws COPSPdpException
126      */
127     protected void finalizeRequestState() throws COPSException {
128         _sender.sendDeleteRequestState();
129         _status = Status.ST_FINAL;
130     }
131
132     /**
133      * Asks for a COPS sync
134      * @throws COPSPdpException
135      */
136     public void syncRequestState() throws COPSException {
137         _sender.sendSyncRequestState();
138         _status = Status.ST_SYNC;
139     }
140
141     /**
142      * Opens a new request state
143      * @throws COPSPdpException
144      */
145     protected void openNewRequestState() throws COPSException {
146         _sender.sendOpenNewRequestState();
147         _status = Status.ST_NEW;
148     }
149
150     /**
151      * Processes a COPS delete message
152      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PEP
153      * @throws COPSPdpException
154      */
155     public void processDeleteRequestState(final COPSDeleteMsg dMsg) throws COPSException {
156         if (_process != null)
157             _process.closeRequestState(this);
158
159         _status = Status.ST_DEL;
160     }
161
162 }