Created abstract super class for all COPS Message Senders as each contained duplicate...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpep / COPSPepMsgSender.java
1 /*
2  * Copyright (c) 2004 University of Murcia.  All rights reserved.
3  * --------------------------------------------------------------
4  * For more information, please see <http://www.umu.euro6ix.org/>.
5  */
6
7 package org.umu.cops.prpep;
8
9 import org.umu.cops.COPSMsgSender;
10 import org.umu.cops.stack.*;
11 import org.umu.cops.stack.COPSClientSI.CSIType;
12 import org.umu.cops.stack.COPSContext.RType;
13 import org.umu.cops.stack.COPSReason.ReasonCode;
14 import org.umu.cops.stack.COPSReportType.ReportType;
15
16 import java.io.IOException;
17 import java.net.Socket;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21
22 /**
23  * COPSPepMsgSender sends COPS messages to PDP.
24  *
25  * @version COPSPepMsgSender.java, v 2.00 2004
26  *
27  */
28 public class COPSPepMsgSender extends COPSMsgSender {
29
30     /**
31      * Create a COPSPepMsgSender
32      *
33      * @param clientType        client-type
34      * @param clientHandle      client handle
35      * @param sock              socket of PDP connection
36      */
37     public COPSPepMsgSender(short clientType, COPSHandle clientHandle, Socket sock) {
38         super(clientType, clientHandle, sock);
39     }
40
41     /**
42      * Send Request to PDP.
43      *   The PEP establishes a request state client handle for which the
44      *   remote PDP may maintain state.
45      *
46      * @param    clientSIs              a  Hashtable
47      *
48      * @throws   COPSPepException
49      *
50      */
51     public void sendRequest(final Map<String, String> clientSIs) throws COPSPepException {
52         final Set<COPSClientSI> clientSISet = new HashSet<>();
53         // Add the clientSIs
54         for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
55             //  (PRID)
56             final COPSPrID prid = new COPSPrID();
57             prid.setData(new COPSData(entry.getKey()));
58             clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
59
60             //  (EPD)
61             final COPSPrEPD epd = new COPSPrEPD();
62             epd.setData(new COPSData(entry.getValue()));
63             clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
64         }
65         final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0),
66                 null, null, null, clientSISet, null);
67
68         // Send message
69         try {
70             msg.writeData(_sock);
71         } catch (IOException e) {
72             throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
73         }
74     }
75
76     /**
77      * Send Fail Report to PDP.
78      *    The RPT message is used by the PEP to communicate to the PDP its
79      *    success or failure in carrying out the PDP's decision, or to report
80      *    an accounting related change in state.
81      *
82      * @throws   COPSPepException
83      *
84      */
85     public void sendFailReport(final Map<String, String> clientSIs) throws COPSPepException {
86         sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
87     }
88
89     /**
90      * Send Succes Report to PDP.
91      *    The RPT message is used by the PEP to communicate to the PDP its
92      *    success or failure in carrying out the PDP's decision, or to report
93      *    an accounting related change in state.
94      *
95      * @throws   COPSPepException
96      *
97      */
98     public void sendSuccessReport(final Map<String, String> clientSIs) throws COPSPepException {
99         sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
100     }
101
102     public void sendAcctReport(final Map<String, String> clientSIs) throws COPSPepException {
103         sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
104     }
105
106     private void sendReport(final Map<String, String> clientSIs, final COPSReportType reportType)
107             throws COPSPepException {
108         // Report SUCESS
109         for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
110             //  (PRID)
111             final COPSPrID prid = new COPSPrID();
112             prid.setData(new COPSData(entry.getKey()));
113
114             final COPSReportMsg pridMsg = new COPSReportMsg(_clientType, _handle, reportType,
115                     new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())), null);
116             try {
117                 pridMsg.writeData(_sock);
118             } catch (IOException e) {
119                 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
120             }
121
122             //  (EPD)
123             final COPSPrEPD epd = new COPSPrEPD();
124             epd.setData(new COPSData(entry.getValue()));
125             final COPSReportMsg epdMsg = new COPSReportMsg(_clientType, _handle, reportType,
126                     new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())), null);
127             try {
128                 pridMsg.writeData(_sock);
129             } catch (IOException e) {
130                 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
131             }
132         }
133     }
134
135     /**
136      * Send Sync State Complete to PDP.
137      *   The Synchronize State Complete is sent by the PEP to the PDP after
138      *   the PDP sends a synchronize state request to the PEP and the PEP has
139      *   finished synchronization.
140      *
141      * @throws   COPSPepException
142      *
143      */
144     public void sendSyncComplete() throws COPSPepException {
145         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
146         try {
147             msg.writeData(_sock);
148         } catch (IOException e) {
149             throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
150         }
151     }
152
153     /**
154      * Send Delete Request to PDP.
155      * When sent from the PEP this message indicates to the remote PDP that
156      * the state identified by the client handle is no longer
157      * available/relevant.
158      *
159      * @throws   COPSPepException
160      *
161      */
162     public void sendDeleteRequest() throws COPSPepException {
163         // *** TODO: send a real reason
164         final COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
165         final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
166         try {
167             msg.writeData(_sock);
168         } catch (IOException e) {
169             throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
170         }
171     }
172 }
173
174
175
176