4 package org.pcmm.rcd.impl;
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;
19 import java.net.Socket;
21 import java.util.concurrent.Callable;
24 * This class starts a mock CMTS that can be used for testing.
26 public class CMTS extends AbstractPCMMServer implements ICMTS {
29 * Constructor for having the server port automatically assigned
30 * Call getPort() after startServer() is called to determine the port number of the server
37 * Constructor for starting the server to a pre-defined port number
38 * @param port - the port number on which to start the server.
40 public CMTS(final int port) {
45 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
47 return new AbstractPCMMClientHandler(socket) {
49 private COPSHandle handle;
54 // set the major version info and minor version info to
56 logger.info("Send OPN message to the PS");
57 sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
59 COPSMsg recvMsg = readMessage();
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
68 if (recvMsg.getHeader().getOpCode().equals(OPCode.CAT)) {
69 logger.info("received Client-Accept from PS");
70 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
72 if (cMsg.getIntegrity() != null) {
73 throw new COPSPepException("Unsupported object (Integrity)");
77 COPSKATimer kt = cMsg.getKATimer();
79 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
80 short kaTimeVal = kt.getTimerVal();
83 COPSAcctTimer at = cMsg.getAcctTimer();
86 acctTimer = at.getTimerVal();
88 logger.info("Send a REQ message to the PS");
90 Properties prop = new Properties();
91 COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
92 handle = ((COPSReqMsg) reqMsg).getClientHandle();
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();
106 // messages of other types are not expected
107 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
109 } catch (Exception e) {
110 logger.error(e.getMessage());
115 public void task(Callable<?> c) {
116 // TODO Auto-generated method stub
121 public void shouldWait(int t) {
122 // TODO Auto-generated method stub
128 // TODO Auto-generated method stub
135 class PCMMCmtsConnection extends COPSPepConnection {
137 public PCMMCmtsConnection(final short clientType, final Socket sock) {
138 super(clientType, sock);
141 public COPSPepReqStateMan addRequestState(final COPSHandle clientHandle, final COPSPepDataProcess process)
142 throws COPSException {
143 return super.addRequestState(clientHandle, process);
147 class PCMMPSReqStateMan extends COPSPepReqStateMan {
149 public PCMMPSReqStateMan(final short clientType, final COPSHandle clientHandle) {
150 super(clientType, clientHandle, new CmtsDataProcessor());
154 protected void processDecision(final COPSDecisionMsg dMsg, final Socket socket) throws COPSPepException {
155 final Map<String, String> removeDecs = new HashMap<>();
156 final Map<String, String> installDecs = new HashMap<>();
157 final Map<String, String> errorDecs = new HashMap<>();
159 for (final Set<COPSDecision> copsDecisions : dMsg.getDecisions().values()) {
160 final COPSDecision cmddecision = copsDecisions.iterator().next();
162 // cmddecision --> we must check whether it is an error!
164 switch (cmddecision.getCommand()) {
166 for (final COPSDecision decision : copsDecisions) {
167 final 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();
176 case COPSPrObjBase.PR_EPD:
177 installDecs.put(prid, obj.getData().str());
184 for (final COPSDecision decision : copsDecisions) {
185 final 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();
194 case COPSPrObjBase.PR_EPD:
195 removeDecs.put(prid, obj.getData().str());
204 if (_process != null) {
205 // ** Apply decisions to the configuration
206 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
207 _status = Status.ST_DECS;
208 if (_process.isFailReport(this)) {
209 // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
210 _sender.sendFailReport(_process.getReportData(this));
212 // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
213 _sender.sendSuccessReport(_process.getReportData(this));
215 _status = Status.ST_REPORT;
220 class CmtsDataProcessor implements COPSPepDataProcess {
222 private Map<String, String> removeDecs;
223 private Map<String, String> installDecs;
224 private Map<String, String> errorDecs;
225 private COPSPepReqStateMan stateManager;
227 public CmtsDataProcessor() {
228 setRemoveDecs(new HashMap<String, String>());
229 setInstallDecs(new HashMap<String, String>());
230 setErrorDecs(new HashMap<String, String>());
234 public void setDecisions(final COPSPepReqStateMan man, final Map<String, String> removeDecs,
235 final Map<String, String> installDecs, final Map<String, String> errorDecs) {
236 setRemoveDecs(removeDecs);
237 setInstallDecs(installDecs);
238 setErrorDecs(errorDecs);
239 setStateManager(man);
243 public boolean isFailReport(final COPSPepReqStateMan man) {
244 return (errorDecs != null && errorDecs.size() > 0);
248 public Map<String, String> getReportData(final COPSPepReqStateMan man) {
249 if (isFailReport(man)) {
252 final Map<String, String> siDataHashTable = new HashMap<>();
253 if (installDecs.size() > 0) {
255 for (String k : installDecs.keySet()) {
256 data = installDecs.get(k);
259 final ITransactionID transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
260 final IPCMMGate responseGate = new PCMMGateReq();
261 responseGate.setTransactionID(transactionID);
263 // TODO FIXME - Why is the key always null??? What value should be used here???
264 final String key = null;
265 siDataHashTable.put(key, new String(responseGate.getData()));
267 return siDataHashTable;
272 public Map<String, String> getClientData(COPSPepReqStateMan man) {
273 // TODO Auto-generated method stub
274 return new HashMap<>();
278 public Map<String, String> getAcctData(COPSPepReqStateMan man) {
279 // TODO Auto-generated method stub
280 return new HashMap<>();
284 public void notifyClosedConnection(final COPSStateMan man, final COPSError error) {
285 // TODO Auto-generated method stub
289 public void notifyNoKAliveReceived(final COPSStateMan man) {
290 // TODO Auto-generated method stub
294 public void closeRequestState(final COPSStateMan man) {
295 // TODO Auto-generated method stub
299 public void newRequestState(final COPSPepReqStateMan man) {
300 // TODO Auto-generated method stub
303 public Map<String, String> getRemoveDecs() {
304 return new HashMap<>(removeDecs);
307 public void setRemoveDecs(final Map<String, String> removeDecs) {
308 this.removeDecs = new HashMap<>(removeDecs);
311 public Map<String, String> getInstallDecs() {
312 return new HashMap<>(installDecs);
315 public void setInstallDecs(final Map<String, String> installDecs) {
316 this.installDecs = new HashMap<>(installDecs);
319 public Map<String, String> getErrorDecs() {
323 public void setErrorDecs(final Map<String, String> errorDecs) {
324 this.errorDecs = new HashMap<>(errorDecs);
327 public COPSPepReqStateMan getStateManager() {
331 public void setStateManager(COPSPepReqStateMan stateManager) {
332 this.stateManager = stateManager;