Fixed building of models, moved code into directory structure.
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / ospdp / COPSPdpOSReqStateMan.java
1 package org.umu.cops.ospdp;\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.COPSDeleteMsg;\r
8 import org.umu.cops.stack.COPSError;\r
9 import org.umu.cops.stack.COPSHandle;\r
10 import org.umu.cops.stack.COPSReportMsg;\r
11 import org.umu.cops.stack.COPSReportType;\r
12 import org.umu.cops.stack.COPSReqMsg;\r
13 import org.umu.cops.stack.COPSSyncStateMsg;\r
14 \r
15 /**\r
16  * State manager class for outsourcing requests, at the PDP side.\r
17  */\r
18 public class COPSPdpOSReqStateMan {\r
19     /**\r
20      * Request State created\r
21      */\r
22     public final static short ST_CREATE = 1;\r
23     /**\r
24      * Request received\r
25      */\r
26     public final static short ST_INIT = 2;\r
27     /**\r
28      * Decisions sent\r
29      */\r
30     public final static short ST_DECS = 3;\r
31     /**\r
32      * Report received\r
33      */\r
34     public final static short ST_REPORT = 4;\r
35     /**\r
36      * Request state finalized\r
37      */\r
38     public final static short ST_FINAL = 5;\r
39     /**\r
40      * New request state solicited\r
41      */\r
42     public final static short ST_NEW = 6;\r
43     /**\r
44      * Delete request state solicited\r
45      */\r
46     public final static short ST_DEL = 7;\r
47     /**\r
48      * SYNC request sent\r
49      */\r
50     public final static short ST_SYNC = 8;\r
51     /**\r
52      * SYNC completed\r
53      */\r
54     public final static short ST_SYNCALL = 9;\r
55     /**\r
56      * Close connection received\r
57      */\r
58     public final static short ST_CCONN = 10;\r
59     /**\r
60      * Keep-alive timeout\r
61      */\r
62     public final static short ST_NOKA = 11;\r
63     /**\r
64      * Accounting timeout\r
65      */\r
66     public final static short ST_ACCT = 12;\r
67 \r
68     /**\r
69      * COPS client-type that identifies the policy client\r
70      */\r
71     protected short _clientType;\r
72 \r
73     /**\r
74      *  COPS client handle used to uniquely identify a particular\r
75      *  PEP's request for a client-type\r
76      */\r
77     protected COPSHandle _handle;\r
78 \r
79     /**\r
80      * Object for performing policy data processing\r
81      */\r
82     protected COPSPdpOSDataProcess _process;\r
83 \r
84     /**\r
85      *  Current state of the request being managed\r
86      */\r
87     protected short _status;\r
88 \r
89     /** COPS message transceiver used to send COPS messages */\r
90     protected COPSPdpOSMsgSender _sender;\r
91 \r
92     /**\r
93      * Creates a request state manager\r
94      * @param clientType    Client-type\r
95      * @param clientHandle  Client handle\r
96      */\r
97     public COPSPdpOSReqStateMan(short clientType, String clientHandle) {\r
98         // COPS Handle\r
99         _handle = new COPSHandle();\r
100         COPSData id = new COPSData(clientHandle);\r
101         _handle.setId(id);\r
102         // client-type\r
103         _clientType = clientType;\r
104 \r
105         _status = ST_CREATE;\r
106     }\r
107 \r
108     /**\r
109      * Gets the client handle\r
110      * @return   Client's <tt>COPSHandle</tt>\r
111      */\r
112     public COPSHandle getClientHandle() {\r
113         return _handle;\r
114     }\r
115 \r
116     /**\r
117      * Gets the client-type\r
118      * @return   Client-type value\r
119      */\r
120     public short getClientType() {\r
121         return _clientType;\r
122     }\r
123 \r
124     /**\r
125      * Gets the status of the request\r
126      * @return      Request state value\r
127      */\r
128     public short getStatus() {\r
129         return _status;\r
130     }\r
131 \r
132     /**\r
133      * Gets the policy data processing object\r
134      * @return   Policy data processing object\r
135      */\r
136     public COPSPdpOSDataProcess getDataProcess() {\r
137         return _process;\r
138     }\r
139 \r
140     /**\r
141      * Sets the policy data processing object\r
142      * @param   process Policy data processing object\r
143      */\r
144     public void setDataProcess(COPSPdpOSDataProcess process) {\r
145         _process = process;\r
146     }\r
147 \r
148     /**\r
149      * Called when COPS sync is completed\r
150      * @param    repMsg              COPS sync message\r
151      * @throws   COPSPdpException\r
152      */\r
153     protected void processSyncComplete(COPSSyncStateMsg repMsg)\r
154     throws COPSPdpException {\r
155 \r
156         _status = ST_SYNCALL;\r
157 \r
158         // maybe we should notifySyncComplete ...\r
159     }\r
160 \r
161     /**\r
162      * Initializes a new request state over a socket\r
163      * @param sock  Socket to the PEP\r
164      * @throws COPSPdpException\r
165      */\r
166     protected void initRequestState(Socket sock)\r
167     throws COPSPdpException {\r
168         // Inits an object for sending COPS messages to the PDP\r
169         _sender = new COPSPdpOSMsgSender(_clientType, _handle, sock);\r
170 \r
171         // Initial state\r
172         _status = ST_INIT;\r
173     }\r
174 \r
175     /**\r
176      * Processes a COPS request\r
177      * @param msg   COPS request received from the PEP\r
178      * @throws COPSPdpException\r
179      */\r
180     protected void processRequest(COPSReqMsg msg) throws COPSPdpException {\r
181         Vector clientSIs = msg.getClientSI();\r
182 \r
183         //** Here we must retrieve a decision depending on the\r
184         //** supplied ClientSIs\r
185         /*Vector removeDecs = new Vector();\r
186         Vector installDecs = new Vector();*/\r
187         _process.setClientData(this, clientSIs);\r
188 \r
189         Vector removeDecs = _process.getRemovePolicy(this);\r
190         Vector installDecs = _process.getInstallPolicy(this);\r
191 \r
192         //** We create a SOLICITED decision\r
193         //**\r
194         _sender.sendSolicitedDecision(removeDecs, installDecs);\r
195         _status = ST_DECS;\r
196     }\r
197 \r
198     /**\r
199      * Processes a report\r
200      * @param msg   Report message from the PEP\r
201      * @throws COPSPdpException\r
202      */\r
203     protected void processReport(COPSReportMsg msg) throws COPSPdpException {\r
204         //** Analyze the report\r
205         //**\r
206 \r
207         /*\r
208          * <Report State> ::= <Common Header>\r
209          *                      <Client Handle>\r
210          *                      <Report Type>\r
211          *                      *(<Named ClientSI>)\r
212          *                      [<Integrity>]\r
213          * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>\r
214          * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)\r
215          *\r
216          * Important, <Named ClientSI> is not parsed\r
217         */\r
218 \r
219         // COPSHeader hdrmsg = msg.getHeader();\r
220         // COPSHandle handlemsg = msg.getClientHandle();\r
221 \r
222         // Report Type\r
223         COPSReportType rtypemsg = msg.getReport();\r
224 \r
225         // Named ClientSI\r
226         Vector clientSIs = msg.getClientSI();\r
227 \r
228         //** We should act here in accordance with\r
229         //** the received report\r
230         if (rtypemsg.isSuccess()) {\r
231             _status = ST_REPORT;\r
232             _process.successReport(this, clientSIs);\r
233         } else if (rtypemsg.isFailure()) {\r
234             _status = ST_REPORT;\r
235             _process.failReport(this, clientSIs);\r
236         } else if (rtypemsg.isAccounting()) {\r
237             _status = ST_ACCT;\r
238             _process.acctReport(this, clientSIs);\r
239         }\r
240     }\r
241 \r
242     /**\r
243      * Called when connection is closed\r
244      * @param error Reason\r
245      * @throws COPSPdpException\r
246      */\r
247     protected void processClosedConnection(COPSError error)\r
248     throws COPSPdpException {\r
249         if (_process != null)\r
250             _process.notifyClosedConnection(this, error);\r
251 \r
252         _status = ST_CCONN;\r
253     }\r
254 \r
255     /**\r
256      * Called when no keep-alive is received\r
257      * @throws COPSPdpException\r
258      */\r
259     protected void processNoKAConnection()\r
260     throws COPSPdpException {\r
261         if (_process != null)\r
262             _process.notifyNoKAliveReceived(this);\r
263 \r
264         _status = ST_NOKA;\r
265     }\r
266 \r
267     /**\r
268      * Deletes the request state\r
269      * @throws COPSPdpException\r
270      */\r
271     protected void finalizeRequestState()\r
272     throws COPSPdpException {\r
273         _sender.sendDeleteRequestState();\r
274         _status = ST_FINAL;\r
275     }\r
276 \r
277     /**\r
278      * Asks for a COPS sync\r
279      * @throws COPSPdpException\r
280      */\r
281     protected void syncRequestState()\r
282     throws COPSPdpException {\r
283         _sender.sendSyncRequestState();\r
284         _status = ST_SYNC;\r
285     }\r
286 \r
287     /**\r
288      * Opens a new request state\r
289      * @throws COPSPdpException\r
290      */\r
291     protected void openNewRequestState()//FIXME: unused?\r
292     throws COPSPdpException {\r
293         _sender.sendOpenNewRequestState();\r
294         _status = ST_NEW;\r
295     }\r
296 \r
297     /**\r
298      * Processes a COPS delete message\r
299      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PEP\r
300      * @throws COPSPdpException\r
301      */\r
302     protected void processDeleteRequestState(COPSDeleteMsg dMsg)\r
303     throws COPSPdpException {\r
304         if (_process != null)\r
305             _process.closeRequestState(this);\r
306 \r
307         _status = ST_DEL;\r
308     }\r
309 \r
310 }\r