Created abstract super class for all COPS Message Senders as each contained duplicate...
[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.COPSMsgSender;
4 import org.umu.cops.stack.*;
5 import org.umu.cops.stack.COPSContext.RType;
6 import org.umu.cops.stack.COPSReason.ReasonCode;
7 import org.umu.cops.stack.COPSReportType.ReportType;
8
9 import java.io.IOException;
10 import java.net.Socket;
11 import java.util.List;
12 import java.util.Set;
13
14 /**
15  * COPS message transceiver class for outsourcing connections at the PEP side.
16  */
17 public class COPSPepOSMsgSender extends COPSMsgSender {
18
19     /**
20      * Creates a COPSPepMsgSender
21      *
22      * @param clientType        Client-type
23      * @param clientHandle      Client handle
24      * @param sock              Socket connected to the PDP
25      */
26     public COPSPepOSMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
27         super(clientType, clientHandle, sock);
28     }
29
30     /**
31      * Sends a request to the PDP.
32      * The PEP establishes a request state client handle for which the
33      * remote PDP may maintain state.
34      * @param    clientSIs              Client data
35      * @throws   COPSPepException
36      */
37     public void sendRequest(final Set<COPSClientSI> clientSIs) throws COPSPepException {
38         // Create COPS Message
39         final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0), null,
40                 null, null, clientSIs, null);
41
42         // Send message
43         try {
44             msg.writeData(_sock);
45         } catch (IOException e) {
46             throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
47         }
48     }
49
50     /**
51      * Sends a failure report to the PDP. This report message notifies the PDP
52      * of failure when carrying out the PDP's decision, or when reporting
53      *  an accounting related state change.
54      * @param clientSIs Report data
55      * @throws   COPSPepException
56      */
57     public void sendFailReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
58         sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
59     }
60
61     /**
62      * Sends a success report to the PDP. This report message notifies the PDP
63      * of success when carrying out the PDP's decision, or when reporting
64      *  an accounting related state change.
65      * @param   clientSIs   Report data
66      * @throws  COPSPepException
67      */
68     public void sendSuccessReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
69         sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
70     }
71
72     /**
73      * Sends an accounting report to the PDP
74      * @param clientSIs Report data
75      * @throws COPSPepException
76      */
77     public void sendAcctReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
78         sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
79     }
80
81     private void sendReport(final List<COPSClientSI> clientSIs, final COPSReportType type) throws COPSPepException {
82         // Change back to old way if it is ultimately determined that a report may contain more than one COPSClientSI
83         final COPSReportMsg msg = new COPSReportMsg(_clientType, _handle, type, null, null);
84         try {
85             msg.writeData(_sock);
86         } catch (IOException e) {
87             throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
88         }
89     }
90
91     /**
92      * Sends a sync-complete message to the PDP. This indicates the
93      * end of a synchronization requested by the PDP.
94      * @throws   COPSPepException
95      */
96     public void sendSyncComplete() throws COPSPepException {
97         // Common Header with the same ClientType as the request
98         // Client Handle with the same clientHandle as the request
99         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
100         try {
101             msg.writeData(_sock);
102         } catch (IOException e) {
103             throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
104         }
105     }
106
107     /**
108      * Sends a delete request to the PDP.
109      * When sent from the PEP this message indicates to the remote PDP that
110      * the state identified by the client handle is no longer
111      * available/relevant.
112      * @throws   COPSPepException
113      */
114     public void sendDeleteRequest() throws COPSPepException {
115         // *** TODO: use real reason codes
116         COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
117
118         final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
119         try {
120             msg.writeData(_sock);
121         } catch (IOException e) {
122             throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
123         }
124     }
125
126 }