0361c7bda5de89e4f3e274d08bd40a206f82fc5f
[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 _process;
27
28     /**
29         COPS message transceiver used to send COPS messages
30      */
31     protected transient COPSPepOSMsgSender _sender;
32
33     /**
34      * Sync state
35      */
36     protected transient boolean _syncState;
37
38     /**
39      * Creates a state request manager
40      * @param    clientType Client-type
41      * @param   clientHandle    Client's <tt>COPSHandle</tt>
42      */
43     public COPSPepOSReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPepOSDataProcess process,
44                                 final Collection<COPSClientSI> clientSIs) {
45         super(clientType, clientHandle, process);
46         this._process = process;
47         this._clientSIs = new HashSet<>(clientSIs);
48         _syncState = true;
49     }
50
51     @Override
52     protected void initRequestState(final Socket sock) throws COPSException {
53         // Inits an object for sending COPS messages to the PDP
54         _sender = new COPSPepOSMsgSender(_clientType, _handle, sock);
55
56         // If an object exists for retrieving the PEP features,
57         // use it for retrieving them.
58         /*      Hashtable clientSIs;
59                 if (_process != null)
60                     clientSIs = _process.getClientData(this);
61                 else
62                     clientSIs = null;*/
63
64         // Semd the request
65         _sender.sendRequest(_clientSIs);
66
67         // Initial state
68         _status = Status.ST_INIT;
69     }
70
71     /**
72      * Deletes the request state
73      * @throws COPSPepException
74      */
75     protected void finalizeRequestState() throws COPSException {
76         _sender.sendDeleteRequest();
77         _status = Status.ST_FINAL;
78     }
79
80     /**
81      * Processes the decision message
82      * @param    dMsg Decision message from the PDP
83      * @throws   COPSPepException
84      */
85     protected void processDecision(final COPSDecisionMsg dMsg) throws COPSException {
86         //** Applies decisions to the configuration
87         //_process.setDecisions(this, removeDecs, installDecs, errorDecs);
88         // second param changed to dMsg so that the data processor
89         // can check the 'solicited' flag
90         final boolean isFailReport = _process.setDecisions(this, dMsg);
91         _status = Status.ST_DECS;
92
93         if (isFailReport) {
94             logger.info("Sending FAIL Report");
95             _sender.sendFailReport(_process.getReportData(this));
96         } else {
97             logger.info("Sending SUCCESS Report");
98             _sender.sendSuccessReport(_process.getReportData(this));
99         }
100         _status = Status.ST_REPORT;
101
102         if (!_syncState) {
103             _sender.sendSyncComplete();
104             _syncState = true;
105             _status = Status.ST_SYNCALL;
106         }
107     }
108
109
110     /**
111      * Processes a COPS delete message
112      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PDP
113      * @throws COPSPepException
114      */
115     protected void processDeleteRequestState(final COPSDecisionMsg dMsg) throws COPSPepException {
116         if (_process != null)
117             _process.closeRequestState(this);
118
119         _status = Status.ST_DEL;
120     }
121
122     /**
123      * Processes the message SycnStateRequest.
124      * The message SycnStateRequest indicates that the remote PDP
125      * wishes the client (which appears in the common header)
126      * to re-send its state.
127      *
128      * @param    ssMsg               The sync request from the PDP
129      *
130      * @throws   COPSPepException
131      *
132      */
133     protected void processSyncStateRequest(final COPSSyncStateMsg ssMsg) throws COPSException {
134         _syncState = false;
135         // If an object exists for retrieving the PEP features,
136         // use it for retrieving them.
137
138         // Send the request
139         _sender.sendRequest(_clientSIs);
140
141         _status = Status.ST_SYNC;
142     }
143
144     /**
145      * Called when connection is closed
146      * @param error Reason
147      * @throws COPSPepException
148      */
149     protected void processClosedConnection(final COPSError error) throws COPSPepException {
150         if (_process != null)
151             _process.notifyClosedConnection(this, error);
152
153         _status = Status.ST_CCONN;
154     }
155
156     /**
157      * Called when no keep-alive is received
158      * @throws COPSPepException
159      */
160     protected void processNoKAConnection() throws COPSPepException {
161         if (_process != null)
162             _process.notifyNoKAliveReceived(this);
163
164         _status = Status.ST_NOKA;
165     }
166
167     /**
168      * Processes the accounting report
169      * @throws COPSPepException
170      */
171     protected void processAcctReport() throws COPSPepException {
172         final List<COPSClientSI> report;
173         if (_process != null) report = new ArrayList<>(_process.getAcctData(this));
174         else report = new ArrayList<>();
175
176         _sender.sendAcctReport(report);
177
178         _status = Status.ST_ACCT;
179     }
180 }