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