Cleanup of state managers' interfaces (constructor and init).
[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.COPSStateMan;
12 import org.umu.cops.prpep.COPSPepConnection;
13 import org.umu.cops.prpep.COPSPepDataProcess;
14 import org.umu.cops.prpep.COPSPepException;
15 import org.umu.cops.prpep.COPSPepReqStateMan;
16 import org.umu.cops.stack.*;
17 import org.umu.cops.stack.COPSHeader.OPCode;
18
19 import java.net.Socket;
20 import java.util.*;
21 import java.util.concurrent.Callable;
22
23 /**
24  * This class starts a mock CMTS that can be used for testing.
25  */
26 public class CMTS extends AbstractPCMMServer implements ICMTS {
27
28         /**
29          * Constructor for having the server port automatically assigned
30          * Call getPort() after startServer() is called to determine the port number of the server
31          */
32         public CMTS() {
33                 this(0);
34         }
35
36         /**
37          * Constructor for starting the server to a pre-defined port number
38          * @param port - the port number on which to start the server.
39          */
40         public CMTS(final int port) {
41                 super(port);
42         }
43
44         @Override
45         protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
46
47                 return new AbstractPCMMClientHandler(socket) {
48
49                         private COPSHandle handle;
50
51                         public void run() {
52                                 try {
53                                         // send OPN message
54                                         // set the major version info and minor version info to
55                                         // default (5,0)
56                                         logger.info("Send OPN message to the PS");
57                                         sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
58                                         // wait for CAT
59                                         COPSMsg recvMsg = readMessage();
60
61                                         if (recvMsg.getHeader().getOpCode().equals(OPCode.CC)) {
62                                                 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvMsg;
63                                                 logger.info("PS requested Client-Close" + cMsg.getError().getDescription());
64                                                 // send a CC message and close the socket
65                                                 disconnect();
66                                                 return;
67                                         }
68                                         if (recvMsg.getHeader().getOpCode().equals(OPCode.CAT)) {
69                                                 logger.info("received Client-Accept from PS");
70                                                 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
71                                                 // Support
72                                                 if (cMsg.getIntegrity() != null) {
73                                                         throw new COPSPepException("Unsupported object (Integrity)");
74                                                 }
75
76                                                 // Mandatory KATimer
77                                                 COPSKATimer kt = cMsg.getKATimer();
78                                                 if (kt == null)
79                                                         throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
80                                                 short kaTimeVal = kt.getTimerVal();
81
82                                                 // ACTimer
83                                                 COPSAcctTimer at = cMsg.getAcctTimer();
84                                                 short acctTimer = 0;
85                                                 if (at != null)
86                                                         acctTimer = at.getTimerVal();
87
88                                                 logger.info("Send a REQ message to the PS");
89                                                 {
90                                                         Properties prop = new Properties();
91                                                         COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
92                                                         handle = ((COPSReqMsg) reqMsg).getClientHandle();
93                                                         sendRequest(reqMsg);
94                                                 }
95                                                 // Create the connection manager
96                                                 final PCMMCmtsConnection conn = new PCMMCmtsConnection(CLIENT_TYPE, socket);
97                                                 // pcmm specific handler
98                                                 // conn.addReqStateMgr(handle, new
99                                                 // PCMMPSReqStateMan(CLIENT_TYPE, handle));
100                                                 conn.addRequestState(handle, new CmtsDataProcessor());
101                                                 conn.setKaTimer(kaTimeVal);
102                                                 conn.setAcctTimer(acctTimer);
103                                                 logger.info(getClass().getName() + " Thread(conn).start");
104                                                 new Thread(conn).start();
105                                         } else {
106                                                 // messages of other types are not expected
107                                                 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
108                                         }
109                                 } catch (Exception e) {
110                                         logger.error(e.getMessage());
111                                 }
112                         }
113
114                         @Override
115                         public void task(Callable<?> c) {
116                                 // TODO Auto-generated method stub
117
118                         }
119
120                         @Override
121                         public void shouldWait(int t) {
122                                 // TODO Auto-generated method stub
123
124                         }
125
126                         @Override
127                         public void done() {
128                                 // TODO Auto-generated method stub
129
130                         }
131
132                 };
133         }
134
135         class PCMMCmtsConnection extends COPSPepConnection {
136
137                 public PCMMCmtsConnection(final short clientType, final Socket sock) {
138                         super(clientType, sock);
139                 }
140
141                 public COPSPepReqStateMan addRequestState(final COPSHandle clientHandle, final COPSPepDataProcess process)
142                                 throws COPSException {
143                         return super.addRequestState(clientHandle, process);
144                 }
145         }
146
147         class CmtsDataProcessor implements COPSPepDataProcess {
148
149                 private Map<String, String> removeDecs;
150                 private Map<String, String> installDecs;
151                 private Map<String, String> errorDecs;
152                 private COPSPepReqStateMan stateManager;
153
154                 public CmtsDataProcessor() {
155                         setRemoveDecs(new HashMap<String, String>());
156                         setInstallDecs(new HashMap<String, String>());
157                         setErrorDecs(new HashMap<String, String>());
158                 }
159
160                 @Override
161                 public void setDecisions(final COPSPepReqStateMan man, final Map<String, String> removeDecs,
162                                  final Map<String, String> installDecs, final Map<String, String> errorDecs) {
163                         setRemoveDecs(removeDecs);
164                         setInstallDecs(installDecs);
165                         setErrorDecs(errorDecs);
166                         setStateManager(man);
167                 }
168
169                 @Override
170                 public boolean isFailReport(final COPSPepReqStateMan man) {
171                         return (errorDecs != null && errorDecs.size() > 0);
172                 }
173
174                 @Override
175                 public Map<String, String> getReportData(final COPSPepReqStateMan man) {
176                         if (isFailReport(man)) {
177                                 return errorDecs;
178                         } else {
179                                 final Map<String, String> siDataHashTable = new HashMap<>();
180                                 if (installDecs.size() > 0) {
181                                         String data = "";
182                                         for (String k : installDecs.keySet()) {
183                                                 data = installDecs.get(k);
184                                                 break;
185                                         }
186                                         final ITransactionID transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
187                                         final IPCMMGate responseGate = new PCMMGateReq();
188                                         responseGate.setTransactionID(transactionID);
189
190                     // TODO FIXME - Why is the key always null??? What value should be used here???
191                     final String key = null;
192                                         siDataHashTable.put(key, new String(responseGate.getData()));
193                                 }
194                                 return siDataHashTable;
195                         }
196                 }
197
198                 @Override
199                 public Map<String, String> getClientData(COPSPepReqStateMan man) {
200                         // TODO Auto-generated method stub
201                         return new HashMap<>();
202                 }
203
204                 @Override
205                 public Map<String, String> getAcctData(COPSPepReqStateMan man) {
206                         // TODO Auto-generated method stub
207                         return new HashMap<>();
208                 }
209
210                 @Override
211                 public void notifyClosedConnection(final COPSStateMan man, final COPSError error) {
212                         // TODO Auto-generated method stub
213                 }
214
215                 @Override
216                 public void notifyNoKAliveReceived(final COPSStateMan man) {
217                         // TODO Auto-generated method stub
218                 }
219
220                 @Override
221                 public void closeRequestState(final COPSStateMan man) {
222                         // TODO Auto-generated method stub
223                 }
224
225                 @Override
226                 public void newRequestState(final COPSPepReqStateMan man) {
227                         // TODO Auto-generated method stub
228                 }
229
230                 public Map<String, String> getRemoveDecs() {
231                         return new HashMap<>(removeDecs);
232                 }
233
234                 public void setRemoveDecs(final Map<String, String> removeDecs) {
235                         this.removeDecs = new HashMap<>(removeDecs);
236                 }
237
238                 public Map<String, String> getInstallDecs() {
239                         return new HashMap<>(installDecs);
240                 }
241
242                 public void setInstallDecs(final Map<String, String> installDecs) {
243                         this.installDecs = new HashMap<>(installDecs);
244                 }
245
246                 public Map<String, String> getErrorDecs() {
247                         return errorDecs;
248                 }
249
250                 public void setErrorDecs(final Map<String, String> errorDecs) {
251                         this.errorDecs = new HashMap<>(errorDecs);
252                 }
253
254                 public COPSPepReqStateMan getStateManager() {
255                         return stateManager;
256                 }
257
258                 public void setStateManager(COPSPepReqStateMan stateManager) {
259                         this.stateManager = stateManager;
260                 }
261
262         }
263 }