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 CmtsDataProcessor implements COPSPepDataProcess {
149 private Map<String, String> removeDecs;
150 private Map<String, String> installDecs;
151 private Map<String, String> errorDecs;
152 private COPSPepReqStateMan stateManager;
154 public CmtsDataProcessor() {
155 setRemoveDecs(new HashMap<String, String>());
156 setInstallDecs(new HashMap<String, String>());
157 setErrorDecs(new HashMap<String, String>());
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);
170 public boolean isFailReport(final COPSPepReqStateMan man) {
171 return (errorDecs != null && errorDecs.size() > 0);
175 public Map<String, String> getReportData(final COPSPepReqStateMan man) {
176 if (isFailReport(man)) {
179 final Map<String, String> siDataHashTable = new HashMap<>();
180 if (installDecs.size() > 0) {
182 for (String k : installDecs.keySet()) {
183 data = installDecs.get(k);
186 final ITransactionID transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
187 final IPCMMGate responseGate = new PCMMGateReq();
188 responseGate.setTransactionID(transactionID);
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()));
194 return siDataHashTable;
199 public Map<String, String> getClientData(COPSPepReqStateMan man) {
200 // TODO Auto-generated method stub
201 return new HashMap<>();
205 public Map<String, String> getAcctData(COPSPepReqStateMan man) {
206 // TODO Auto-generated method stub
207 return new HashMap<>();
211 public void notifyClosedConnection(final COPSStateMan man, final COPSError error) {
212 // TODO Auto-generated method stub
216 public void notifyNoKAliveReceived(final COPSStateMan man) {
217 // TODO Auto-generated method stub
221 public void closeRequestState(final COPSStateMan man) {
222 // TODO Auto-generated method stub
226 public void newRequestState(final COPSPepReqStateMan man) {
227 // TODO Auto-generated method stub
230 public Map<String, String> getRemoveDecs() {
231 return new HashMap<>(removeDecs);
234 public void setRemoveDecs(final Map<String, String> removeDecs) {
235 this.removeDecs = new HashMap<>(removeDecs);
238 public Map<String, String> getInstallDecs() {
239 return new HashMap<>(installDecs);
242 public void setInstallDecs(final Map<String, String> installDecs) {
243 this.installDecs = new HashMap<>(installDecs);
246 public Map<String, String> getErrorDecs() {
250 public void setErrorDecs(final Map<String, String> errorDecs) {
251 this.errorDecs = new HashMap<>(errorDecs);
254 public COPSPepReqStateMan getStateManager() {
258 public void setStateManager(COPSPepReqStateMan stateManager) {
259 this.stateManager = stateManager;