Cleanup of state managers' interfaces (constructor and init).
[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         Object for performing policy data processing
20      */
21     protected final COPSPepOSDataProcess _thisProcess;
22
23     /**
24         COPS message transceiver used to send COPS messages
25      */
26     protected final COPSPepOSMsgSender _thisSender;
27
28     /**
29      * ClientSI data populated when initRequestState() is called.
30      */
31     protected transient Set<COPSClientSI> _clientSIs;
32
33     /**
34      * Constructor
35      * @param clientType - the PEP client type
36      * @param clientHandle - the client-handle
37      * @param process - the data processor
38      * @param clientSIs - the known client SI objects
39      * @param socket - the socket connection
40      */
41     public COPSPepOSReqStateMan(final short clientType, final COPSHandle clientHandle,
42                                 final COPSPepOSDataProcess process, final Collection<COPSClientSI> clientSIs,
43                                 final Socket socket) {
44         super(clientType, clientHandle, process, socket, new COPSPepOSMsgSender(clientType, clientHandle, socket));
45         this._thisProcess = process;
46         if (clientSIs == null)
47             this._clientSIs = new HashSet<>();
48         else this._clientSIs = new HashSet<>(clientSIs);
49
50         // Inits an object for sending COPS messages to the PDP
51         _thisSender = new COPSPepOSMsgSender(_clientType, _handle, _socket);
52     }
53
54     @Override
55     public void initRequestState() throws COPSException {
56         _clientSIs.addAll(_thisProcess.getClientData(this));
57         // Send the request
58         _thisSender.sendRequest(new HashSet<>(_clientSIs));
59
60         // Initial state
61         _status = Status.ST_INIT;
62     }
63
64     @Override
65     protected void processDecision(final COPSDecisionMsg dMsg) throws COPSException {
66         //** Applies decisions to the configuration
67         //_thisProcess.setDecisions(this, removeDecs, installDecs, errorDecs);
68         // second param changed to dMsg so that the data processor
69         // can check the 'solicited' flag
70         final boolean isFailReport = _thisProcess.setDecisions(this, dMsg);
71         _status = Status.ST_DECS;
72
73         if (isFailReport) {
74             logger.info("Sending FAIL Report");
75             _thisSender.sendFailReport(_thisProcess.getReportData(this));
76         } else {
77             logger.info("Sending SUCCESS Report");
78             _thisSender.sendSuccessReport(_thisProcess.getReportData(this));
79         }
80         _status = Status.ST_REPORT;
81
82         if (!_syncState) {
83             _thisSender.sendSyncComplete();
84             _syncState = true;
85             _status = Status.ST_SYNCALL;
86         }
87     }
88
89     @Override
90     protected void processSyncStateRequest(final COPSSyncStateMsg ssMsg) throws COPSException {
91         _syncState = false;
92         // If an object exists for retrieving the PEP features,
93         // use it for retrieving them.
94
95         // Send the request
96         _thisSender.sendRequest(_clientSIs);
97
98         _status = Status.ST_SYNC;
99     }
100
101     @Override
102     public void processAcctReport() throws COPSPepException {
103         final List<COPSClientSI> report;
104         if (_thisProcess != null) report = new ArrayList<>(_thisProcess.getAcctData(this));
105         else report = new ArrayList<>();
106
107         _thisSender.sendAcctReport(report);
108
109         _status = Status.ST_ACCT;
110     }
111 }