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.COPSDecision.Command;
18 import java.net.Socket;
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21 import java.util.Properties;
22 import java.util.Vector;
23 import java.util.concurrent.Callable;
28 public class CMTS extends AbstractPCMMServer implements ICMTS {
35 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
37 return new AbstractPCMMClientHandler(socket) {
39 private String handle;
44 // set the major version info and minor version info to
46 logger.info("Send OPN message to the PS");
47 sendRequest(MessageFactory.getInstance().create(COPSHeader.COPS_OP_OPN, new Properties()));
49 COPSMsg recvMsg = readMessage();
51 if (recvMsg.getHeader().isAClientClose()) {
52 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvMsg;
53 logger.info("PS requested Client-Close" + cMsg.getError().getDescription());
54 // send a CC message and close the socket
58 if (recvMsg.getHeader().isAClientAccept()) {
59 logger.info("received Client-Accept from PS");
60 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
62 if (cMsg.getIntegrity() != null) {
63 throw new COPSPepException("Unsupported object (Integrity)");
67 COPSKATimer kt = cMsg.getKATimer();
69 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
70 short kaTimeVal = kt.getTimerVal();
73 COPSAcctTimer at = cMsg.getAcctTimer();
76 acctTimer = at.getTimerVal();
78 logger.info("Send a REQ message to the PS");
80 Properties prop = new Properties();
81 COPSMsg reqMsg = MessageFactory.getInstance().create(COPSHeader.COPS_OP_REQ, prop);
82 handle = ((COPSReqMsg) reqMsg).getClientHandle().getId().str();
85 // Create the connection manager
86 PCMMCmtsConnection conn = new PCMMCmtsConnection(CLIENT_TYPE, socket);
87 // pcmm specific handler
88 // conn.addReqStateMgr(handle, new
89 // PCMMPSReqStateMan(CLIENT_TYPE, handle));
90 conn.addRequestState(handle, new CmtsDataProcessor());
91 conn.setKaTimer(kaTimeVal);
92 conn.setAcctTimer(acctTimer);
93 logger.info(getClass().getName() + " Thread(conn).start");
94 new Thread(conn).start();
96 // messages of other types are not expected
97 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
99 } catch (Exception e) {
100 logger.error(e.getMessage());
105 public void task(Callable<?> c) {
106 // TODO Auto-generated method stub
111 public void shouldWait(int t) {
112 // TODO Auto-generated method stub
118 // TODO Auto-generated method stub
125 /* public */class PCMMCmtsConnection extends COPSPepConnection {
127 public PCMMCmtsConnection(short clientType, Socket sock) {
128 super(clientType, sock);
131 public COPSPepReqStateMan addRequestState(String clientHandle, COPSPepDataProcess process)
132 throws COPSException, COPSPepException {
133 return super.addRequestState(clientHandle, process);
136 // public void addReqStateMgr(String hanlde, COPSPepReqStateMan r) {
137 // // map < String(COPSHandle), COPSPepReqStateMan>;
138 // getReqStateMans().put(hanlde, r);
142 @SuppressWarnings("rawtypes")
143 class PCMMPSReqStateMan extends COPSPepReqStateMan {
145 public PCMMPSReqStateMan(short clientType, String clientHandle) {
146 super(clientType, clientHandle);
147 _process = new CmtsDataProcessor();
152 protected void processDecision(COPSDecisionMsg dMsg)
153 throws COPSPepException {
155 // COPSHandle handle = dMsg.getClientHandle();
156 Hashtable decisions = dMsg.getDecisions();
158 Hashtable<String, String> removeDecs = new Hashtable<String, String>(10);
159 Hashtable<String, String> installDecs = new Hashtable<String, String>(10);
160 Hashtable<String, String> errorDecs = new Hashtable<String, String>(10);
161 for (Enumeration e = decisions.keys(); e.hasMoreElements();) {
163 COPSContext context = (COPSContext) e.nextElement();
164 Vector v = (Vector) decisions.get(context);
165 Enumeration ee = v.elements();
166 COPSDecision cmddecision = (COPSDecision) ee.nextElement();
168 // cmddecision --> we must check whether it is an error!
170 if (cmddecision.getCommand().equals(Command.INSTALL)) {
172 for (; ee.hasMoreElements();) {
173 COPSDecision decision = (COPSDecision) ee.nextElement();
174 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
175 switch (obj.getSNum()) {
176 // TODO when there is install request only the PR_PRID
177 // is git but the ClientSI object containing the PR_EPD
178 // is null??? this is why the tests fail and so I set
179 // the assertion to NOT true....
180 case COPSPrObjBase.PR_PRID:
181 prid = obj.getData().str();
183 case COPSPrObjBase.PR_EPD:
184 installDecs.put(prid, obj.getData().str());
191 if (cmddecision.getCommand().equals(Command.REMOVE)) {
192 String prid = new String();
193 for (; ee.hasMoreElements();) {
194 COPSDecision decision = (COPSDecision) ee.nextElement();
195 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
196 switch (obj.getSNum()) {
197 case COPSPrObjBase.PR_PRID:
198 prid = obj.getData().str();
200 case COPSPrObjBase.PR_EPD:
201 removeDecs.put(prid, obj.getData().str());
209 if (_process != null) {
210 // ** Apply decisions to the configuration
211 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
213 if (_process.isFailReport(this)) {
214 // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
215 _sender.sendFailReport(_process.getReportData(this));
217 // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
218 _sender.sendSuccessReport(_process.getReportData(this));
225 @SuppressWarnings("rawtypes")
226 class CmtsDataProcessor extends COPSPepDataProcess {
228 private Hashtable<String, String> removeDecs;
229 private Hashtable<String, String> installDecs;
230 private Hashtable<String, String> errorDecs;
231 private COPSPepReqStateMan stateManager;
233 public CmtsDataProcessor() {
234 setRemoveDecs(new Hashtable<String, String>(10));
235 setInstallDecs(new Hashtable<String, String>(10));
236 setErrorDecs(new Hashtable<String, String>(10));
239 @SuppressWarnings("unchecked")
241 public void setDecisions(COPSPepReqStateMan man, Hashtable removeDecs, Hashtable installDecs, Hashtable errorDecs) {
242 setRemoveDecs(removeDecs);
243 setInstallDecs(installDecs);
244 setErrorDecs(errorDecs);
245 setStateManager(man);
249 public boolean isFailReport(COPSPepReqStateMan man) {
250 return (errorDecs != null && errorDecs.size() > 0);
254 public Hashtable getReportData(COPSPepReqStateMan man) {
255 if (isFailReport(man)) {
258 ITransactionID transactionID = null;
260 Hashtable<String, String> siDataHashTable = new Hashtable<String, String>();
261 if (installDecs.size() > 0) {
263 for (String k : installDecs.keySet()) {
264 data = installDecs.get(k);
267 transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
268 IPCMMGate responseGate = new PCMMGateReq();
269 responseGate.setTransactionID(transactionID);
270 siDataHashTable.put(key, new String(responseGate.getData()));
272 return siDataHashTable;
277 public Hashtable getClientData(COPSPepReqStateMan man) {
278 // TODO Auto-generated method stub
279 return new Hashtable<String, String>();
283 public Hashtable getAcctData(COPSPepReqStateMan man) {
284 // TODO Auto-generated method stub
285 return new Hashtable<String, String>();
289 public void notifyClosedConnection(COPSPepReqStateMan man, COPSError error) {
294 public void notifyNoKAliveReceived(COPSPepReqStateMan man) {
295 // TODO Auto-generated method stub
300 public void closeRequestState(COPSPepReqStateMan man) {
301 // TODO Auto-generated method stub
306 public void newRequestState(COPSPepReqStateMan man) {
307 // TODO Auto-generated method stub
311 public Hashtable<String, String> getRemoveDecs() {
315 public void setRemoveDecs(Hashtable<String, String> removeDecs) {
316 this.removeDecs = removeDecs;
319 public Hashtable<String, String> getInstallDecs() {
323 public void setInstallDecs(Hashtable<String, String> installDecs) {
324 this.installDecs = installDecs;
327 public Hashtable<String, String> getErrorDecs() {
331 public void setErrorDecs(Hashtable<String, String> errorDecs) {
332 this.errorDecs = errorDecs;
335 public COPSPepReqStateMan getStateManager() {
339 public void setStateManager(COPSPepReqStateMan stateManager) {
340 this.stateManager = stateManager;