2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
7 import org.pcmm.gates.IGateID;
8 import org.pcmm.gates.IPCMMGate;
9 import org.pcmm.gates.ITransactionID;
10 import org.pcmm.gates.ITransactionID.GateCommandType;
11 import org.pcmm.gates.impl.PCMMGateReq;
12 import org.pcmm.gates.impl.TransactionID;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.umu.cops.COPSMsgSender;
16 import org.umu.cops.prpdp.COPSPdpException;
17 import org.umu.cops.stack.*;
18 import org.umu.cops.stack.COPSContext.RType;
19 import org.umu.cops.stack.COPSDecision.Command;
20 import org.umu.cops.stack.COPSDecision.DecisionFlag;
21 import org.umu.cops.stack.COPSHeader.OPCode;
22 import org.umu.cops.stack.COPSObjHeader.CNum;
23 import org.umu.cops.stack.COPSObjHeader.CType;
25 import java.io.IOException;
26 import java.net.Socket;
27 import java.util.HashMap;
28 import java.util.HashSet;
33 * Example of an UNSOLICITED decision
35 * <Gate Control Command> = <COPS Common Header> <Client Handle> <Context> <Decision Flags> <ClientSI Data>
37 * <ClientSI Data> = <Gate-Set> | <Gate-Info> | <Gate-Delete> |
38 * <PDP-Config> | <Synch-Request> | <Msg-Receipt>
39 * <Gate-Set> = <Decision Header> <TransactionID> <AMID> <SubscriberID> [<GateID>] <GateSpec>
40 * <Traffic Profile> <classifier> [<classifier...>] [<Event Generation Info>]
41 * [<Volume-Based Usage Limit>] [<Time-Based Usage Limit>][<Opaque Data>] [<UserID>]
45 * COPS message transceiver class for provisioning connections at the PDP side.
47 public class PCMMPdpMsgSender extends COPSMsgSender {
49 public final static Logger logger = LoggerFactory.getLogger(PCMMPdpMsgSender.class);
51 protected final short _transactionID;
52 protected final short _classifierID;
54 // XXX - this does not need to be here
55 protected IGateID _gateID;
58 * Creates a PCMMPdpMsgSender
67 public PCMMPdpMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
68 this(clientType, (short)0, clientHandle, sock);
71 public PCMMPdpMsgSender(final short clientType, final short tID, final COPSHandle clientHandle,
73 super(clientType, clientHandle, sock);
74 _transactionID = tID == 0 ? (short) (Math.random() * hashCode()) : tID;
81 * @return the gate-id value
83 public IGateID getGateID() {
88 * Sends a PCMM GateSet COPS Decision message
89 * @param gate - the gate
90 * @throws COPSPdpException
92 public void sendGateSet(final IPCMMGate gate) throws COPSPdpException {
93 // set transaction ID to gate set
94 final ITransactionID trID = new TransactionID(_transactionID, GateCommandType.GATE_SET);
96 gate.setTransactionID(trID);
97 // retain the transactionId to gate request mapping for gateID recovery after response
98 // see PCMMPdpReqStateMan.processReport()
99 final Short trIDnum = trID.getTransactionIdentifier();
100 logger.info("Adding gate to cache - " + gate + " with key - " + trIDnum);
101 PCMMGlobalConfig.transactionGateMap.put(trIDnum, gate);
103 // new pcmm specific clientsi
104 final byte[] data = gate.getData();
105 // Common Header with the same ClientType as the request
106 // Client Handle with the same clientHandle as the request
108 final Set<COPSDecision> decisionSet = new HashSet<>();
109 decisionSet.add(new COPSDecision(CType.DEF, Command.INSTALL, DecisionFlag.REQERROR));
110 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
111 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
113 final COPSClientSI clientSD = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData(data, 0, data.length));
114 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, clientSD);
116 // ** Send the GateSet Decision
118 decisionMsg.writeData(_sock);
119 } catch (IOException e) {
120 logger.error("Failed to send the decision", e);
125 public boolean handleGateReport(final Socket socket) throws COPSPdpException {
127 // waits for the gate-set-ack or error
128 final COPSMsg responseMsg = COPSTransceiver.receiveMsg(socket);
129 if (responseMsg.getHeader().getOpCode().equals(OPCode.RPT)) {
130 logger.info("processing received report from CMTS");
131 final COPSReportMsg reportMsg = (COPSReportMsg) responseMsg;
132 if (reportMsg.getClientSI() == null) {
135 final IPCMMGate responseGate = PCMMGateReq.parse(reportMsg.getClientSI().getData().getData());
136 if (responseGate.getTransactionID() != null
137 && responseGate.getTransactionID().getGateCommandType().equals(GateCommandType.GATE_SET_ACK)) {
138 logger.info("the CMTS has sent a Gate-Set-Ack response");
139 // here CMTS responded that he acknowledged the Gate-Set
140 // TODO do further check of Gate-Set-Ack GateID etc...
141 _gateID = responseGate.getGateID();
148 } catch (Exception e) { // COPSException, IOException
149 throw new COPSPdpException("Error COPSTransceiver.receiveMsg", e);
155 * Sends a message asking that the request state be deleted
157 * @throws COPSPdpException
159 public void sendGateDelete(final IPCMMGate gate) throws COPSPdpException {
160 // set transaction ID to gate set
161 final ITransactionID trID = new TransactionID(_transactionID, GateCommandType.GATE_DELETE);
162 gate.setTransactionID(trID);
164 Short trIDnum = trID.getTransactionIdentifier();
165 PCMMGlobalConfig.transactionGateMap.put(trIDnum, gate);
167 // gateDelete only requires AMID, subscriberID, and gateID
168 // remove the gateSpec, traffic profile, and classifiers from original gate request
169 gate.setGateSpec(null);
170 gate.setTrafficProfile(null);
171 gate.setClassifiers(null);
172 // clear the error object
176 final byte[] data = gate.getData();
177 final Set<COPSDecision> decisionSet = new HashSet<>();
178 decisionSet.add(new COPSDecision(CType.DEF, Command.INSTALL, DecisionFlag.REQERROR));
179 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
180 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
181 final COPSClientSI clientSD = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData(data, 0, data.length));
183 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, clientSD);
185 // ** Send the GateDelete Decision
188 decisionMsg.writeData(_sock);
189 // decisionMsg.writeData(socket_id);
190 } catch (IOException e) {
191 logger.error("Failed to send the decision", e);
196 * Sends a request asking that a new request state be created
198 * @throws COPSPdpException
200 public void sendOpenNewRequestState() throws COPSPdpException {
202 * <Decision Message> ::= <Common Header: Flag UNSOLICITED> <Client
203 * Handle> *(<Decision>) [<Integrity>] <Decision> ::= <Context>
204 * <Decision: Flags> <Decision: Flags> ::= Install Request-State
207 final Set<COPSDecision> decisionSet = new HashSet<>();
208 decisionSet.add(new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE));
209 final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
210 decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
212 final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
215 decisionMsg.writeData(_sock);
216 } catch (IOException e) {
217 throw new COPSPdpException("Failed to send the open new request state", e);
222 * Sends a message asking for a COPS sync operation
224 * @throws COPSPdpException
226 public void sendGateInfo() throws COPSPdpException {
228 * <Gate-Info> ::= <Common Header> [<Client Handle>] [<Integrity>]
230 final COPSSyncStateMsg msg = new COPSSyncStateMsg(getClientType(), _handle, null);
232 msg.writeData(_sock);
233 } catch (IOException e) {
234 throw new COPSPdpException("Failed to send the GateInfo request", e);
239 * Sends a message asking for a COPS sync operation
241 * @throws COPSPdpException
243 public void sendSyncRequest() throws COPSPdpException {
245 * <Synchronize State Request> ::= <Common Header> [<Client Handle>]
249 // Client Handle with the same clientHandle as the request
250 final COPSSyncStateMsg msg = new COPSSyncStateMsg(getClientType(), _handle, null);
252 msg.writeData(_sock);
253 } catch (IOException e) {
254 throw new COPSPdpException("Failed to send the sync state request", e);
258 public void sendSyncRequestState() throws COPSPdpException {
261 public void sendDeleteRequestState() throws COPSPdpException {