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