79470ff80f3bcbe0af86327a5eebecc4519a28e3
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / PCMMPdpReqStateMan.java
1 /**
2  @header@
3  */
4
5 package org.pcmm;
6
7 import org.pcmm.gates.IGateID;
8 import org.pcmm.gates.IPCMMGate;
9 import org.pcmm.gates.ITransactionID;
10 import org.pcmm.gates.impl.PCMMError;
11 import org.pcmm.gates.impl.PCMMGateReq;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.umu.cops.prpdp.COPSPdpException;
15 import org.umu.cops.prpdp.COPSPdpReqStateMan;
16 import org.umu.cops.stack.*;
17 import org.umu.cops.stack.COPSReportType.ReportType;
18
19 import java.net.Socket;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /**
25  * State manager class for provisioning requests, at the PDP side.
26  */
27 public class PCMMPdpReqStateMan extends COPSPdpReqStateMan {
28
29     private final static Logger logger = LoggerFactory.getLogger(PCMMPdpReqStateMan.class);
30
31     /**
32      * Object for performing policy data processing
33      */
34     protected final PCMMPdpDataProcess _thisProcess;
35
36     /** COPS message transceiver used to send COPS messages */
37     protected final PCMMPdpMsgSender _sender;
38
39     /**
40      * Creates a request state manager
41      * @param clientType    Client-type
42      * @param clientHandle  Client handle
43      */
44     // TODO - consider sending in the COPSHandle object instead
45     public PCMMPdpReqStateMan(final short clientType, final COPSHandle clientHandle, final PCMMPdpDataProcess process,
46                               final Socket socket) {
47         super(clientType, clientHandle, process, socket);
48         this._thisProcess = process;
49         _sender = new PCMMPdpMsgSender(_clientType, _handle, _socket);
50         // Initial state
51         _status = Status.ST_INIT;
52     }
53
54     @Override
55     public void processRequest(final COPSReqMsg msg) throws COPSPdpException {
56         // TODO - Implement me - see commented out code from history prior to May 4, 2015...
57     }
58
59      // TODO - break apart this method
60     @Override
61     protected void processReport(final COPSReportMsg msg) throws COPSPdpException {
62         // Report Type
63         final COPSReportType rtypemsg = msg.getReport();
64
65         if (msg.getClientSI() != null) {
66             final COPSClientSI clientSI = msg.getClientSI();
67             // Named ClientSI
68             final byte[] data = Arrays.copyOfRange(clientSI.getData().getData(), 0, clientSI.getData().getData().length);
69
70             // PCMMUtils.WriteBinaryDump("COPSReportClientSI", data);
71             logger.info("PCMMGateReq Parse Gate Message");
72             final PCMMGateReq gateMsg = new PCMMGateReq(data);
73
74             // TODO FIXME - Why is this Map being filled but never used???
75             final Map<String, String> repSIs = new HashMap<>();
76             String strobjprid = "";
77             final COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
78             switch (obj.getSNum()) {
79                 case COPSPrObjBase.PR_PRID:
80                     logger.info("COPSPrObjBase.PR_PRID");
81                     // TODO FIXME - this value is never used
82                     strobjprid = obj.getData().str();
83                     break;
84                 case COPSPrObjBase.PR_EPD:
85                     logger.info("COPSPrObjBase.PR_EPD");
86                     // TODO FIXME - strobjprid is always empty
87                     repSIs.put(strobjprid, obj.getData().str());
88                     logger.info("PRID: " + strobjprid);
89                     logger.info("EPD: " + obj.getData().str());
90                     break;
91                 default:
92                     logger.error("Object s-num: " + obj.getSNum() + "stype " + obj.getSType());
93                     logger.error("PRID: " + strobjprid);
94                     logger.error("EPD: " + obj.getData().str());
95                     break;
96             }
97
98             logger.info("rtypemsg process");
99             //** Here we must act in accordance with
100             //** the report received
101
102             // retrieve and remove the transactionId to gate request map entry
103             // see PCMMPdpMsgSender.sendGateSet(IPCMMGate gate)
104             final ITransactionID trID = gateMsg.getTransactionID();
105             final Short trIDnum = trID.getTransactionIdentifier();
106
107             logger.info("Removing gate from cache with key - " + trIDnum);
108             final IPCMMGate gate = PCMMGlobalConfig.transactionGateMap.remove(trIDnum);
109             if (gate != null) {
110                 // capture the "error" message if any
111                 gate.setError(gateMsg.getError());
112                 logger.info("Setting error on gate - " + gateMsg.getError());
113             }else {
114                 logger.error("processReport(): gateReq not found for transactionID {}", trIDnum);
115                 return;
116             }
117
118             if (rtypemsg.getReportType().equals(ReportType.SUCCESS)) {
119                 logger.info("rtypemsg success");
120                 _status = Status.ST_REPORT;
121                 final IGateID gateID = gateMsg.getGateID();
122                 logger.info("Setting gate ID on gate object - " + gateID);
123                 gate.setGateID(gateID);
124                 if (_thisProcess != null)
125                     _thisProcess.successReport(this, gateMsg);
126             } else {
127                 final String cmdType;
128                 if ( trID.getGateCommandType() == ITransactionID.GateDeleteAck ) {
129                     cmdType = "GateDeleteAck";
130                 } else if ( trID.getGateCommandType() == ITransactionID.GateSetAck ) {
131                     cmdType = "GateSetAck";
132                 } else cmdType = null;
133                 // capture the gateId from the response message
134                 final IGateID gateID = gateMsg.getGateID();
135                 logger.info("Setting gate ID on gate object - " + gateID);
136                 gate.setGateID(gateID);
137
138                 if (gateID != null) {
139                     int gateIdInt = gateID.getGateID();
140                     String gateIdHex = String.format("%08x", gateIdInt);
141                     logger.info(getClass().getName() + ": " + cmdType + ": GateID = " + gateIdHex);
142                 } else {
143                     logger.warn("Gate ID is null");
144                 }
145             }
146             if (rtypemsg.getReportType().equals(ReportType.FAILURE)) {
147                 logger.info("rtypemsg failure");
148                 _status = Status.ST_REPORT;
149                 if (_thisProcess != null)
150                     _thisProcess.failReport(this, gateMsg);
151                 else
152                     if (gateMsg.getError() != null)
153                         logger.info("Gate message error - " + gateMsg.getError().toString());
154                     else {
155                         final PCMMError error = new PCMMError();
156                         // TODO - figure out correct error code
157                         error.setErrorCode((short)19);
158                         gate.setError(error);
159                         logger.warn("Gate request failed without an error, setting one - " + error);
160                     }
161             } else if (rtypemsg.getReportType().equals(ReportType.ACCOUNTING)) {
162                     logger.info("rtypemsg account");
163                     _status = Status.ST_ACCT;
164                     if (_thisProcess != null)
165                         _thisProcess.acctReport(this, gateMsg);
166             }
167
168             // let the waiting gateSet/gateDelete sender proceed
169             // TODO - see PCMMService#processReport() gate.notify(). Should determine a better means to
170             // TODO - handle this synchronization.
171             logger.info("Notify gate request has been updated with ID - " + gate.getGateID());
172             synchronized(gate) {
173                 gate.notify();
174             }
175             logger.info("Out processReport");
176         }
177     }
178
179 }