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