1 package org.umu.cops.ospdp;
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;
8 import java.io.IOException;
9 import java.net.Socket;
10 import java.util.Enumeration;
11 import java.util.Vector;
14 * COPS message transceiver class for outsourcing connections at the PDP side.
16 * TODO - change all references of Vector to List<>
18 public class COPSPdpOSMsgSender {
20 * Socket connected to PEP
22 protected Socket _sock;
25 * COPS client-type that identifies the policy client
27 protected short _clientType;
30 * COPS client handle used to uniquely identify a particular
31 * PEP's request for a client-type
33 protected COPSHandle _handle;
36 * Creates a COPSPepMsgSender
38 * @param clientType COPS client-type
39 * @param clientHandle Client handle
40 * @param sock Socket to the PEP
42 public COPSPdpOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
44 _handle = clientHandle;
45 _clientType = clientType;
51 * Gets the client handle
52 * @return Client's <tt>COPSHandle</tt>
54 public COPSHandle getClientHandle() {
59 * Gets the client-type
60 * @return Client-type value
62 public short getClientType() {
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
72 public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
73 sendDecision(removeDecs, installDecs, true);
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
82 public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
83 sendDecision(removeDecs, installDecs, false);
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
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());
98 hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED);
100 // Client Handle with the same clientHandle as the request
101 final COPSHandle handle = new COPSHandle(getClientHandle().getId());
103 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
105 decisionMsg.add(hdr);
106 decisionMsg.add(handle);
108 // Decisions (no flags supplied)
110 COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
114 if (removeDecs.size() > 0) {
115 COPSDecision rdec1 = new COPSDecision(Command.REMOVE);
116 decisionMsg.addDecision(rdec1, cntxt);
118 Enumeration removeDecsEnum = removeDecs.elements();
119 while (removeDecsEnum.hasMoreElements())
120 decisionMsg.addDecision((COPSDecision) removeDecsEnum.nextElement(), cntxt);
125 if (installDecs.size() > 0) {
126 COPSDecision idec1 = new COPSDecision(Command.INSTALL);
127 decisionMsg.addDecision(idec1, cntxt);
129 Enumeration installDecsEnum = installDecs.elements();
130 while (installDecsEnum.hasMoreElements())
131 decisionMsg.addDecision((COPSDecision) installDecsEnum.nextElement(), cntxt);
133 COPSIntegrity intr = new COPSIntegrity();
136 intr.setKeyDigest(new COPSData("KEY DIGEST"));
137 decisionMsg.add(intr);
140 } catch (COPSException e) {
142 throw new COPSPdpException("Error making Msg");
148 decisionMsg.writeData(_sock);
149 } catch (IOException e) {
150 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
155 * Sends a message asking that the request state be deleted
156 * @throws COPSPdpException
158 public void sendDeleteRequestState() throws COPSPdpException {
159 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
163 * <Decision> ::= <Context>
165 * <Decision: Flags> ::= Remove Request-State
169 // Common Header with the same ClientType as the request (default UNSOLICITED)
170 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
172 // Client Handle with the same clientHandle as the request
173 final COPSHandle clienthandle = new COPSHandle(_handle.getId());
177 COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
179 COPSDecision dec = new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE);
180 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
182 decisionMsg.add(hdr);
183 decisionMsg.add(clienthandle);
184 decisionMsg.addDecision(dec, cntxt);
185 } catch (COPSException e) {
186 throw new COPSPdpException("Error making Msg");
190 decisionMsg.writeData(_sock);
191 } catch (IOException e) {
192 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
197 * Method sendOpenNewRequestState
199 * @throws COPSPdpException
203 public void sendOpenNewRequestState() throws COPSPdpException {
204 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
208 * <Decision> ::= <Context>
210 * <Decision: Flags> ::= Install Request-State
214 // Common Header with the same ClientType as the request (default UNSOLICITED)
215 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
217 // Client Handle with the same clientHandle as the request
218 final COPSHandle clienthandle = new COPSHandle(_handle.getId());
222 COPSContext cntxt = new COPSContext(RType.CONFIG, (short) 0);
224 COPSDecision dec = new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE);
225 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
227 decisionMsg.add(hdr);
228 decisionMsg.add(clienthandle);
229 decisionMsg.addDecision(dec, cntxt);
230 } catch (COPSException e) {
231 throw new COPSPdpException("Error making Msg");
235 decisionMsg.writeData(_sock);
236 } catch (IOException e) {
237 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
242 * Sends a message asking for a COPS sync operation
243 * @throws COPSPdpException
245 public void sendSyncRequestState()
246 throws COPSPdpException {
247 /* <Synchronize State Request> ::= <Common Header>
252 // Common Header with the same ClientType as the request
253 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());
255 // Client Handle with the same clientHandle as the request
256 final COPSHandle clienthandle = new COPSHandle(_handle.getId());
258 COPSSyncStateMsg msg = new COPSSyncStateMsg();
261 msg.add(clienthandle);
262 } catch (Exception e) {
263 throw new COPSPdpException("Error making Msg");
267 msg.writeData(_sock);
268 } catch (IOException e) {
269 throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());