2 * Copyright (c) 2004 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.prpdp;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.COPSStateMan;
12 import org.umu.cops.stack.*;
14 import java.net.Socket;
15 import java.util.HashMap;
19 * State manager class for provisioning requests, at the PDP side.
21 public class COPSPdpReqStateMan extends COPSStateMan {
23 private final static Logger logger = LoggerFactory.getLogger(COPSPdpReqStateMan.class);
26 * Object for performing policy data processing
28 protected final COPSPdpDataProcess _process;
30 /** COPS message transceiver used to send COPS messages */
31 protected transient COPSPdpMsgSender _sender;
34 * Creates a request state manager
35 * @param clientType Client-type
36 * @param clientHandle Client handle
38 public COPSPdpReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPdpDataProcess process) {
39 super(clientType, clientHandle);
40 this._process = process;
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);
49 _status = Status.ST_INIT;
53 * Processes a COPS request
54 * @param msg COPS request received from the PEP
55 * @throws COPSPdpException
57 protected void processRequest(final COPSReqMsg msg) throws COPSPdpException {
59 // TODO - Implement me
60 // COPSHeader hdrmsg = msg.getHeader();
61 // COPSHandle handlemsg = msg.getClientHandle();
62 // COPSContext contextmsg = msg.getContext();
64 //** Analyze the request
67 /* <Request> ::= <Common Header>
72 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
74 * Very important, this is actually being treated like this:
75 * <Named ClientSI> ::= <PRID> | <EPD>
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();
85 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
86 switch (obj.getSNum())
88 case COPSPrObjBase.PR_PRID:
89 strobjprid = obj.getData().str();
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());
101 //** Here we must retrieve a decision depending on
102 //** the supplied ClientSIs
103 // reqSIs is a hashtable with the prid and epds
107 Hashtable removeDecs = new Hashtable();
108 Hashtable installDecs = new Hashtable();
109 _process.setClientData(this, reqSIs);
111 removeDecs = _process.getRemovePolicy(this);
112 installDecs = _process.getInstallPolicy(this);
114 //** We create the SOLICITED decision
116 _sender.sendDecision(removeDecs, installDecs);
123 * @param msg Report message from the PEP
124 * @throws COPSPdpException
126 protected void processReport(final COPSReportMsg msg) throws COPSPdpException {
128 //** Analyze the report
132 * <Report State> ::= <Common Header>
135 * *(<Named ClientSI>)
137 * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
138 * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
140 * Important, <Named ClientSI> is not parsed
143 // COPSHeader hdrmsg = msg.getHeader();
144 // COPSHandle handlemsg = msg.getClientHandle();
146 if (msg.getClientSI() != null) {
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();
156 case COPSPrObjBase.PR_EPD:
157 repSIs.put(strobjprid, obj.getData().str());
158 logger.info("PRID: " + strobjprid);
159 logger.info("EPD: " + obj.getData().str());
165 //** Here we must act in accordance with
166 //** the report received
167 switch (rtypemsg.getReportType()) {
169 _status = Status.ST_REPORT;
170 _process.successReport(this, repSIs);
173 _status = Status.ST_REPORT;
174 _process.failReport(this, repSIs);
177 _status = Status.ST_ACCT;
178 _process.acctReport(this, repSIs);
187 * Called when connection is closed
188 * @param error Reason
189 * @throws COPSPdpException
191 protected void processClosedConnection(final COPSError error) throws COPSPdpException {
192 if (_process != null)
193 _process.notifyClosedConnection(this, error);
195 _status = Status.ST_CCONN;
199 * Called when no keep-alive is received
200 * @throws COPSPdpException
202 protected void processNoKAConnection() throws COPSPdpException {
203 if (_process != null)
204 _process.notifyNoKAliveReceived(this);
206 _status = Status.ST_NOKA;
210 * Deletes the request state
211 * @throws COPSPdpException
213 protected void finalizeRequestState() throws COPSException {
214 _sender.sendDeleteRequestState();
215 _status = Status.ST_FINAL;
219 * Asks for a COPS sync
220 * @throws COPSPdpException
222 protected void syncRequestState() throws COPSException {
223 _sender.sendSyncRequestState();
224 _status = Status.ST_SYNC;
228 * Opens a new request state
229 * @throws COPSPdpException
231 protected void openNewRequestState() throws COPSException {
232 _sender.sendOpenNewRequestState();
233 _status = Status.ST_NEW;
237 * Processes a COPS delete message
238 * @param dMsg <tt>COPSDeleteMsg</tt> received from the PEP
239 * @throws COPSPdpException
241 protected void processDeleteRequestState(final COPSDeleteMsg dMsg) throws COPSPdpException {
242 if (_process != null)
243 _process.closeRequestState(this);
245 _status = Status.ST_DEL;