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