2 * Copyright (c) 2004 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.prpdp;
9 import org.umu.cops.COPSMsgSender;
10 import org.umu.cops.stack.*;
11 import org.umu.cops.stack.COPSContext.RType;
12 import org.umu.cops.stack.COPSDecision.Command;
13 import org.umu.cops.stack.COPSDecision.DecisionFlag;
14 import org.umu.cops.stack.COPSObjHeader.CType;
16 import java.io.IOException;
17 import java.net.Socket;
18 import java.util.HashMap;
19 import java.util.HashSet;
24 * COPS message transceiver class for provisioning connections at the PDP side.
26 * TODO - Need to continue refactoring by removing all instances of Hashtable (change to Map<>)
28 public class COPSPdpMsgSender extends COPSMsgSender {
31 * Creates a COPSPepMsgSender
33 * @param clientType COPS client-type
34 * @param clientHandle Client handle
35 * @param sock Socket to the PEP
37 public COPSPdpMsgSender (final short clientType, final COPSHandle clientHandle, final Socket sock) {
38 super(clientType, clientHandle, sock);
42 * Sends a decision message
43 * @param removeDecs Decisions to be removed
44 * @param installDecs Decisions to be installed
45 * @throws COPSPdpException
47 public void sendDecision(final Map<String, String> removeDecs, Map<String, String> installDecs)
48 throws COPSPdpException {
50 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
52 // Decisions (no flags supplied)
54 final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
58 final COPSDecision rdec1 = new COPSDecision(Command.REMOVE);
60 final Set<COPSDecision> decisionSet = new HashSet<>();
61 decisionSet.add(rdec1);
62 decisionMap.put(cntxt, decisionSet);
64 for (final Map.Entry<String, String> entry : removeDecs.entrySet()) {
65 // <Named Decision Data: Provisioning> (PRID)
66 final COPSPrID prid = new COPSPrID();
67 prid.setData(new COPSData(entry.getKey()));
68 final COPSDecision decisionPrid = new COPSDecision(CType.NAMED,
69 new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
71 decisionMap.get(cntxt).add(decisionPrid);
73 final COPSPrEPD epd = new COPSPrEPD();
74 final COPSDecision decisionPrepd = new COPSDecision(CType.NAMED,
75 new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
77 decisionMap.get(cntxt).add(decisionPrepd);
82 final COPSDecision idec1 = new COPSDecision(Command.INSTALL);
83 decisionMap.get(cntxt).add(idec1);
85 for (final Map.Entry<String, String> entry : installDecs.entrySet()) {
86 // <Named Decision Data: Provisioning> (PRID)
87 final COPSPrID prid = new COPSPrID();
88 prid.setData(new COPSData(entry.getKey()));
89 final COPSDecision decisionPrid2 = new COPSDecision(CType.NAMED,
90 new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
92 decisionMap.get(cntxt).add(decisionPrid2);
95 // Common Header with the same ClientType as the request
96 // Client Handle with the same clientHandle as the request
97 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
99 //** Send the decision
101 decisionMsg.writeData(_sock);
102 } catch (IOException e) {
103 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
108 * Sends a decision message which was not requested by the PEP
109 * @param removeDecs Decisions to be removed
110 * @param installDecs Decisions to be installed
111 * @throws COPSPdpException
113 public void sendUnsolicitedDecision(final Map<String, String> removeDecs, final Map<String, String> installDecs)
114 throws COPSPdpException {
115 // Common Header with the same ClientType as the request
116 // Client Handle with the same clientHandle as the request
117 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
118 // Decisions (no flags supplied)
120 final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
124 final Set<COPSDecision> decisionSet = new HashSet<>();
125 decisionSet.add(new COPSDecision(Command.REMOVE));
126 decisionMap.put(cntxt, decisionSet);
128 for (final Map.Entry<String, String> entry : removeDecs.entrySet()) {
129 // <Named Decision Data: Provisioning> (PRID)
130 final COPSPrID prid = new COPSPrID();
131 prid.setData(new COPSData(entry.getKey()));
132 decisionMap.get(cntxt).add(new COPSDecision(CType.NAMED,
133 new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
135 // <Named Decision Data: Provisioning> (EPD)
136 final COPSPrEPD epd = new COPSPrEPD();
137 epd.setData(new COPSData(entry.getValue()));
138 decisionMap.get(cntxt).add(
139 new COPSDecision(CType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
144 decisionMap.get(cntxt).add(new COPSDecision(Command.INSTALL));
146 for (final Map.Entry<String, String> entry : installDecs.entrySet()) {
147 // <Named Decision Data: Provisioning> (PRID)
148 final COPSPrID prid = new COPSPrID();
149 prid.setData(new COPSData(entry.getKey()));
150 decisionMap.get(cntxt).add(new COPSDecision(CType.NAMED,
151 new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
153 final COPSPrEPD epd = new COPSPrEPD();
154 epd.setData(new COPSData(entry.getValue()));
155 decisionMap.get(cntxt).add(
156 new COPSDecision(CType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
160 COPSIntegrity intr = new COPSIntegrity();
163 intr.setKeyDigest(new COPSData("KEY DIGEST"));
164 decisionMsg.add(intr);
167 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
169 //** Send the decision
171 decisionMsg.writeData(_sock);
172 } catch (IOException e) {
173 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
178 * Sends a message asking that the request state be deleted
179 * @throws COPSPdpException
181 public void sendDeleteRequestState() throws COPSPdpException {
182 final COPSDecision dec = new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE);
183 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
184 final Set<COPSDecision> decisionSet = new HashSet<>();
185 decisionSet.add(dec);
186 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
188 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
190 decisionMsg.writeData(_sock);
191 } catch (IOException e) {
192 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
197 * Sends a request asking that a new request state be created
198 * @throws COPSPdpException
200 public void sendOpenNewRequestState() throws COPSPdpException {
201 final COPSDecision dec = new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE);
202 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
203 final Set<COPSDecision> decisionSet = new HashSet<>();
204 decisionSet.add(dec);
205 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
207 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
210 decisionMsg.writeData(_sock);
211 } catch (IOException e) {
212 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
217 * Sends a message asking for a COPS sync operation
218 * @throws COPSPdpException
220 public void sendSyncRequestState() throws COPSPdpException {
221 final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
223 msg.writeData(_sock);
224 } catch (IOException e) {
225 throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());