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