711c4fe69636a4e8ba580f43231cc93c7ea336a7
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospdp / COPSPdpOSMsgSender.java
1 package org.umu.cops.ospdp;
2
3 import org.umu.cops.stack.*;
4 import org.umu.cops.stack.COPSContext.RType;
5 import org.umu.cops.stack.COPSDecision.Command;
6 import org.umu.cops.stack.COPSDecision.DecisionFlag;
7
8 import java.io.IOException;
9 import java.net.Socket;
10 import java.util.Enumeration;
11 import java.util.Vector;
12
13 /**
14  * COPS message transceiver class for outsourcing connections at the PDP side.
15  *
16  * TODO - change all references of Vector to List<>
17  */
18 public class COPSPdpOSMsgSender {
19     /**
20      * Socket connected to PEP
21      */
22     protected Socket _sock;
23
24     /**
25      * COPS client-type that identifies the policy client
26      */
27     protected short _clientType;
28
29     /**
30      * COPS client handle used to uniquely identify a particular
31      * PEP's request for a client-type
32      */
33     protected COPSHandle _handle;
34
35     /**
36      * Creates a COPSPepMsgSender
37      *
38      * @param clientType        COPS client-type
39      * @param clientHandle      Client handle
40      * @param sock              Socket to the PEP
41      */
42     public COPSPdpOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
43         // COPS Handle
44         _handle = clientHandle;
45         _clientType = clientType;
46
47         _sock = sock;
48     }
49
50     /**
51      * Gets the client handle
52      * @return   Client's <tt>COPSHandle</tt>
53      */
54     public COPSHandle getClientHandle() {
55         return _handle;
56     }
57
58     /**
59      * Gets the client-type
60      * @return   Client-type value
61      */
62     public short getClientType() {
63         return _clientType;
64     }
65
66     /**
67      * Sends a decision message which was requested by the PEP
68      * @param removeDecs    Decisions to be removed
69      * @param installDecs   Decisions to be installed
70      * @throws COPSPdpException
71      */
72     public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
73         sendDecision(removeDecs, installDecs, true);
74     }
75
76     /**
77      * Sends a decision message which was not requested by the PEP
78      * @param removeDecs    Decisions to be removed
79      * @param installDecs   Decisions to be installed
80      * @throws COPSPdpException
81      */
82     public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
83         sendDecision(removeDecs, installDecs, false);
84     }
85
86     /**
87      * Sends a decision message to the PEP
88      * @param removeDecs    Decisions to be removed
89      * @param installDecs   Decisions to be installed
90      * @param solicited     <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
91      * @throws COPSPdpException
92      */
93     public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {
94         // Common Header holding the same ClientType as the request
95         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
96
97         if (solicited)
98             hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED);
99
100         // Client Handle with the same clientHandle as the request
101         final COPSHandle handle = new COPSHandle(getClientHandle().getId());
102
103         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
104         try {
105             decisionMsg.add(hdr);
106             decisionMsg.add(handle);
107
108             // Decisions (no flags supplied)
109             //  <Context>
110             COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
111
112             // Remove Decisions
113             //  <Decision: Flags>
114             if (removeDecs.size() > 0) {
115                 COPSDecision rdec1 = new COPSDecision(Command.REMOVE);
116                 decisionMsg.addDecision(rdec1, cntxt);
117
118                 Enumeration removeDecsEnum = removeDecs.elements();
119                 while (removeDecsEnum.hasMoreElements())
120                     decisionMsg.addDecision((COPSDecision) removeDecsEnum.nextElement(), cntxt);
121             }
122
123             // Install Decisions
124             //  <Decision: Flags>
125             if (installDecs.size() > 0) {
126                 COPSDecision idec1 = new COPSDecision(Command.INSTALL);
127                 decisionMsg.addDecision(idec1, cntxt);
128
129                 Enumeration installDecsEnum = installDecs.elements();
130                 while (installDecsEnum.hasMoreElements())
131                     decisionMsg.addDecision((COPSDecision) installDecsEnum.nextElement(), cntxt);
132                 /**
133                 COPSIntegrity intr = new COPSIntegrity();
134                 intr.setKeyId(19);
135                 intr.setSeqNum(9);
136                 intr.setKeyDigest(new COPSData("KEY DIGEST"));
137                 decisionMsg.add(intr);
138                 /**/
139             }
140         } catch (COPSException e) {
141             e.printStackTrace();
142             throw new COPSPdpException("Error making Msg");
143         }
144
145         //** Send decision
146         //**
147         try {
148             decisionMsg.writeData(_sock);
149         } catch (IOException e) {
150             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
151         }
152     }
153
154     /**FIXME: unused?
155      * Sends a message asking that the request state be deleted
156      * @throws   COPSPdpException
157      */
158     public void sendDeleteRequestState() throws COPSPdpException {
159         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
160          *                          <Client Handle>
161          *                          *(<Decision>)
162          *                          [<Integrity>]
163          * <Decision> ::= <Context>
164          *                  <Decision: Flags>
165          * <Decision: Flags> ::= Remove Request-State
166          *
167         */
168
169         // Common Header with the same ClientType as the request (default UNSOLICITED)
170         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
171
172         // Client Handle with the same clientHandle as the request
173         final COPSHandle clienthandle = new COPSHandle(_handle.getId());
174
175         // Decisions
176         //  <Context>
177         COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
178         //  <Decision: Flags>
179         COPSDecision dec = new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE);
180         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
181         try {
182             decisionMsg.add(hdr);
183             decisionMsg.add(clienthandle);
184             decisionMsg.addDecision(dec, cntxt);
185         } catch (COPSException e) {
186             throw new COPSPdpException("Error making Msg");
187         }
188
189         try {
190             decisionMsg.writeData(_sock);
191         } catch (IOException e) {
192             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
193         }
194     }
195
196     /**
197      * Method sendOpenNewRequestState
198      *
199      * @throws   COPSPdpException
200      *
201      */
202     //FIXME: Unused?
203     public void sendOpenNewRequestState() throws COPSPdpException {
204         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
205          *                          <Client Handle>
206          *                          *(<Decision>)
207          *                          [<Integrity>]
208          * <Decision> ::= <Context>
209          *                  <Decision: Flags>
210          * <Decision: Flags> ::= Install Request-State
211          *
212         */
213
214         // Common Header with the same ClientType as the request (default UNSOLICITED)
215         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
216
217         // Client Handle with the same clientHandle as the request
218         final COPSHandle clienthandle = new COPSHandle(_handle.getId());
219
220         // Decisions
221         //  <Context>
222         COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
223         //  <Decision: Flags>
224         COPSDecision dec = new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE);
225         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
226         try {
227             decisionMsg.add(hdr);
228             decisionMsg.add(clienthandle);
229             decisionMsg.addDecision(dec, cntxt);
230         } catch (COPSException e) {
231             throw new COPSPdpException("Error making Msg");
232         }
233
234         try {
235             decisionMsg.writeData(_sock);
236         } catch (IOException e) {
237             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
238         }
239     }
240
241     /**
242      * Sends a message asking for a COPS sync operation
243      * @throws COPSPdpException
244      */
245     public void sendSyncRequestState()
246     throws COPSPdpException {
247         /* <Synchronize State Request>  ::= <Common Header>
248          *                                  [<Client Handle>]
249          *                                  [<Integrity>]
250          */
251
252         // Common Header with the same ClientType as the request
253         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());
254
255         // Client Handle with the same clientHandle as the request
256         final COPSHandle clienthandle = new COPSHandle(_handle.getId());
257
258         COPSSyncStateMsg msg = new COPSSyncStateMsg();
259         try {
260             msg.add(hdr);
261             msg.add(clienthandle);
262         } catch (Exception e) {
263             throw new COPSPdpException("Error making Msg");
264         }
265
266         try {
267             msg.writeData(_sock);
268         } catch (IOException e) {
269             throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());
270         }
271     }
272
273 }