Created abstract super class for all COPS Message Senders as each contained duplicate...
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / PCMMPdpMsgSender.java
1 /**
2  @header@
3  */
4
5 package org.pcmm;
6
7 import org.pcmm.gates.*;
8 import org.pcmm.gates.IGateSpec.DSCPTOS;
9 import org.pcmm.gates.IGateSpec.Direction;
10 import org.pcmm.gates.impl.*;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.umu.cops.COPSMsgSender;
14 import org.umu.cops.prpdp.COPSPdpException;
15 import org.umu.cops.stack.*;
16 import org.umu.cops.stack.COPSClientSI.CSIType;
17 import org.umu.cops.stack.COPSContext.RType;
18 import org.umu.cops.stack.COPSDecision.Command;
19 import org.umu.cops.stack.COPSDecision.DecisionFlag;
20 import org.umu.cops.stack.COPSHeader.OPCode;
21 import org.umu.cops.stack.COPSObjHeader.CNum;
22 import org.umu.cops.stack.COPSObjHeader.CType;
23
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.Socket;
27 import java.net.UnknownHostException;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32
33 /*
34  * Example of an UNSOLICITED decision
35  *
36  * <Gate Control Command> = <COPS Common Header> <Client Handle> <Context> <Decision Flags> <ClientSI Data>
37  *
38  * <ClientSI Data> = <Gate-Set> | <Gate-Info> | <Gate-Delete> |
39  *                   <PDP-Config> | <Synch-Request> | <Msg-Receipt>
40  * <Gate-Set>      = <Decision Header> <TransactionID> <AMID> <SubscriberID> [<GateID>] <GateSpec>
41  *                   <Traffic Profile> <classifier> [<classifier...>] [<Event Generation Info>]
42  *                   [<Volume-Based Usage Limit>] [<Time-Based Usage Limit>][<Opaque Data>] [<UserID>]
43  */
44
45 /**
46  * COPS message transceiver class for provisioning connections at the PDP side.
47  */
48 public class PCMMPdpMsgSender extends COPSMsgSender {
49
50     public final static Logger logger = LoggerFactory.getLogger(PCMMPdpMsgSender.class);
51
52     protected short _transactionID;
53     protected final short _classifierID;
54
55     // XXX - this does not need to be here
56     protected IGateID _gateID;
57
58     /**
59      * Creates a PCMMPdpMsgSender
60      *
61      * @param clientType
62      *            COPS client-type
63      * @param clientHandle
64      *            Client handle
65      * @param sock
66      *            Socket to the PEP
67      */
68     public PCMMPdpMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock)
69             throws COPSPdpException {
70         this(clientType, (short)0, clientHandle, sock);
71     }
72
73     public PCMMPdpMsgSender(final short clientType, final short tID, final COPSHandle clientHandle,
74                             final Socket sock) throws COPSPdpException {
75         super(clientType, clientHandle, sock);
76         _transactionID = tID;
77         _classifierID = 0;
78     }
79
80     /**
81      * Gets the transaction-id
82      *
83      * @return transaction-id value
84      */
85     public short getTransactionID() {
86         return _transactionID;
87     }
88
89     /**
90      * Gets the gate-id
91      *
92      * @return the gate-id value
93      */
94     public IGateID getGateID() {
95         return _gateID;
96     }
97
98     /**
99      * Sends a PCMM GateSet COPS Decision message
100      * @param gate - the gate
101      * @throws COPSPdpException
102      */
103     public void sendGateSet(final IPCMMGate gate) throws COPSPdpException {
104         final ITransactionID trID = new TransactionID();
105
106         // set transaction ID to gate set
107         trID.setGateCommandType(ITransactionID.GateSet);
108         _transactionID = (_transactionID == 0 ? (short) (Math.random() * hashCode()) : _transactionID);
109         trID.setTransactionIdentifier(_transactionID);
110
111         gate.setTransactionID(trID);
112         // retain the transactionId to gate request mapping for gateID recovery after response
113         // see PCMMPdpReqStateMan.processReport()
114         final Short trIDnum = trID.getTransactionIdentifier();
115         logger.info("Adding gate to cache - " + gate + " with key - " + trIDnum);
116         PCMMGlobalConfig.transactionGateMap.put(trIDnum, gate);
117
118         // new pcmm specific clientsi
119         final byte[] data = gate.getData();
120         // Common Header with the same ClientType as the request
121         // Client Handle with the same clientHandle as the request
122
123         final Set<COPSDecision> decisionSet = new HashSet<>();
124         decisionSet.add(new COPSDecision(CType.DEF, Command.INSTALL, DecisionFlag.REQERROR));
125         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
126         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
127
128         final COPSClientSI clientSD = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData(data, 0, data.length));
129         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, clientSD);
130
131         // ** Send the GateSet Decision
132         try {
133             decisionMsg.writeData(_sock);
134         } catch (IOException e) {
135             logger.error("Failed to send the decision", e);
136         }
137
138     }
139
140     /**
141      * Sends a PCMM GateSet COPS Decision message
142      *
143      * @param num - the number
144      * @throws COPSPdpException
145      */
146     public void sendGateSetDemo(int num) throws COPSPdpException {
147         final IPCMMGate gate = new PCMMGateReq();
148         final ITransactionID trID = new TransactionID();
149         final IAMID amid = new AMID();
150         final ISubscriberID subscriberID = new SubscriberID();
151         final IGateSpec gateSpec = new GateSpec();
152         final IClassifier classifier = new Classifier();
153         final IExtendedClassifier eclassifier = new ExtendedClassifier();
154         final int trafficRate;
155         if (num == 1)
156             trafficRate =   PCMMGlobalConfig.DefaultBestEffortTrafficRate;
157         else
158             trafficRate =   PCMMGlobalConfig.DefaultLowBestEffortTrafficRate;
159
160         final ITrafficProfile trafficProfile = new BestEffortService(
161             (byte) 7); //BestEffortService.DEFAULT_ENVELOP);
162         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
163         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
164         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
165         .setMaximumTrafficBurst(
166             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
167         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
168         .setRequestTransmissionPolicy(
169             PCMMGlobalConfig.BETransmissionPolicy);
170         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
171         .setMaximumSustainedTrafficRate(
172             trafficRate);
173         //  PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
174         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
175
176         ((BestEffortService) trafficProfile).getReservedEnvelop()
177         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
178         ((BestEffortService) trafficProfile).getReservedEnvelop()
179         .setMaximumTrafficBurst(
180             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
181         ((BestEffortService) trafficProfile).getReservedEnvelop()
182         .setRequestTransmissionPolicy(
183             PCMMGlobalConfig.BETransmissionPolicy);
184         ((BestEffortService) trafficProfile).getReservedEnvelop()
185         .setMaximumSustainedTrafficRate(
186             trafficRate);
187         //  PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
188         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
189
190
191         ((BestEffortService) trafficProfile).getCommittedEnvelop()
192         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
193         ((BestEffortService) trafficProfile).getCommittedEnvelop()
194         .setMaximumTrafficBurst(
195             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
196         ((BestEffortService) trafficProfile).getCommittedEnvelop()
197         .setRequestTransmissionPolicy(
198             PCMMGlobalConfig.BETransmissionPolicy);
199         ((BestEffortService) trafficProfile).getCommittedEnvelop()
200         .setMaximumSustainedTrafficRate(
201             trafficRate);
202         //  PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
203         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
204
205
206
207         // set transaction ID to gate set
208         trID.setGateCommandType(ITransactionID.GateSet);
209         _transactionID = (_transactionID == 0 ? (short) (Math.random() * hashCode()) : _transactionID);
210         trID.setTransactionIdentifier(_transactionID);
211
212         amid.setApplicationType((short) 1);
213         amid.setApplicationMgrTag((short) 1);
214         gateSpec.setDirection(Direction.UPSTREAM);
215         gateSpec.setDSCP_TOSOverwrite(DSCPTOS.OVERRIDE);
216         gateSpec.setTimerT1(PCMMGlobalConfig.GateT1);
217         gateSpec.setTimerT2(PCMMGlobalConfig.GateT2);
218         gateSpec.setTimerT3(PCMMGlobalConfig.GateT3);
219         gateSpec.setTimerT4(PCMMGlobalConfig.GateT4);
220
221         // XXX - if the version major is less than 4 we need to use Classifier
222         // TODO - Use some variable here or remove...
223         if (true) {
224             //eclassifier.setProtocol(IClassifier.Protocol.NONE);
225 //            eclassifier.setProtocol(IClassifier.Protocol.TCP);
226             try {
227                 InetAddress subIP = InetAddress
228                                     .getByName(PCMMGlobalConfig.SubscriberID);
229                 InetAddress srcIP = InetAddress
230                                     .getByName(PCMMGlobalConfig.srcIP);
231                 InetAddress dstIP = InetAddress
232                                     .getByName(PCMMGlobalConfig.dstIP);
233                 InetAddress mask = InetAddress.getByName("0.0.0.0");
234                 subscriberID.setSourceIPAddress(subIP);
235                 eclassifier.setSourceIPAddress(srcIP);
236                 eclassifier.setDestinationIPAddress(dstIP);
237                 eclassifier.setIPDestinationMask(mask);
238                 eclassifier.setIPSourceMask(mask);
239             } catch (UnknownHostException unae) {
240                 logger.error("Error getByName", unae);
241             }
242             eclassifier.setSourcePortStart(PCMMGlobalConfig.srcPort);
243             eclassifier.setSourcePortEnd(PCMMGlobalConfig.srcPort);
244             eclassifier.setDestinationPortStart(PCMMGlobalConfig.dstPort);
245             eclassifier.setDestinationPortEnd(PCMMGlobalConfig.dstPort);
246             eclassifier.setActivationState((byte) 0x01);
247             // check if we have a stored value of classifierID else we just
248             // create
249             // one
250             // eclassifier.setClassifierID((short) 0x01);
251             eclassifier.setClassifierID((short) (_classifierID == 0 ? Math
252                                                  .random() * hashCode() : _classifierID));
253             // XXX - testie
254             // eclassifier.setClassifierID((short) 1);
255
256             eclassifier.setAction((byte) 0x00);
257             // XXX - temp default until Gate Modify is hacked in
258             // eclassifier.setPriority(PCMMGlobalConfig.EClassifierPriority);
259             eclassifier.setPriority((byte) 65);
260
261         } else {
262 //            classifier.setProtocol(IClassifier.Protocol.TCP);
263             try {
264                 InetAddress subIP = InetAddress
265                                     .getByName(PCMMGlobalConfig.SubscriberID);
266                 InetAddress srcIP = InetAddress
267                                     .getByName(PCMMGlobalConfig.srcIP);
268                 InetAddress dstIP = InetAddress
269                                     .getByName(PCMMGlobalConfig.dstIP);
270                 subscriberID.setSourceIPAddress(subIP);
271                 classifier.setSourceIPAddress(srcIP);
272                 classifier.setDestinationIPAddress(dstIP);
273             } catch (UnknownHostException unae) {
274                 logger.error("Error getByName", unae);
275             }
276             classifier.setSourcePort(PCMMGlobalConfig.srcPort);
277             classifier.setDestinationPort(PCMMGlobalConfig.dstPort);
278         }
279
280         gate.setTransactionID(trID);
281         gate.setAMID(amid);
282         gate.setSubscriberID(subscriberID);
283         gate.setGateSpec(gateSpec);
284         gate.setTrafficProfile(trafficProfile);
285         gate.setClassifier(eclassifier);
286
287         final byte[] data = gate.getData();
288
289         final Set<COPSDecision> decisionSet = new HashSet<>();
290         decisionSet.add(new COPSDecision(CType.NA, Command.INSTALL, DecisionFlag.REQERROR));
291         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
292         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
293         final COPSClientSI clientSD = new COPSClientSI(CSIType.NAMED, new COPSData(data, 0, data.length));
294
295         // Common Header with the same ClientType as the request
296         // Client Handle with the same clientHandle as the request
297         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, clientSD);
298
299         // ** Send the GateSet Decision
300         // **
301         try {
302             decisionMsg.writeData(_sock);
303         } catch (IOException e) {
304             logger.error("Failed to send the decision", e);
305         }
306
307     }
308     /**
309      * Sends a PCMM GateSet COPS Decision message
310      * @throws COPSPdpException
311      */
312     public void sendGateSetBestEffortWithExtendedClassifier() throws COPSPdpException {
313         final IPCMMGate gate = new PCMMGateReq();
314         final ITransactionID trID = new TransactionID();
315         final IAMID amid = new AMID();
316         final ISubscriberID subscriberID = new SubscriberID();
317         final IGateSpec gateSpec = new GateSpec();
318         final IClassifier classifier = new Classifier();
319         final IExtendedClassifier eclassifier = new ExtendedClassifier();
320
321         // XXX check if other values should be provided
322         //
323         final ITrafficProfile trafficProfile = new BestEffortService(
324             (byte) 7); //BestEffortService.DEFAULT_ENVELOP);
325         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
326         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
327         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
328         .setMaximumTrafficBurst(
329             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
330         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
331         .setRequestTransmissionPolicy(
332             PCMMGlobalConfig.BETransmissionPolicy);
333         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
334         .setMaximumSustainedTrafficRate(
335             PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
336         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
337
338         ((BestEffortService) trafficProfile).getReservedEnvelop()
339         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
340         ((BestEffortService) trafficProfile).getReservedEnvelop()
341         .setMaximumTrafficBurst(
342             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
343         ((BestEffortService) trafficProfile).getReservedEnvelop()
344         .setRequestTransmissionPolicy(
345             PCMMGlobalConfig.BETransmissionPolicy);
346         ((BestEffortService) trafficProfile).getReservedEnvelop()
347         .setMaximumSustainedTrafficRate(
348             PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
349         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
350
351
352         ((BestEffortService) trafficProfile).getCommittedEnvelop()
353         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
354         ((BestEffortService) trafficProfile).getCommittedEnvelop()
355         .setMaximumTrafficBurst(
356             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
357         ((BestEffortService) trafficProfile).getCommittedEnvelop()
358         .setRequestTransmissionPolicy(
359             PCMMGlobalConfig.BETransmissionPolicy);
360         ((BestEffortService) trafficProfile).getCommittedEnvelop()
361         .setMaximumSustainedTrafficRate(
362             PCMMGlobalConfig.DefaultLowBestEffortTrafficRate );
363         //  PCMMGlobalConfig.DefaultBestEffortTrafficRate);
364
365
366
367         // set transaction ID to gate set
368         trID.setGateCommandType(ITransactionID.GateSet);
369         _transactionID = (_transactionID == 0 ? (short) (Math.random() * hashCode()) : _transactionID);
370         trID.setTransactionIdentifier(_transactionID);
371
372         amid.setApplicationType((short) 1);
373         amid.setApplicationMgrTag((short) 1);
374         gateSpec.setDirection(Direction.UPSTREAM);
375         gateSpec.setDSCP_TOSOverwrite(DSCPTOS.OVERRIDE);
376         gateSpec.setTimerT1(PCMMGlobalConfig.GateT1);
377         gateSpec.setTimerT2(PCMMGlobalConfig.GateT2);
378         gateSpec.setTimerT3(PCMMGlobalConfig.GateT3);
379         gateSpec.setTimerT4(PCMMGlobalConfig.GateT4);
380
381         // XXX - if the version major is less than 4 we need to use Classifier
382         if (true) {
383             //eclassifier.setProtocol(IClassifier.Protocol.NONE);
384 //            eclassifier.setProtocol(IClassifier.Protocol.TCP);
385             try {
386                 InetAddress subIP = InetAddress
387                                     .getByName(PCMMGlobalConfig.SubscriberID);
388                 InetAddress srcIP = InetAddress
389                                     .getByName(PCMMGlobalConfig.srcIP);
390                 InetAddress dstIP = InetAddress
391                                     .getByName(PCMMGlobalConfig.dstIP);
392                 InetAddress mask = InetAddress.getByName("0.0.0.0");
393                 subscriberID.setSourceIPAddress(subIP);
394                 eclassifier.setSourceIPAddress(srcIP);
395                 eclassifier.setDestinationIPAddress(dstIP);
396                 eclassifier.setIPDestinationMask(mask);
397                 eclassifier.setIPSourceMask(mask);
398             } catch (UnknownHostException unae) {
399                 logger.error("Error getByName", unae);
400             }
401             eclassifier.setSourcePortStart(PCMMGlobalConfig.srcPort);
402             eclassifier.setSourcePortEnd(PCMMGlobalConfig.srcPort);
403             eclassifier.setDestinationPortStart(PCMMGlobalConfig.dstPort);
404             eclassifier.setDestinationPortEnd(PCMMGlobalConfig.dstPort);
405             eclassifier.setActivationState((byte) 0x01);
406             // check if we have a stored value of classifierID else we just
407             // create
408             // one
409             // eclassifier.setClassifierID((short) 0x01);
410             eclassifier.setClassifierID((short) (_classifierID == 0 ? Math
411                                                  .random() * hashCode() : _classifierID));
412             // XXX - testie
413             // eclassifier.setClassifierID((short) 1);
414
415             eclassifier.setAction((byte) 0x00);
416             // XXX - temp default until Gate Modify is hacked in
417             // eclassifier.setPriority(PCMMGlobalConfig.EClassifierPriority);
418             eclassifier.setPriority((byte) 65);
419
420         } else {
421 //            classifier.setProtocol(IClassifier.Protocol.TCP);
422             try {
423                 InetAddress subIP = InetAddress
424                                     .getByName(PCMMGlobalConfig.SubscriberID);
425                 InetAddress srcIP = InetAddress
426                                     .getByName(PCMMGlobalConfig.srcIP);
427                 InetAddress dstIP = InetAddress
428                                     .getByName(PCMMGlobalConfig.dstIP);
429                 subscriberID.setSourceIPAddress(subIP);
430                 classifier.setSourceIPAddress(srcIP);
431                 classifier.setDestinationIPAddress(dstIP);
432             } catch (UnknownHostException unae) {
433                 logger.error("Error getByName", unae);
434             }
435             classifier.setSourcePort(PCMMGlobalConfig.srcPort);
436             classifier.setDestinationPort(PCMMGlobalConfig.dstPort);
437         }
438
439         gate.setTransactionID(trID);
440         gate.setAMID(amid);
441         gate.setSubscriberID(subscriberID);
442         gate.setGateSpec(gateSpec);
443         gate.setTrafficProfile(trafficProfile);
444         gate.setClassifier(eclassifier);
445
446         byte[] data = gate.getData();
447
448         final Set<COPSDecision> decisionSet = new HashSet<>();
449         decisionSet.add(new COPSDecision(CType.CSI, Command.INSTALL, DecisionFlag.REQERROR));
450         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
451         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
452         final COPSClientSI clientSD = new COPSClientSI(CSIType.NAMED, new COPSData(data, 0, data.length));
453
454         // Common Header with the same ClientType as the request
455         // Client Handle with the same clientHandle as the request
456         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, clientSD);
457
458         // ** Send the GateSet Decision
459         // **
460         try {
461             decisionMsg.writeData(_sock);
462         } catch (IOException e) {
463             logger.error("Failed to send the decision", e);
464         }
465
466     }
467
468
469     public boolean handleGateReport(final Socket socket) throws COPSPdpException {
470         try {
471             // waits for the gate-set-ack or error
472             final COPSMsg responseMsg = COPSTransceiver.receiveMsg(socket);
473             if (responseMsg.getHeader().getOpCode().equals(OPCode.RPT)) {
474                 logger.info("processing received report from CMTS");
475                 final COPSReportMsg reportMsg = (COPSReportMsg) responseMsg;
476                 if (reportMsg.getClientSI() == null) {
477                     return false;
478                 }
479                 final IPCMMGate responseGate = new PCMMGateReq(reportMsg.getClientSI().getData().getData());
480                 if (responseGate.getTransactionID() != null
481                         && responseGate.getTransactionID().getGateCommandType() == ITransactionID.GateSetAck) {
482                     logger.info("the CMTS has sent a Gate-Set-Ack response");
483                     // here CMTS responded that he acknowledged the Gate-Set
484                     // TODO do further check of Gate-Set-Ack GateID etc...
485                     _gateID = responseGate.getGateID();
486                     return true;
487                 } else {
488                     return false;
489                 }
490             }
491             return false;
492         } catch (Exception e) { // COPSException, IOException
493             throw new COPSPdpException("Error COPSTransceiver.receiveMsg", e);
494         }
495     }
496
497
498     /**
499      * Sends a PCMM GateSet COPS Decision message
500      *
501      * @throws COPSPdpException
502      */
503     public void sendGateSet() throws COPSPdpException {
504         // Common Header with the same ClientType as the request
505
506         final IPCMMGate gate = new PCMMGateReq();
507         final ITransactionID trID = new TransactionID();
508
509         final IAMID amid = new AMID();
510         final ISubscriberID subscriberID = new SubscriberID();
511         final IGateSpec gateSpec = new GateSpec();
512         final IClassifier classifier = new Classifier();
513         // XXX check if other values should be provided
514         final ITrafficProfile trafficProfile = new BestEffortService(
515             BestEffortService.DEFAULT_ENVELOP);
516         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
517         .setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
518         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
519         .setMaximumTrafficBurst(
520             BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
521         ((BestEffortService) trafficProfile).getAuthorizedEnvelop()
522         .setRequestTransmissionPolicy(
523             PCMMGlobalConfig.BETransmissionPolicy);
524
525         // set transaction ID to gate set
526         trID.setGateCommandType(ITransactionID.GateSet);
527         _transactionID = (_transactionID == 0 ? (short) (Math.random() * hashCode()) : _transactionID);
528         trID.setTransactionIdentifier(_transactionID);
529
530         amid.setApplicationType((short) 1);
531         amid.setApplicationMgrTag((short) 1);
532         gateSpec.setDirection(Direction.UPSTREAM);
533         gateSpec.setDSCP_TOSOverwrite(DSCPTOS.OVERRIDE);
534         gateSpec.setTimerT1(PCMMGlobalConfig.GateT1);
535         gateSpec.setTimerT2(PCMMGlobalConfig.GateT2);
536         gateSpec.setTimerT3(PCMMGlobalConfig.GateT3);
537         gateSpec.setTimerT4(PCMMGlobalConfig.GateT4);
538
539         /*
540          * ((DOCSISServiceClassNameTrafficProfile) trafficProfile)
541          * .setServiceClassName("S_up");
542          */
543
544 //        classifier.setProtocol(IClassifier.Protocol.TCP);
545         try {
546             InetAddress subIP = InetAddress.getByName(PCMMGlobalConfig.SubscriberID);
547             InetAddress srcIP = InetAddress.getByName(PCMMGlobalConfig.srcIP);
548             InetAddress dstIP = InetAddress.getByName(PCMMGlobalConfig.dstIP);
549             subscriberID.setSourceIPAddress(subIP);
550             classifier.setSourceIPAddress(srcIP);
551             classifier.setDestinationIPAddress(dstIP);
552         } catch (UnknownHostException unae) {
553             logger.error("Error getByName", unae);
554         }
555         classifier.setSourcePort(PCMMGlobalConfig.srcPort);
556         classifier.setDestinationPort(PCMMGlobalConfig.dstPort);
557
558         gate.setTransactionID(trID);
559         gate.setAMID(amid);
560         gate.setSubscriberID(subscriberID);
561         gate.setGateSpec(gateSpec);
562         gate.setTrafficProfile(trafficProfile);
563         gate.setClassifier(classifier);
564
565         final byte[] data = gate.getData();
566
567         final Set<COPSDecision> decisionSet = new HashSet<>();
568         decisionSet.add(new COPSDecision(CType.CSI, Command.INSTALL, DecisionFlag.REQERROR));
569         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
570         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
571
572         final COPSClientSI clientSD = new COPSClientSI(CSIType.NAMED, new COPSData(data, 0, data.length));
573
574         // Client Handle with the same clientHandle as the request
575         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, clientSD);
576
577         // ** Send the GateSet Decision
578         // **
579         try {
580             decisionMsg.writeData(_sock);
581         } catch (IOException e) {
582             logger.error("Failed to send the decision", e);
583         }
584     }
585
586     /**
587      * Sends a message asking that the request state be deleted
588      *
589      * @throws COPSPdpException
590      */
591     public void sendGateDelete(final IPCMMGate gate) throws COPSPdpException {
592         // set transaction ID to gate set
593         final ITransactionID trID = new TransactionID();
594         trID.setGateCommandType(ITransactionID.GateDelete);
595         _transactionID = (_transactionID == 0 ? (short) (Math.random() * hashCode()) : _transactionID);
596         trID.setTransactionIdentifier(_transactionID);
597         gate.setTransactionID(trID);
598
599         Short trIDnum = trID.getTransactionIdentifier();
600         PCMMGlobalConfig.transactionGateMap.put(trIDnum, gate);
601
602         // gateDelete only requires AMID, subscriberID, and gateID
603         // remove the gateSpec, traffic profile, and classifiers from original gate request
604         gate.setGateSpec(null);
605         gate.setTrafficProfile(null);
606         gate.setClassifier(null);
607         // clear the error object
608         gate.setError(null);
609
610         // XXX - GateID
611         final byte[] data = gate.getData();
612         final Set<COPSDecision> decisionSet = new HashSet<>();
613         decisionSet.add(new COPSDecision(CType.DEF, Command.INSTALL, DecisionFlag.REQERROR));
614         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
615         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
616         final COPSClientSI clientSD = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData(data, 0, data.length));
617
618         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, clientSD);
619
620         // ** Send the GateDelete Decision
621         // **
622         try {
623             decisionMsg.writeData(_sock);
624             // decisionMsg.writeData(socket_id);
625         } catch (IOException e) {
626             logger.error("Failed to send the decision", e);
627         }
628     }
629
630     /**
631      * Sends a request asking that a new request state be created
632      *
633      * @throws COPSPdpException
634      */
635     public void sendOpenNewRequestState() throws COPSPdpException {
636         /*
637          * <Decision Message> ::= <Common Header: Flag UNSOLICITED> <Client
638          * Handle> *(<Decision>) [<Integrity>] <Decision> ::= <Context>
639          * <Decision: Flags> <Decision: Flags> ::= Install Request-State
640          */
641
642         final Set<COPSDecision> decisionSet = new HashSet<>();
643         decisionSet.add(new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE));
644         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
645         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
646
647         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
648
649         try {
650             decisionMsg.writeData(_sock);
651         } catch (IOException e) {
652             throw new COPSPdpException("Failed to send the open new request state", e);
653         }
654     }
655
656     /**
657      * Sends a message asking for a COPS sync operation
658      *
659      * @throws COPSPdpException
660      */
661     public void sendGateInfo() throws COPSPdpException {
662         /*
663          * <Gate-Info> ::= <Common Header> [<Client Handle>] [<Integrity>]
664          */
665         final COPSSyncStateMsg msg = new COPSSyncStateMsg(getClientType(), _handle, null);
666         try {
667             msg.writeData(_sock);
668         } catch (IOException e) {
669             throw new COPSPdpException("Failed to send the GateInfo request", e);
670         }
671     }
672
673     /**
674      * Sends a message asking for a COPS sync operation
675      *
676      * @throws COPSPdpException
677      */
678     public void sendSyncRequest() throws COPSPdpException {
679         /*
680          * <Synchronize State Request> ::= <Common Header> [<Client Handle>]
681          * [<Integrity>]
682          */
683
684         // Client Handle with the same clientHandle as the request
685         final COPSSyncStateMsg msg = new COPSSyncStateMsg(getClientType(), _handle, null);
686         try {
687             msg.writeData(_sock);
688         } catch (IOException e) {
689             throw new COPSPdpException("Failed to send the sync state request", e);
690         }
691     }
692     // XXX - Temp
693     public void sendSyncRequestState() throws COPSPdpException {
694     }
695     // XXX - Temp
696     public void sendDeleteRequestState() throws COPSPdpException {
697     }
698 }