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