7 import org.pcmm.gates.ITransactionID;
8 import org.pcmm.gates.impl.PCMMGateReq;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.prpdp.COPSPdpException;
12 import org.umu.cops.stack.*;
13 import org.umu.cops.stack.COPSReportType.ReportType;
15 import java.net.Socket;
16 import java.util.Arrays;
17 import java.util.HashMap;
21 * State manager class for provisioning requests, at the PDP side.
23 public class PCMMPdpReqStateMan {
25 public final static Logger logger = LoggerFactory.getLogger(PCMMPdpReqStateMan.class);
28 * Request State created
30 public final static short ST_CREATE = 1;
34 public final static short ST_INIT = 2;
38 public final static short ST_DECS = 3;
42 public final static short ST_REPORT = 4;
44 * Request State finalized
46 public final static short ST_FINAL = 5;
48 * New Request State solicited
50 public final static short ST_NEW = 6;
52 * Delete Request State solicited
54 public final static short ST_DEL = 7;
58 public final static short ST_SYNC = 8;
62 public final static short ST_SYNCALL = 9;
64 * Close connection received
66 public final static short ST_CCONN = 10;
70 public final static short ST_NOKA = 11;
74 public final static short ST_ACCT = 12;
77 * COPS client-type that identifies the policy client
79 protected short _clientType;
82 * COPS client handle used to uniquely identify a particular
83 * PEP's request for a client-type
85 protected COPSHandle _handle;
88 * Object for performing policy data processing
90 protected PCMMPdpDataProcess _process;
93 * Current state of the request being managed
95 protected short _status;
97 /** COPS message transceiver used to send COPS messages */
98 protected PCMMPdpMsgSender _sender;
101 * Creates a request state manager
102 * @param clientType Client-type
103 * @param clientHandle Client handle
105 public PCMMPdpReqStateMan(final short clientType, final String clientHandle) {
106 _handle = new COPSHandle(new COPSData(clientHandle));
107 _clientType = clientType;
112 * Gets the client handle
113 * @return Client's <tt>COPSHandle</tt>
115 public COPSHandle getClientHandle() {
120 * Gets the client-type
121 * @return Client-type value
123 public int getClientType() {
128 * Gets the status of the request
129 * @return Request state value
131 public short getStatus() {
136 * Gets the policy data processing object
137 * @return Policy data processing object
139 public PCMMPdpDataProcess getDataProcess() {
144 * Sets the policy data processing object
145 * @param process Policy data processing object
147 public void setDataProcess(PCMMPdpDataProcess process) {
152 * Called when COPS sync is completed
153 * @param repMsg COPS sync message
154 * @throws COPSPdpException
156 protected void processSyncComplete(COPSSyncStateMsg repMsg)
157 throws COPSPdpException {
159 _status = ST_SYNCALL;
161 // maybe we should notifySyncComplete ...
165 * Initializes a new request state over a socket
166 * @param sock Socket to the PEP
167 * @throws COPSPdpException
169 protected void initRequestState(Socket sock)
170 throws COPSPdpException {
171 // Inits an object for sending COPS messages to the PEP
172 _sender = new PCMMPdpMsgSender(_clientType, _handle, sock);
181 * Processes a COPS request
182 * @param msg COPS request received from the PEP
183 * @throws COPSPdpException
185 protected void processRequest(COPSReqMsg msg)
186 throws COPSPdpException {
188 COPSHeader hdrmsg = msg.getHeader();
189 COPSHandle handlemsg = msg.getClientHandle();
190 COPSContext contextmsg = msg.getContext();
192 //** Analyze the request
195 /* <Request> ::= <Common Header>
198 * *(<Named ClientSI>)
200 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
202 * Very important, this is actually being treated like this:
203 * <Named ClientSI> ::= <PRID> | <EPD>
207 Vector clientSIs = msg.getClientSI();
208 Hashtable reqSIs = new Hashtable(40);
209 String strobjprid = new String();
210 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
211 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
213 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
214 switch (obj.getSNum())
216 case COPSPrObjBase.PR_PRID:
217 strobjprid = obj.getData().str();
219 case COPSPrObjBase.PR_EPD:
220 reqSIs.put(strobjprid, obj.getData().str());
221 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
222 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
229 //** Here we must retrieve a decision depending on
230 //** the supplied ClientSIs
231 // reqSIs is a hashtable with the prid and epds
235 Hashtable removeDecs = new Hashtable();
236 Hashtable installDecs = new Hashtable();
237 _process.setClientData(this, reqSIs);
239 removeDecs = _process.getRemovePolicy(this);
240 installDecs = _process.getInstallPolicy(this);
242 //** We create the SOLICITED decision
244 _sender.sendDecision(removeDecs, installDecs);
251 * @param msg Report message from the PEP
252 * @throws COPSPdpException
254 protected void processReport(COPSReportMsg msg) throws COPSPdpException {
256 final COPSReportType rtypemsg = msg.getReport();
258 if (msg.getClientSI() != null) {
259 final COPSClientSI clientSI = msg.getClientSI();
261 final byte[] data = Arrays.copyOfRange(clientSI.getData().getData(), 0, clientSI.getData().getData().length);
263 // PCMMUtils.WriteBinaryDump("COPSReportClientSI", data);
264 logger.info("PCMMGateReq Parse Gate Message");
265 PCMMGateReq gateMsg = new PCMMGateReq(data);
267 final Map<String, String> repSIs = new HashMap<>();
268 String strobjprid = "";
269 final COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
270 switch (obj.getSNum()) {
271 case COPSPrObjBase.PR_PRID:
272 logger.info("COPSPrObjBase.PR_PRID");
273 strobjprid = obj.getData().str();
275 case COPSPrObjBase.PR_EPD:
276 logger.info("COPSPrObjBase.PR_EPD");
277 repSIs.put(strobjprid, obj.getData().str());
278 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
279 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
282 logger.error("Object s-num: " + obj.getSNum() + "stype " + obj.getSType());
283 logger.error("PRID: " + strobjprid);
284 logger.error("EPD: " + obj.getData().str());
288 logger.info("rtypemsg process");
289 //** Here we must act in accordance with
290 //** the report received
291 if (rtypemsg.getReportType().equals(ReportType.SUCCESS)) {
292 logger.info("rtypemsg success");
294 if (_process != null)
295 _process.successReport(this, gateMsg);
297 if (gateMsg.getTransactionID().getGateCommandType() == ITransactionID.GateDeleteAck) {
298 logger.info("GateDeleteAck: GateID = " + gateMsg.getGateID().getGateID());
299 if (gateMsg.getGateID().getGateID() == PCMMGlobalConfig.getGateID1())
300 PCMMGlobalConfig.setGateID1(0);
301 if (gateMsg.getGateID().getGateID() == PCMMGlobalConfig.getGateID2())
302 PCMMGlobalConfig.setGateID2(0);
305 if (gateMsg.getTransactionID().getGateCommandType() == ITransactionID.GateSetAck) {
306 logger.info("GateSetAck: GateID = " + gateMsg.getGateID().getGateID());
307 if (0 == PCMMGlobalConfig.getGateID1())
308 PCMMGlobalConfig.setGateID1(gateMsg.getGateID().getGateID());
309 if (0 == PCMMGlobalConfig.getGateID2())
310 PCMMGlobalConfig.setGateID2(gateMsg.getGateID().getGateID());
313 if (rtypemsg.getReportType().equals(ReportType.FAILURE)) {
314 logger.info("rtypemsg failure");
316 if (_process != null)
317 _process.failReport(this, gateMsg);
319 logger.info("Gate message error - " + gateMsg.getError().toString());
321 if (rtypemsg.getReportType().equals(ReportType.ACCOUNTING)) {
322 logger.info("rtypemsg account");
324 if (_process != null)
325 _process.acctReport(this, gateMsg);
331 * Called when connection is closed
332 * @param error Reason
333 * @throws COPSPdpException
335 protected void processClosedConnection(COPSError error)
336 throws COPSPdpException {
337 if (_process != null)
338 _process.notifyClosedConnection(this, error);
344 * Called when no keep-alive is received
345 * @throws COPSPdpException
347 protected void processNoKAConnection()
348 throws COPSPdpException {
349 if (_process != null)
350 _process.notifyNoKAliveReceived(this);
356 * Deletes the request state
357 * @throws COPSPdpException
359 protected void finalizeRequestState()
360 throws COPSPdpException {
361 _sender.sendDeleteRequestState();
366 * Asks for a COPS sync
367 * @throws COPSPdpException
369 protected void syncRequestState()
370 throws COPSPdpException {
371 _sender.sendSyncRequestState();
376 * Opens a new request state
377 * @throws COPSPdpException
379 protected void openNewRequestState()
380 throws COPSPdpException {
381 _sender.sendOpenNewRequestState();
386 * Processes a COPS delete message
387 * @param dMsg <tt>COPSDeleteMsg</tt> received from the PEP
388 * @throws COPSPdpException
390 protected void processDeleteRequestState(COPSDeleteMsg dMsg)
391 throws COPSPdpException {
392 if (_process != null)
393 _process.closeRequestState(this);