Merge "Changed tests to leverage a dynamic port for testing COPS message marshalling...
[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.stack.*;
10 import org.umu.cops.stack.COPSClientSI.CSIType;
11 import org.umu.cops.stack.COPSContext.RType;
12 import org.umu.cops.stack.COPSReason.ReasonCode;
13 import org.umu.cops.stack.COPSReportType.ReportType;
14
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
21 /**
22  * COPSPepMsgSender sends COPS messages to PDP.
23  *
24  * @version COPSPepMsgSender.java, v 2.00 2004
25  *
26  */
27 public class COPSPepMsgSender {
28
29     /**
30      * Socket connection to PDP
31      */
32     protected Socket _sock;
33
34     /**
35      * The client-type identifies the policy client
36      */
37     protected short _clientType;
38
39     /**
40      * The client handle is used to uniquely identify a particular
41      * PEP's request for a client-type
42      */
43     protected COPSHandle _handle;
44
45     /**
46      * Create a COPSPepMsgSender
47      *
48      * @param clientType        client-type
49      * @param clientHandle      client handle
50      * @param sock              socket of PDP connection
51      */
52     public COPSPepMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
53         // COPS Handle
54         _handle = clientHandle;
55         _clientType = clientType;
56
57         _sock = sock;
58     }
59
60     /**
61      * Return client handle
62      *
63      * @return   a COPSHandle
64      *
65      */
66     public COPSHandle getClientHandle() {
67         return _handle;
68     }
69
70     /**
71      * Return client-type
72      *
73      * @return   a short
74      *
75      */
76     public int getClientType() {
77         return _clientType;
78     }
79
80     /**
81      * Send Request to PDP.
82      *   The PEP establishes a request state client handle for which the
83      *   remote PDP may maintain state.
84      *
85      * @param    clientSIs              a  Hashtable
86      *
87      * @throws   COPSPepException
88      *
89      */
90     public void sendRequest(final Map<String, String> clientSIs) throws COPSPepException {
91         final Set<COPSClientSI> clientSISet = new HashSet<>();
92         // Add the clientSIs
93         for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
94             //  (PRID)
95             final COPSPrID prid = new COPSPrID();
96             prid.setData(new COPSData(entry.getKey()));
97             clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
98
99             //  (EPD)
100             final COPSPrEPD epd = new COPSPrEPD();
101             epd.setData(new COPSData(entry.getValue()));
102             clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
103         }
104         final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0),
105                 null, null, null, clientSISet, null);
106
107         // Send message
108         try {
109             msg.writeData(_sock);
110         } catch (IOException e) {
111             throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
112         }
113     }
114
115     /**
116      * Send Fail Report to PDP.
117      *    The RPT message is used by the PEP to communicate to the PDP its
118      *    success or failure in carrying out the PDP's decision, or to report
119      *    an accounting related change in state.
120      *
121      * @throws   COPSPepException
122      *
123      */
124     public void sendFailReport(final Map<String, String> clientSIs) throws COPSPepException {
125         sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
126     }
127
128     /**
129      * Send Succes Report to PDP.
130      *    The RPT message is used by the PEP to communicate to the PDP its
131      *    success or failure in carrying out the PDP's decision, or to report
132      *    an accounting related change in state.
133      *
134      * @throws   COPSPepException
135      *
136      */
137     public void sendSuccessReport(final Map<String, String> clientSIs) throws COPSPepException {
138         sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
139     }
140
141     public void sendAcctReport(final Map<String, String> clientSIs) throws COPSPepException {
142         sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
143     }
144
145     private void sendReport(final Map<String, String> clientSIs, final COPSReportType reportType)
146             throws COPSPepException {
147         // Report SUCESS
148         for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
149             //  (PRID)
150             final COPSPrID prid = new COPSPrID();
151             prid.setData(new COPSData(entry.getKey()));
152
153             final COPSReportMsg pridMsg = new COPSReportMsg(_clientType, _handle, reportType,
154                     new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())), null);
155             try {
156                 pridMsg.writeData(_sock);
157             } catch (IOException e) {
158                 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
159             }
160
161             //  (EPD)
162             final COPSPrEPD epd = new COPSPrEPD();
163             epd.setData(new COPSData(entry.getValue()));
164             final COPSReportMsg epdMsg = new COPSReportMsg(_clientType, _handle, reportType,
165                     new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())), null);
166             try {
167                 pridMsg.writeData(_sock);
168             } catch (IOException e) {
169                 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
170             }
171         }
172     }
173
174     /**
175      * Send Sync State Complete to PDP.
176      *   The Synchronize State Complete is sent by the PEP to the PDP after
177      *   the PDP sends a synchronize state request to the PEP and the PEP has
178      *   finished synchronization.
179      *
180      * @throws   COPSPepException
181      *
182      */
183     public void sendSyncComplete() throws COPSPepException {
184         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
185         try {
186             msg.writeData(_sock);
187         } catch (IOException e) {
188             throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
189         }
190     }
191
192     /**
193      * Send Delete Request to PDP.
194      * When sent from the PEP this message indicates to the remote PDP that
195      * the state identified by the client handle is no longer
196      * available/relevant.
197      *
198      * @throws   COPSPepException
199      *
200      */
201     public void sendDeleteRequest() throws COPSPepException {
202         // *** TODO: send a real reason
203         final COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
204         final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
205         try {
206             msg.writeData(_sock);
207         } catch (IOException e) {
208             throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
209         }
210     }
211 }
212
213
214
215