Completed COPS PEP request state manager refactor primarily to remove redundant code.
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospep / COPSPepOSReqStateMan.java
1 package org.umu.cops.ospep;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.umu.cops.prpep.COPSPepReqStateMan;
6 import org.umu.cops.stack.*;
7
8 import java.net.Socket;
9 import java.util.*;
10
11 /**
12  * State manager class for outsourcing requests, at the PEP side.
13  */
14 public class COPSPepOSReqStateMan extends COPSPepReqStateMan {
15
16     private final static Logger logger = LoggerFactory.getLogger(COPSPepOSReqStateMan.class);
17
18     /**
19      * ClientSI data from signaling.
20      */
21     protected final Set<COPSClientSI> _clientSIs;
22
23     /**
24         Object for performing policy data processing
25      */
26     protected final COPSPepOSDataProcess _thisProcess;
27
28     /**
29         COPS message transceiver used to send COPS messages
30      */
31     protected transient COPSPepOSMsgSender _thisSender;
32
33     /**
34      * Creates a state request manager
35      * @param    clientType Client-type
36      * @param   clientHandle    Client's <tt>COPSHandle</tt>
37      */
38     public COPSPepOSReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPepOSDataProcess process,
39                                 final Collection<COPSClientSI> clientSIs) {
40         super(clientType, clientHandle, process);
41         this._thisProcess = process;
42         this._clientSIs = new HashSet<>(clientSIs);
43     }
44
45     @Override
46     protected void initRequestState(final Socket sock) throws COPSException {
47         // Inits an object for sending COPS messages to the PDP
48         _thisSender = new COPSPepOSMsgSender(_clientType, _handle, sock);
49         _sender = _thisSender;
50
51         // Send the request
52         _thisSender.sendRequest(_clientSIs);
53
54         // Initial state
55         _status = Status.ST_INIT;
56     }
57
58     /**
59      * Processes the decision message
60      * @param    dMsg Decision message from the PDP
61      * @throws   COPSPepException
62      */
63     protected void processDecision(final COPSDecisionMsg dMsg) throws COPSException {
64         //** Applies decisions to the configuration
65         //_thisProcess.setDecisions(this, removeDecs, installDecs, errorDecs);
66         // second param changed to dMsg so that the data processor
67         // can check the 'solicited' flag
68         final boolean isFailReport = _thisProcess.setDecisions(this, dMsg);
69         _status = Status.ST_DECS;
70
71         if (isFailReport) {
72             logger.info("Sending FAIL Report");
73             _thisSender.sendFailReport(_thisProcess.getReportData(this));
74         } else {
75             logger.info("Sending SUCCESS Report");
76             _thisSender.sendSuccessReport(_thisProcess.getReportData(this));
77         }
78         _status = Status.ST_REPORT;
79
80         if (!_syncState) {
81             _sender.sendSyncComplete();
82             _syncState = true;
83             _status = Status.ST_SYNCALL;
84         }
85     }
86
87     @Override
88     protected void processSyncStateRequest(final COPSSyncStateMsg ssMsg) throws COPSException {
89         _syncState = false;
90         // If an object exists for retrieving the PEP features,
91         // use it for retrieving them.
92
93         // Send the request
94         _thisSender.sendRequest(_clientSIs);
95
96         _status = Status.ST_SYNC;
97     }
98
99     @Override
100     public void processAcctReport() throws COPSPepException {
101         final List<COPSClientSI> report;
102         if (_thisProcess != null) report = new ArrayList<>(_thisProcess.getAcctData(this));
103         else report = new ArrayList<>();
104
105         _thisSender.sendAcctReport(report);
106
107         _status = Status.ST_ACCT;
108     }
109 }