Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospdp / COPSPdpOSReqStateMan.java
1 package org.umu.cops.ospdp;
2
3 import org.umu.cops.COPSStateMan;
4 import org.umu.cops.stack.*;
5
6 import java.net.Socket;
7 import java.util.Vector;
8
9 /**
10  * State manager class for outsourcing requests, at the PDP side.
11  */
12 public class COPSPdpOSReqStateMan extends COPSStateMan {
13
14     /**
15      * Object for performing policy data processing
16      */
17     private final COPSPdpOSDataProcess _process;
18
19     /** COPS message transceiver used to send COPS messages */
20     private transient COPSPdpOSMsgSender _sender;
21
22     /**
23      * Creates a request state manager
24      * @param clientType    Client-type
25      * @param clientHandle  Client handle
26      */
27     // TODO - consider sending in the COPSHandle object instead
28     public COPSPdpOSReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPdpOSDataProcess process) {
29         super(clientType, clientHandle);
30         this._process = process;
31     }
32
33     @Override
34     protected void initRequestState(final Socket sock) throws COPSException {
35         // Inits an object for sending COPS messages to the PDP
36         _sender = new COPSPdpOSMsgSender(_clientType, _handle, sock);
37
38         // Initial state
39         _status = Status.ST_INIT;
40     }
41
42     /**
43      * Processes a COPS request
44      * @param msg   COPS request received from the PEP
45      * @throws COPSException
46      */
47     protected void processRequest(COPSReqMsg msg) throws COPSException {
48         //** Here we must retrieve a decision depending on the
49         //** supplied ClientSIs
50         /*Vector removeDecs = new Vector();
51         Vector installDecs = new Vector();*/
52         if (msg.getClientSI() != null)
53             _process.setClientData(this, msg.getClientSI().toArray(new COPSClientSI[msg.getClientSI().size()]));
54
55         Vector removeDecs = _process.getRemovePolicy(this);
56         Vector installDecs = _process.getInstallPolicy(this);
57
58         //** We create a SOLICITED decision
59         //**
60         _sender.sendSolicitedDecision(removeDecs, installDecs);
61         _status = Status.ST_DECS;
62     }
63
64     /**
65      * Processes a report
66      * @param msg   Report message from the PEP
67      * @throws COPSPdpException
68      */
69     protected void processReport(final COPSReportMsg msg) throws COPSPdpException {
70         //** Analyze the report
71         //**
72
73         /*
74          * <Report State> ::= <Common Header>
75          *                      <Client Handle>
76          *                      <Report Type>
77          *                      *(<Named ClientSI>)
78          *                      [<Integrity>]
79          * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
80          * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
81          *
82          * Important, <Named ClientSI> is not parsed
83         */
84
85         // COPSHeader hdrmsg = msg.getHeader();
86         // COPSHandle handlemsg = msg.getClientHandle();
87
88         // Report Type
89         final COPSReportType rtypemsg = msg.getReport();
90
91         // Named ClientSI
92         if (msg.getClientSI() != null) {
93             //** Here we must act in accordance with
94             //** the report received
95             switch (rtypemsg.getReportType()) {
96                 case SUCCESS:
97                     _status = Status.ST_REPORT;
98                     _process.successReport(this, msg.getClientSI());
99                     break;
100                 case FAILURE:
101                     _status = Status.ST_REPORT;
102                     _process.failReport(this, msg.getClientSI());
103                     break;
104                 case ACCOUNTING:
105                     _status = Status.ST_ACCT;
106                     _process.acctReport(this, msg.getClientSI());
107                     break;
108             }
109         }
110
111     }
112
113     /**
114      * Called when connection is closed
115      * @param error Reason
116      * @throws COPSPdpException
117      */
118     protected void processClosedConnection(final COPSError error) throws COPSPdpException {
119         if (_process != null)
120             _process.notifyClosedConnection(this, error);
121
122         _status = Status.ST_CCONN;
123     }
124
125     /**
126      * Called when no keep-alive is received
127      * @throws COPSPdpException
128      */
129     protected void processNoKAConnection() throws COPSPdpException {
130         if (_process != null)
131             _process.notifyNoKAliveReceived(this);
132
133         _status = Status.ST_NOKA;
134     }
135
136     /**
137      * Deletes the request state
138      * @throws COPSPdpException
139      */
140     protected void finalizeRequestState() throws COPSException {
141         _sender.sendDeleteRequestState();
142         _status = Status.ST_FINAL;
143     }
144
145     /**
146      * Asks for a COPS sync
147      * @throws COPSPdpException
148      */
149     protected void syncRequestState() throws COPSException {
150         _sender.sendSyncRequestState();
151         _status = Status.ST_SYNC;
152     }
153
154     /**
155      * Opens a new request state
156      * @throws COPSPdpException
157      */
158     protected void openNewRequestState() throws COPSException {
159         _sender.sendOpenNewRequestState();
160         _status = Status.ST_NEW;
161     }
162
163     /**
164      * Processes a COPS delete message
165      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PEP
166      * @throws COPSPdpException
167      */
168     protected void processDeleteRequestState(final COPSDeleteMsg dMsg) throws COPSPdpException {
169         if (_process != null)
170             _process.closeRequestState(this);
171
172         _status = Status.ST_DEL;
173     }
174
175 }