1 package org.umu.cops.ospdp;
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.COPSDecision.Command;
7 import org.umu.cops.stack.COPSDecision.DecisionFlag;
8 import org.umu.cops.stack.COPSHeader.Flag;
10 import java.io.IOException;
11 import java.net.Socket;
15 * COPS message transceiver class for outsourcing connections at the PDP side.
17 * TODO - change all references of Vector to List<>
19 public class COPSPdpOSMsgSender extends COPSMsgSender {
22 * Creates a COPSPepMsgSender
24 * @param clientType COPS client-type
25 * @param clientHandle Client handle
26 * @param sock Socket to the PEP
28 public COPSPdpOSMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
29 super(clientType, clientHandle, sock);
33 * Sends a decision message which was requested by the PEP
34 * @param removeDecs Decisions to be removed
35 * @param installDecs Decisions to be installed
36 * @throws COPSPdpException
38 public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
39 sendDecision(removeDecs, installDecs, true);
43 * Sends a decision message which was not requested by the PEP
44 * @param removeDecs Decisions to be removed
45 * @param installDecs Decisions to be installed
46 * @throws COPSPdpException
48 public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
49 sendDecision(removeDecs, installDecs, false);
53 * Sends a decision message to the PEP
54 * @param removeDecs Decisions to be removed
55 * @param installDecs Decisions to be installed
56 * @param solicited <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
57 * @throws COPSPdpException
59 public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {
60 // Common Header holding the same ClientType as the request
65 flag = Flag.UNSOLICITED;
67 final Map<COPSContext, Set<COPSDecision>> decisions = new HashMap<>();
69 // Decisions (no flags supplied)
71 final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
75 final COPSDecision rdec1;
76 if (installDecs.size() == 0)
77 rdec1 = new COPSDecision(Command.REMOVE);
79 rdec1 = new COPSDecision(Command.INSTALL);
81 if (decisions.get(cntxt) == null) {
82 final Set<COPSDecision> decisionSet = new HashSet<>();
83 decisionSet.add(rdec1);
84 decisions.put(cntxt, decisionSet);
86 decisions.get(cntxt).add(rdec1);
89 // Client Handle with the same clientHandle as the request
90 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(1, flag, getClientType(), _handle, decisions,
96 decisionMsg.writeData(_sock);
97 } catch (IOException e) {
98 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
103 * Sends a message asking that the request state be deleted
104 * @throws COPSPdpException
106 public void sendDeleteRequestState() throws COPSPdpException {
107 final Set<COPSDecision> decisionSet = new HashSet<>();
108 decisionSet.add(new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE));
109 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
110 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
112 // Common Header with the same ClientType as the request (default UNSOLICITED)
113 // Client Handle with the same clientHandle as the request
114 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
117 decisionMsg.writeData(_sock);
118 } catch (IOException e) {
119 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
124 * Method sendOpenNewRequestState
126 * @throws COPSPdpException
129 public void sendOpenNewRequestState() throws COPSPdpException {
130 final Set<COPSDecision> decisionSet = new HashSet<>();
131 decisionSet.add(new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE));
132 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
133 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
135 // Common Header with the same ClientType as the request (default UNSOLICITED)
136 // Client Handle with the same clientHandle as the request
137 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
140 decisionMsg.writeData(_sock);
141 } catch (IOException e) {
142 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
147 * Sends a message asking for a COPS sync operation
148 * @throws COPSPdpException
150 public void sendSyncRequestState() throws COPSPdpException {
151 // Client Handle with the same clientHandle as the request
152 final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
154 msg.writeData(_sock);
155 } catch (IOException e) {
156 throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());