Completed COPS Message refactoring. Was planning on one additional patch starting...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospep / COPSPepOSReqStateMan.java
1 package org.umu.cops.ospep;
2
3 import org.umu.cops.stack.*;
4 import org.umu.cops.stack.COPSHeader.ClientType;
5
6 import java.net.Socket;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.Vector;
10
11 /**
12  * State manager class for outsourcing requests, at the PEP side.
13  */
14 public class COPSPepOSReqStateMan {
15     /**
16      * Request State created
17      */
18     public final static short ST_CREATE = 1;
19     /**
20      * Request sent
21      */
22     public final static short ST_INIT = 2;
23     /**
24      * Decisions received
25      */
26     public final static short ST_DECS = 3;
27     /**
28      * Report sent
29      */
30     public final static short ST_REPORT = 4;
31     /**
32      * Request State finalized
33      */
34     public final static short ST_FINAL = 5;
35     /**
36      * New Request State solicited
37      */
38     public final static short ST_NEW = 6;
39     /**
40      * Delete Request State solicited
41      */
42     public final static short ST_DEL = 7;
43     /**
44      * SYNC request received
45      */
46     public final static short ST_SYNC = 8;
47     /**
48      * Sync completed
49      */
50     public final static short ST_SYNCALL = 9;
51     /**
52      * Close connection received
53      */
54     public final static short ST_CCONN = 10;
55     /**
56      * Keep-alive timeout
57      */
58     public final static short ST_NOKA = 11;
59     /**
60      * Accounting timeout
61      */
62     public final static short ST_ACCT = 12;
63
64     /**
65      * COPS client-type that identifies the policy client
66      */
67     protected ClientType _clientType;
68
69     /**
70      *  COPS client handle used to uniquely identify a particular
71      *  PEP's request for a client-type
72      */
73     protected COPSHandle _handle;
74
75     /**
76         Object for performing policy data processing
77      */
78     protected COPSPepOSDataProcess _process;
79
80     /**
81      * ClientSI data from signaling.
82      */
83     protected final Set<COPSClientSI> _clientSIs;
84
85     /**
86      *  Current state of the request being managed
87      */
88     protected short _status;
89
90     /**
91         COPS message transceiver used to send COPS messages
92      */
93     protected COPSPepOSMsgSender _sender;
94
95     /**
96      * Sync state
97      */
98     protected boolean _syncState;
99
100     /**
101      * Creates a state request manager
102      * @param    clientType Client-type
103      * @param   clientHandle    Client's <tt>COPSHandle</tt>
104      */
105     public COPSPepOSReqStateMan(final ClientType clientType, final String clientHandle) {
106         // COPS Handle
107         _handle = new COPSHandle(new COPSData(clientHandle));
108         _clientType = clientType;
109         _syncState = true;
110         _status = ST_CREATE;
111         _clientSIs = null;
112     }
113
114     /**
115      * Gets the client handle
116      * @return  Client's <tt>COPSHandle</tt>
117      */
118     public COPSHandle getClientHandle() {
119         return _handle;
120     }
121
122     /**
123      * Sets the client SI data.
124      * @param someClientSIs Client SI data built by the event listener
125      */
126     public void setClientSI(final List<COPSClientSI> someClientSIs) {
127         _clientSIs.addAll(someClientSIs);
128     }
129
130     /**
131      * Gets the client-type
132      * @return  Client-type value
133      */
134     public ClientType getClientType() {
135         return _clientType;
136     }
137
138     /**
139      * Gets the request status
140      * @return  Request status value
141      */
142     public short getStatus() {
143         return _status;
144     }
145
146     /**
147      * Gets the policy data processing object
148      *
149      * @return   Policy data processing object
150      *
151      */
152     public COPSPepOSDataProcess getDataProcess() {
153         return _process;
154     }
155
156     /**
157      * Sets the policy data processing object
158      *
159      * @param   process   Policy data processing object
160      *
161      */
162     public void setDataProcess(COPSPepOSDataProcess process) {
163         _process = process;
164     }
165
166     /**
167      * Initializes a new request state over a socket
168      * @param sock  Socket to the PDP
169      * @throws COPSPepException
170      */
171     protected void initRequestState(Socket sock) throws COPSPepException {
172         // Inits an object for sending COPS messages to the PDP
173         _sender = new COPSPepOSMsgSender(_clientType, _handle, sock);
174
175         // If an object exists for retrieving the PEP features,
176         // use it for retrieving them.
177         /*      Hashtable clientSIs;
178                 if (_process != null)
179                     clientSIs = _process.getClientData(this);
180                 else
181                     clientSIs = null;*/
182
183         // Semd the request
184         _sender.sendRequest(_clientSIs);
185
186         // Initial state
187         _status = ST_INIT;
188     }
189
190     /**
191      * Deletes the request state
192      * @throws COPSPepException
193      */
194     protected void finalizeRequestState() throws COPSPepException {
195         _sender.sendDeleteRequest();
196         _status = ST_FINAL;
197     }
198
199     /**
200      * Processes the decision message
201      * @param    dMsg Decision message from the PDP
202      * @throws   COPSPepException
203      */
204     protected void processDecision(COPSDecisionMsg dMsg) throws COPSPepException {
205         // COPSDebug.out(getClass().getName(), "ClientId:" + getClientHandle().getId().str());
206
207         //Hashtable decisionsPerContext = dMsg.getDecisions();
208
209         //** Applies decisions to the configuration
210         //_process.setDecisions(this, removeDecs, installDecs, errorDecs);
211         // second param changed to dMsg so that the data processor
212         // can check the 'solicited' flag
213         boolean isFailReport = _process.setDecisions(this, dMsg /*decisionsPerContext*/);
214         _status = ST_DECS;
215
216         if (isFailReport) {
217             // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
218             _sender.sendFailReport(_process.getReportData(this));
219         } else {
220             // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
221             _sender.sendSuccessReport(_process.getReportData(this));
222         }
223         _status = ST_REPORT;
224
225         if (!_syncState) {
226             _sender.sendSyncComplete();
227             _syncState = true;
228             _status = ST_SYNCALL;
229         }
230     }
231
232
233     /**
234      * Processes a COPS delete message
235      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PDP
236      * @throws COPSPepException
237      */
238     protected void processDeleteRequestState(COPSDecisionMsg dMsg) throws COPSPepException {
239         if (_process != null)
240             _process.closeRequestState(this);
241
242         _status = ST_DEL;
243     }
244
245     /**
246      * Processes the message SycnStateRequest.
247      * The message SycnStateRequest indicates that the remote PDP
248      * wishes the client (which appears in the common header)
249      * to re-send its state.
250      *
251      * @param    ssMsg               The sync request from the PDP
252      *
253      * @throws   COPSPepException
254      *
255      */
256     protected void processSyncStateRequest(COPSSyncStateMsg ssMsg) throws COPSPepException {
257         _syncState = false;
258         // If an object exists for retrieving the PEP features,
259         // use it for retrieving them.
260
261         // Send the request
262         _sender.sendRequest(_clientSIs);
263
264         _status = ST_SYNC;
265     }
266
267     /**
268      * Called when connection is closed
269      * @param error Reason
270      * @throws COPSPepException
271      */
272     protected void processClosedConnection(COPSError error) throws COPSPepException {
273         if (_process != null)
274             _process.notifyClosedConnection(this, error);
275
276         _status = ST_CCONN;
277     }
278
279     /**
280      * Called when no keep-alive is received
281      * @throws COPSPepException
282      */
283     protected void processNoKAConnection() throws COPSPepException {
284         if (_process != null)
285             _process.notifyNoKAliveReceived(this);
286
287         _status = ST_NOKA;
288     }
289
290     /**
291      * Processes the accounting report
292      * @throws COPSPepException
293      */
294     protected void processAcctReport() throws COPSPepException {
295         Vector report = new Vector();
296
297         if (_process != null)
298             report = _process.getAcctData(this);
299
300         _sender.sendAcctReport(report);
301
302         _status = ST_ACCT;
303     }
304 }