fa89754a344657ac46d1a669124dc98a05f673bb
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / pcmm / rcd / impl / CMTS.java
1 /**
2  @header@
3  */
4 package org.pcmm.rcd.impl;
5
6 import java.net.Socket;
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.Properties;
10 import java.util.Vector;
11 import java.util.concurrent.Callable;
12
13 import org.pcmm.gates.IPCMMGate;
14 import org.pcmm.gates.ITransactionID;
15 import org.pcmm.gates.impl.PCMMGateReq;
16 import org.pcmm.messages.impl.MessageFactory;
17 import org.pcmm.rcd.ICMTS;
18 import org.umu.cops.prpep.COPSPepConnection;
19 import org.umu.cops.prpep.COPSPepDataProcess;
20 import org.umu.cops.prpep.COPSPepException;
21 import org.umu.cops.prpep.COPSPepReqStateMan;
22 import org.umu.cops.stack.COPSAcctTimer;
23 import org.umu.cops.stack.COPSClientAcceptMsg;
24 import org.umu.cops.stack.COPSClientCloseMsg;
25 import org.umu.cops.stack.COPSContext;
26 import org.umu.cops.stack.COPSData;
27 import org.umu.cops.stack.COPSDecision;
28 import org.umu.cops.stack.COPSDecisionMsg;
29 import org.umu.cops.stack.COPSError;
30 import org.umu.cops.stack.COPSException;
31 import org.umu.cops.stack.COPSHeader;
32 import org.umu.cops.stack.COPSKATimer;
33 import org.umu.cops.stack.COPSMsg;
34 import org.umu.cops.stack.COPSPrObjBase;
35 import org.umu.cops.stack.COPSReqMsg;
36
37 /**
38  *
39  */
40 public class CMTS extends AbstractPCMMServer implements ICMTS {
41
42         public CMTS() {
43                 super();
44         }
45
46         @Override
47         protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
48
49                 return new AbstractPCMMClientHandler(socket) {
50
51                         private String handle;
52
53                         public void run() {
54                                 try {
55                                         // send OPN message
56                                         // set the major version info and minor version info to
57                                         // default (5,0)
58                                         logger.info("Send OPN message to the PS");
59                                         sendRequest(MessageFactory.getInstance().create(COPSHeader.COPS_OP_OPN, new Properties()));
60                                         // wait for CAT
61                                         COPSMsg recvMsg = readMessage();
62
63                                         if (recvMsg.getHeader().isAClientClose()) {
64                                                 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvMsg;
65                                                 logger.info("PS requested Client-Close" + cMsg.getError().getDescription());
66                                                 // send a CC message and close the socket
67                                                 disconnect();
68                                                 return;
69                                         }
70                                         if (recvMsg.getHeader().isAClientAccept()) {
71                                                 logger.info("received Client-Accept from PS");
72                                                 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
73                                                 // Support
74                                                 if (cMsg.getIntegrity() != null) {
75                                                         throw new COPSPepException("Unsupported object (Integrity)");
76                                                 }
77
78                                                 // Mandatory KATimer
79                                                 COPSKATimer kt = cMsg.getKATimer();
80                                                 if (kt == null)
81                                                         throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
82                                                 short kaTimeVal = kt.getTimerVal();
83
84                                                 // ACTimer
85                                                 COPSAcctTimer at = cMsg.getAcctTimer();
86                                                 short acctTimer = 0;
87                                                 if (at != null)
88                                                         acctTimer = at.getTimerVal();
89
90                                                 logger.info("Send a REQ message to the PS");
91                                                 {
92                                                         Properties prop = new Properties();
93                                                         COPSMsg reqMsg = MessageFactory.getInstance().create(COPSHeader.COPS_OP_REQ, prop);
94                                                         handle = ((COPSReqMsg) reqMsg).getClientHandle().getId().str();
95                                                         sendRequest(reqMsg);
96                                                 }
97                                                 // Create the connection manager
98                                                 PCMMCmtsConnection conn = new PCMMCmtsConnection(CLIENT_TYPE, socket);
99                                                 // pcmm specific handler
100                                                 // conn.addReqStateMgr(handle, new
101                                                 // PCMMPSReqStateMan(CLIENT_TYPE, handle));
102                                                 conn.addRequestState(handle, new CmtsDataProcessor());
103                                                 conn.setKaTimer(kaTimeVal);
104                                                 conn.setAcctTimer(acctTimer);
105                                                 logger.info(getClass().getName() + " Thread(conn).start");
106                                                 new Thread(conn).start();
107                                         } else {
108                                                 // messages of other types are not expected
109                                                 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
110                                         }
111                                 } catch (Exception e) {
112                                         logger.error(e.getMessage());
113                                 }
114                         }
115
116                         @Override
117                         public void task(Callable<?> c) {
118                                 // TODO Auto-generated method stub
119
120                         }
121
122                         @Override
123                         public void shouldWait(int t) {
124                                 // TODO Auto-generated method stub
125
126                         }
127
128                         @Override
129                         public void done() {
130                                 // TODO Auto-generated method stub
131
132                         }
133
134                 };
135         }
136
137         /* public */class PCMMCmtsConnection extends COPSPepConnection {
138
139                 public PCMMCmtsConnection(short clientType, Socket sock) {
140                         super(clientType, sock);
141                 }
142
143                 public COPSPepReqStateMan addRequestState(String clientHandle, COPSPepDataProcess process)
144                                 throws COPSException, COPSPepException {
145                         return super.addRequestState(clientHandle, process);
146                 }
147
148                 // public void addReqStateMgr(String hanlde, COPSPepReqStateMan r) {
149                 // // map < String(COPSHandle), COPSPepReqStateMan>;
150                 // getReqStateMans().put(hanlde, r);
151                 // }
152         }
153
154         @SuppressWarnings("rawtypes")
155         class PCMMPSReqStateMan extends COPSPepReqStateMan {
156
157                 public PCMMPSReqStateMan(short clientType, String clientHandle) {
158                         super(clientType, clientHandle);
159                         _process = new CmtsDataProcessor();
160
161                 }
162
163                 @Override
164                 protected void processDecision(COPSDecisionMsg dMsg)
165                                 throws COPSPepException {
166
167                         // COPSHandle handle = dMsg.getClientHandle();
168                         Hashtable decisions = dMsg.getDecisions();
169
170                         Hashtable<String, String> removeDecs = new Hashtable<String, String>(10);
171                         Hashtable<String, String> installDecs = new Hashtable<String, String>(10);
172                         Hashtable<String, String> errorDecs = new Hashtable<String, String>(10);
173                         for (Enumeration e = decisions.keys(); e.hasMoreElements();) {
174
175                                 COPSContext context = (COPSContext) e.nextElement();
176                                 Vector v = (Vector) decisions.get(context);
177                                 Enumeration ee = v.elements();
178                                 COPSDecision cmddecision = (COPSDecision) ee.nextElement();
179
180                                 // cmddecision --> we must check whether it is an error!
181
182                                 if (cmddecision.isInstallDecision()) {
183                                         String prid = new String();
184                                         for (; ee.hasMoreElements();) {
185                                                 COPSDecision decision = (COPSDecision) ee.nextElement();
186                                                 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
187                                                 switch (obj.getSNum()) {
188                                                 // TODO when there is install request only the PR_PRID
189                                                 // is git but the ClientSI object containing the PR_EPD
190                                                 // is null??? this is why the tests fail and so I set
191                                                 // the assertion to NOT true....
192                                                 case COPSPrObjBase.PR_PRID:
193                                                         prid = obj.getData().str();
194                                                 break;
195                                                 case COPSPrObjBase.PR_EPD:
196                                                         installDecs.put(prid, obj.getData().str());
197                                                 break;
198                                                 default:
199                                                 break;
200                                                 }
201                                         }
202                                 }
203                                 if (cmddecision.isRemoveDecision()) {
204                                         String prid = new String();
205                                         for (; ee.hasMoreElements();) {
206                                                 COPSDecision decision = (COPSDecision) ee.nextElement();
207                                                 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
208                                                 switch (obj.getSNum()) {
209                                                 case COPSPrObjBase.PR_PRID:
210                                                         prid = obj.getData().str();
211                                                 break;
212                                                 case COPSPrObjBase.PR_EPD:
213                                                         removeDecs.put(prid, obj.getData().str());
214                                                 break;
215                                                 default:
216                                                 break;
217                                                 }
218                                         }
219                                 }
220                         }
221                         if (_process != null) {
222                                 // ** Apply decisions to the configuration
223                                 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
224                                 _status = ST_DECS;
225                                 if (_process.isFailReport(this)) {
226                                         // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
227                                         _sender.sendFailReport(_process.getReportData(this));
228                                 } else {
229                                         // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
230                                         _sender.sendSuccessReport(_process.getReportData(this));
231                                 }
232                                 _status = ST_REPORT;
233                         }
234                 }
235         }
236
237         @SuppressWarnings("rawtypes")
238         class CmtsDataProcessor extends COPSPepDataProcess {
239
240                 private Hashtable<String, String> removeDecs;
241                 private Hashtable<String, String> installDecs;
242                 private Hashtable<String, String> errorDecs;
243                 private COPSPepReqStateMan stateManager;
244
245                 public CmtsDataProcessor() {
246                         setRemoveDecs(new Hashtable<String, String>(10));
247                         setInstallDecs(new Hashtable<String, String>(10));
248                         setErrorDecs(new Hashtable<String, String>(10));
249                 }
250
251                 @SuppressWarnings("unchecked")
252                 @Override
253                 public void setDecisions(COPSPepReqStateMan man, Hashtable removeDecs, Hashtable installDecs, Hashtable errorDecs) {
254                         setRemoveDecs(removeDecs);
255                         setInstallDecs(installDecs);
256                         setErrorDecs(errorDecs);
257                         setStateManager(man);
258                 }
259
260                 @Override
261                 public boolean isFailReport(COPSPepReqStateMan man) {
262                         return (errorDecs != null && errorDecs.size() > 0);
263                 }
264
265                 @Override
266                 public Hashtable getReportData(COPSPepReqStateMan man) {
267                         if (isFailReport(man)) {
268                                 return errorDecs;
269                         } else {
270                                 ITransactionID transactionID = null;
271                                 String key = null;
272                                 Hashtable<String, String> siDataHashTable = new Hashtable<String, String>();
273                                 if (installDecs.size() > 0) {
274                                         String data = "";
275                                         for (String k : installDecs.keySet()) {
276                                                 data = installDecs.get(k);
277                                                 break;
278                                         }
279                                         transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
280                                         IPCMMGate responseGate = new PCMMGateReq();
281                                         responseGate.setTransactionID(transactionID);
282                                         siDataHashTable.put(key, new String(responseGate.getData()));
283                                 }
284                                 return siDataHashTable;
285                         }
286                 }
287
288                 @Override
289                 public Hashtable getClientData(COPSPepReqStateMan man) {
290                         // TODO Auto-generated method stub
291                         return new Hashtable<String, String>();
292                 }
293
294                 @Override
295                 public Hashtable getAcctData(COPSPepReqStateMan man) {
296                         // TODO Auto-generated method stub
297                         return new Hashtable<String, String>();
298                 }
299
300                 @Override
301                 public void notifyClosedConnection(COPSPepReqStateMan man, COPSError error) {
302
303                 }
304
305                 @Override
306                 public void notifyNoKAliveReceived(COPSPepReqStateMan man) {
307                         // TODO Auto-generated method stub
308
309                 }
310
311                 @Override
312                 public void closeRequestState(COPSPepReqStateMan man) {
313                         // TODO Auto-generated method stub
314
315                 }
316
317                 @Override
318                 public void newRequestState(COPSPepReqStateMan man) {
319                         // TODO Auto-generated method stub
320
321                 }
322
323                 public Hashtable<String, String> getRemoveDecs() {
324                         return removeDecs;
325                 }
326
327                 public void setRemoveDecs(Hashtable<String, String> removeDecs) {
328                         this.removeDecs = removeDecs;
329                 }
330
331                 public Hashtable<String, String> getInstallDecs() {
332                         return installDecs;
333                 }
334
335                 public void setInstallDecs(Hashtable<String, String> installDecs) {
336                         this.installDecs = installDecs;
337                 }
338
339                 public Hashtable<String, String> getErrorDecs() {
340                         return errorDecs;
341                 }
342
343                 public void setErrorDecs(Hashtable<String, String> errorDecs) {
344                         this.errorDecs = errorDecs;
345                 }
346
347                 public COPSPepReqStateMan getStateManager() {
348                         return stateManager;
349                 }
350
351                 public void setStateManager(COPSPepReqStateMan stateManager) {
352                         this.stateManager = stateManager;
353                 }
354         }
355 }