Created a client-type enumeration as part of the original COPS refactor and have...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospep / COPSPepOSMsgSender.java
1 package org.umu.cops.ospep;
2
3 import org.umu.cops.stack.*;
4 import org.umu.cops.stack.COPSContext.RType;
5 import org.umu.cops.stack.COPSReason.ReasonCode;
6 import org.umu.cops.stack.COPSReportType.ReportType;
7
8 import java.io.IOException;
9 import java.net.Socket;
10 import java.util.List;
11 import java.util.Set;
12
13 /**
14  * COPS message transceiver class for outsourcing connections at the PEP side.
15  */
16 public class COPSPepOSMsgSender {
17     /**
18      * Socket connection to PDP
19      */
20     protected Socket _sock;
21
22     /**
23      * COPS client-type that identifies the policy client
24      */
25     protected short _clientType;
26
27     /**
28      * COPS client handle used to uniquely identify a particular
29      * PEP's request for a client-type
30      */
31     protected COPSHandle _handle;
32
33     /**
34      * Creates a COPSPepMsgSender
35      *
36      * @param clientType        Client-type
37      * @param clientHandle      Client handle
38      * @param sock              Socket connected to the PDP
39      */
40     public COPSPepOSMsgSender (final short clientType, final COPSHandle clientHandle, final Socket sock) {
41         // COPS Handle
42         _handle = clientHandle;
43         _clientType = clientType;
44
45         _sock = sock;
46     }
47
48     /**
49      * Gets the client handle
50      * @return  Client's <tt>COPSHandle</tt>
51      */
52     public COPSHandle getClientHandle() {
53         return _handle;
54     }
55
56     /**
57      * Gets the client-type
58      * @return  Client-type value
59      */
60     public int getClientType() {
61         return _clientType;
62     }
63
64     /**
65      * Sends a request to the PDP.
66      * The PEP establishes a request state client handle for which the
67      * remote PDP may maintain state.
68      * @param    clientSIs              Client data
69      * @throws   COPSPepException
70      */
71     public void sendRequest(final Set<COPSClientSI> clientSIs) throws COPSPepException {
72         // Create COPS Message
73         final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0), null,
74                 null, null, clientSIs, null);
75
76         // Send message
77         try {
78             msg.writeData(_sock);
79         } catch (IOException e) {
80             throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
81         }
82     }
83
84     /**
85      * Sends a failure report to the PDP. This report message notifies the PDP
86      * of failure when carrying out the PDP's decision, or when reporting
87      *  an accounting related state change.
88      * @param clientSIs Report data
89      * @throws   COPSPepException
90      */
91     public void sendFailReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
92         sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
93     }
94
95     /**
96      * Sends a success report to the PDP. This report message notifies the PDP
97      * of success when carrying out the PDP's decision, or when reporting
98      *  an accounting related state change.
99      * @param   clientSIs   Report data
100      * @throws  COPSPepException
101      */
102     public void sendSuccessReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
103         sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
104     }
105
106     /**
107      * Sends an accounting report to the PDP
108      * @param clientSIs Report data
109      * @throws COPSPepException
110      */
111     public void sendAcctReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
112         sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
113     }
114
115     private void sendReport(final List<COPSClientSI> clientSIs, final COPSReportType type) throws COPSPepException {
116         // Change back to old way if it is ultimately determined that a report may contain more than one COPSClientSI
117         final COPSReportMsg msg = new COPSReportMsg(_clientType, _handle, type, null, null);
118         try {
119             msg.writeData(_sock);
120         } catch (IOException e) {
121             throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
122         }
123     }
124
125     /**
126      * Sends a sync-complete message to the PDP. This indicates the
127      * end of a synchronization requested by the PDP.
128      * @throws   COPSPepException
129      */
130     public void sendSyncComplete() throws COPSPepException {
131         // Common Header with the same ClientType as the request
132         // Client Handle with the same clientHandle as the request
133         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
134         try {
135             msg.writeData(_sock);
136         } catch (IOException e) {
137             throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
138         }
139     }
140
141     /**
142      * Sends a delete request to the PDP.
143      * When sent from the PEP this message indicates to the remote PDP that
144      * the state identified by the client handle is no longer
145      * available/relevant.
146      * @throws   COPSPepException
147      */
148     public void sendDeleteRequest() throws COPSPepException {
149         // *** TODO: use real reason codes
150         COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
151
152         final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
153         try {
154             msg.writeData(_sock);
155         } catch (IOException e) {
156             throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
157         }
158     }
159
160 }