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