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.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;
18 import java.net.Socket;
20 import java.util.concurrent.Callable;
25 public class CMTS extends AbstractPCMMServer implements ICMTS {
32 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
34 return new AbstractPCMMClientHandler(socket) {
36 private String handle;
41 // set the major version info and minor version info to
43 logger.info("Send OPN message to the PS");
44 sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
46 COPSMsg recvMsg = readMessage();
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
55 if (recvMsg.getHeader().getOpCode().equals(OPCode.CAT)) {
56 logger.info("received Client-Accept from PS");
57 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
59 if (cMsg.getIntegrity() != null) {
60 throw new COPSPepException("Unsupported object (Integrity)");
64 COPSKATimer kt = cMsg.getKATimer();
66 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
67 short kaTimeVal = kt.getTimerVal();
70 COPSAcctTimer at = cMsg.getAcctTimer();
73 acctTimer = at.getTimerVal();
75 logger.info("Send a REQ message to the PS");
77 Properties prop = new Properties();
78 COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
79 handle = ((COPSReqMsg) reqMsg).getClientHandle().getId().str();
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();
93 // messages of other types are not expected
94 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
96 } catch (Exception e) {
97 logger.error(e.getMessage());
102 public void task(Callable<?> c) {
103 // TODO Auto-generated method stub
108 public void shouldWait(int t) {
109 // TODO Auto-generated method stub
115 // TODO Auto-generated method stub
122 /* public */class PCMMCmtsConnection extends COPSPepConnection {
124 public PCMMCmtsConnection(final short clientType, final Socket sock) {
125 super(clientType, sock);
128 public COPSPepReqStateMan addRequestState(String clientHandle, COPSPepDataProcess process)
129 throws COPSException, COPSPepException {
130 return super.addRequestState(clientHandle, process);
133 // public void addReqStateMgr(String hanlde, COPSPepReqStateMan r) {
134 // // map < String(COPSHandle), COPSPepReqStateMan>;
135 // getReqStateMans().put(hanlde, r);
139 @SuppressWarnings("rawtypes")
140 class PCMMPSReqStateMan extends COPSPepReqStateMan {
142 public PCMMPSReqStateMan(final short clientType, final String clientHandle) {
143 super(clientType, clientHandle);
144 _process = new CmtsDataProcessor();
149 protected void processDecision(COPSDecisionMsg dMsg)
150 throws COPSPepException {
152 // COPSHandle handle = dMsg.getClientHandle();
153 Map<COPSContext, Set<COPSDecision>> decisions = dMsg.getDecisions();
155 Map<String, String> removeDecs = new HashMap<>();
156 Map<String, String> installDecs = new HashMap<>();
157 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 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 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);
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));
220 @SuppressWarnings("rawtypes")
221 class CmtsDataProcessor extends COPSPepDataProcess {
223 private Map<String, String> removeDecs;
224 private Map<String, String> installDecs;
225 private Map<String, String> errorDecs;
226 private COPSPepReqStateMan stateManager;
228 public CmtsDataProcessor() {
229 setRemoveDecs(new HashMap<String, String>());
230 setInstallDecs(new HashMap<String, String>());
231 setErrorDecs(new HashMap<String, String>());
234 @SuppressWarnings("unchecked")
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);
245 public boolean isFailReport(COPSPepReqStateMan man) {
246 return (errorDecs != null && errorDecs.size() > 0);
250 public Map<String, String> getReportData(COPSPepReqStateMan man) {
251 if (isFailReport(man)) {
254 Map<String, String> siDataHashTable = new HashMap<>();
255 if (installDecs.size() > 0) {
257 for (String k : installDecs.keySet()) {
258 data = installDecs.get(k);
261 final ITransactionID transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
262 IPCMMGate responseGate = new PCMMGateReq();
263 responseGate.setTransactionID(transactionID);
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()));
269 return siDataHashTable;
274 public Hashtable getClientData(COPSPepReqStateMan man) {
275 // TODO Auto-generated method stub
276 return new Hashtable<String, String>();
280 public Hashtable getAcctData(COPSPepReqStateMan man) {
281 // TODO Auto-generated method stub
282 return new Hashtable<String, String>();
286 public void notifyClosedConnection(COPSPepReqStateMan man, COPSError error) {
291 public void notifyNoKAliveReceived(COPSPepReqStateMan man) {
292 // TODO Auto-generated method stub
297 public void closeRequestState(COPSPepReqStateMan man) {
298 // TODO Auto-generated method stub
303 public void newRequestState(COPSPepReqStateMan man) {
304 // TODO Auto-generated method stub
308 public Map<String, String> getRemoveDecs() {
309 return new HashMap<>(removeDecs);
312 public void setRemoveDecs(final Map<String, String> removeDecs) {
313 this.removeDecs = new HashMap<>(removeDecs);
316 public Map<String, String> getInstallDecs() {
317 return new HashMap<>(installDecs);
320 public void setInstallDecs(final Map<String, String> installDecs) {
321 this.installDecs = new HashMap<>(installDecs);
324 public Map<String, String> getErrorDecs() {
328 public void setErrorDecs(final Map<String, String> errorDecs) {
329 this.errorDecs = new HashMap<>(errorDecs);
332 public COPSPepReqStateMan getStateManager() {
336 public void setStateManager(COPSPepReqStateMan stateManager) {
337 this.stateManager = stateManager;