Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[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     protected 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         super(clientType, clientHandle);
40         this._process = process;
41     }
42
43     @Override
44     protected void initRequestState(final Socket sock) throws COPSPdpException {
45         // Inits an object for sending COPS messages to the PEP
46         _sender = new COPSPdpMsgSender(_clientType, _handle, sock);
47
48         // Initial state
49         _status = Status.ST_INIT;
50     }
51
52     /**
53      * Processes a COPS request
54      * @param msg   COPS request received from the PEP
55      * @throws COPSPdpException
56      */
57     protected void processRequest(final COPSReqMsg msg) throws COPSPdpException {
58
59         // TODO - Implement me
60 //        COPSHeader hdrmsg = msg.getHeader();
61 //        COPSHandle handlemsg = msg.getClientHandle();
62 //        COPSContext contextmsg = msg.getContext();
63
64         //** Analyze the request
65         //**
66
67         /* <Request> ::= <Common Header>
68         *                   <Client Handle>
69         *                   <Context>
70         *                   *(<Named ClientSI>)
71         *                   [<Integrity>]
72         * <Named ClientSI> ::= <*(<PRID> <EPD>)>
73         *
74         * Very important, this is actually being treated like this:
75         * <Named ClientSI> ::= <PRID> | <EPD>
76         *
77
78         // Named ClientSI
79         Vector clientSIs = msg.getClientSI();
80         Hashtable reqSIs = new Hashtable(40);
81         String strobjprid = new String();
82         for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
83             COPSClientSI clientSI = (COPSClientSI) e.nextElement();
84
85             COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
86             switch (obj.getSNum())
87             {
88                 case COPSPrObjBase.PR_PRID:
89                     strobjprid = obj.getData().str();
90                     break;
91                 case COPSPrObjBase.PR_EPD:
92                     reqSIs.put(strobjprid, obj.getData().str());
93                     // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
94                     // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
95                     break;
96                 default:
97                     break;
98             }
99         }
100
101         //** Here we must retrieve a decision depending on
102         //** the supplied ClientSIs
103         // reqSIs is a hashtable with the prid and epds
104
105         // ................
106         //
107         Hashtable removeDecs = new Hashtable();
108         Hashtable installDecs = new Hashtable();
109         _process.setClientData(this, reqSIs);
110
111         removeDecs = _process.getRemovePolicy(this);
112         installDecs = _process.getInstallPolicy(this);
113
114         //** We create the SOLICITED decision
115         //**
116         _sender.sendDecision(removeDecs, installDecs);
117         _status = ST_DECS;
118         */
119     }
120
121     /**
122      * Processes a report
123      * @param msg   Report message from the PEP
124      * @throws COPSPdpException
125      */
126     protected void processReport(final COPSReportMsg msg) throws COPSPdpException {
127
128         //** Analyze the report
129         //**
130
131         /*
132          * <Report State> ::= <Common Header>
133          *                      <Client Handle>
134          *                      <Report Type>
135          *                      *(<Named ClientSI>)
136          *                      [<Integrity>]
137          * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
138          * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
139          *
140          * Important, <Named ClientSI> is not parsed
141         */
142
143         // COPSHeader hdrmsg = msg.getHeader();
144         // COPSHandle handlemsg = msg.getClientHandle();
145
146         if (msg.getClientSI() != null) {
147             // Report Type
148             final COPSReportType rtypemsg = msg.getReport();
149             final Map<String, String> repSIs = new HashMap<>();
150             String strobjprid = "";
151             final COPSPrObjBase obj = new COPSPrObjBase(msg.getClientSI().getData().getData());
152             switch (obj.getSNum()) {
153                 case COPSPrObjBase.PR_PRID:
154                     strobjprid = obj.getData().str();
155                     break;
156                 case COPSPrObjBase.PR_EPD:
157                     repSIs.put(strobjprid, obj.getData().str());
158                     logger.info("PRID: " + strobjprid);
159                     logger.info("EPD: " + obj.getData().str());
160                     break;
161                 default:
162                     break;
163             }
164
165             //** Here we must act in accordance with
166             //** the report received
167             switch (rtypemsg.getReportType()) {
168                 case SUCCESS:
169                     _status = Status.ST_REPORT;
170                     _process.successReport(this, repSIs);
171                     break;
172                 case FAILURE:
173                     _status = Status.ST_REPORT;
174                     _process.failReport(this, repSIs);
175                     break;
176                 case ACCOUNTING:
177                     _status = Status.ST_ACCT;
178                     _process.acctReport(this, repSIs);
179                     break;
180             }
181         }
182
183
184     }
185
186     /**
187      * Called when connection is closed
188      * @param error Reason
189      * @throws COPSPdpException
190      */
191     protected void processClosedConnection(final COPSError error) throws COPSPdpException {
192         if (_process != null)
193             _process.notifyClosedConnection(this, error);
194
195         _status = Status.ST_CCONN;
196     }
197
198     /**
199      * Called when no keep-alive is received
200      * @throws COPSPdpException
201      */
202     protected void processNoKAConnection() throws COPSPdpException {
203         if (_process != null)
204             _process.notifyNoKAliveReceived(this);
205
206         _status = Status.ST_NOKA;
207     }
208
209     /**
210      * Deletes the request state
211      * @throws COPSPdpException
212      */
213     protected void finalizeRequestState() throws COPSException {
214         _sender.sendDeleteRequestState();
215         _status = Status.ST_FINAL;
216     }
217
218     /**
219      * Asks for a COPS sync
220      * @throws COPSPdpException
221      */
222     protected void syncRequestState() throws COPSException {
223         _sender.sendSyncRequestState();
224         _status = Status.ST_SYNC;
225     }
226
227     /**
228      * Opens a new request state
229      * @throws COPSPdpException
230      */
231     protected void openNewRequestState() throws COPSException {
232         _sender.sendOpenNewRequestState();
233         _status = Status.ST_NEW;
234     }
235
236     /**
237      * Processes a COPS delete message
238      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PEP
239      * @throws COPSPdpException
240      */
241     protected void processDeleteRequestState(final COPSDeleteMsg dMsg) throws COPSPdpException {
242         if (_process != null)
243             _process.closeRequestState(this);
244
245         _status = Status.ST_DEL;
246     }
247
248 }