4 package org.pcmm.rcd.impl;
6 import java.net.Socket;
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.Properties;
10 import java.util.Vector;
11 import java.util.concurrent.Callable;
13 import org.pcmm.gates.IPCMMGate;
14 import org.pcmm.gates.ITransactionID;
15 import org.pcmm.gates.impl.PCMMGateReq;
16 import org.pcmm.messages.impl.MessageFactory;
17 import org.pcmm.rcd.ICMTS;
18 import org.umu.cops.prpep.COPSPepConnection;
19 import org.umu.cops.prpep.COPSPepDataProcess;
20 import org.umu.cops.prpep.COPSPepException;
21 import org.umu.cops.prpep.COPSPepReqStateMan;
22 import org.umu.cops.stack.COPSAcctTimer;
23 import org.umu.cops.stack.COPSClientAcceptMsg;
24 import org.umu.cops.stack.COPSClientCloseMsg;
25 import org.umu.cops.stack.COPSContext;
26 import org.umu.cops.stack.COPSData;
27 import org.umu.cops.stack.COPSDecision;
28 import org.umu.cops.stack.COPSDecisionMsg;
29 import org.umu.cops.stack.COPSError;
30 import org.umu.cops.stack.COPSException;
31 import org.umu.cops.stack.COPSHeader;
32 import org.umu.cops.stack.COPSKATimer;
33 import org.umu.cops.stack.COPSMsg;
34 import org.umu.cops.stack.COPSPrObjBase;
35 import org.umu.cops.stack.COPSReqMsg;
40 public class CMTS extends AbstractPCMMServer implements ICMTS {
47 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) {
49 return new AbstractPCMMClientHandler(socket) {
51 private String handle;
56 // set the major version info and minor version info to
58 logger.info("Send OPN message to the PS");
59 sendRequest(MessageFactory.getInstance().create(COPSHeader.COPS_OP_OPN, new Properties()));
61 COPSMsg recvMsg = readMessage();
63 if (recvMsg.getHeader().isAClientClose()) {
64 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvMsg;
65 logger.info("PS requested Client-Close" + cMsg.getError().getDescription());
66 // send a CC message and close the socket
70 if (recvMsg.getHeader().isAClientAccept()) {
71 logger.info("received Client-Accept from PS");
72 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvMsg;
74 if (cMsg.getIntegrity() != null) {
75 throw new COPSPepException("Unsupported object (Integrity)");
79 COPSKATimer kt = cMsg.getKATimer();
81 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
82 short kaTimeVal = kt.getTimerVal();
85 COPSAcctTimer at = cMsg.getAcctTimer();
88 acctTimer = at.getTimerVal();
90 logger.info("Send a REQ message to the PS");
92 Properties prop = new Properties();
93 COPSMsg reqMsg = MessageFactory.getInstance().create(COPSHeader.COPS_OP_REQ, prop);
94 handle = ((COPSReqMsg) reqMsg).getClientHandle().getId().str();
97 // Create the connection manager
98 PCMMCmtsConnection conn = new PCMMCmtsConnection(CLIENT_TYPE, socket);
99 // pcmm specific handler
100 // conn.addReqStateMgr(handle, new
101 // PCMMPSReqStateMan(CLIENT_TYPE, handle));
102 conn.addRequestState(handle, new CmtsDataProcessor());
103 conn.setKaTimer(kaTimeVal);
104 conn.setAcctTimer(acctTimer);
105 logger.info(getClass().getName() + " Thread(conn).start");
106 new Thread(conn).start();
108 // messages of other types are not expected
109 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
111 } catch (Exception e) {
112 logger.error(e.getMessage());
117 public void task(Callable<?> c) {
118 // TODO Auto-generated method stub
123 public void shouldWait(int t) {
124 // TODO Auto-generated method stub
130 // TODO Auto-generated method stub
137 /* public */class PCMMCmtsConnection extends COPSPepConnection {
139 public PCMMCmtsConnection(short clientType, Socket sock) {
140 super(clientType, sock);
143 public COPSPepReqStateMan addRequestState(String clientHandle, COPSPepDataProcess process)
144 throws COPSException, COPSPepException {
145 return super.addRequestState(clientHandle, process);
148 // public void addReqStateMgr(String hanlde, COPSPepReqStateMan r) {
149 // // map < String(COPSHandle), COPSPepReqStateMan>;
150 // getReqStateMans().put(hanlde, r);
154 @SuppressWarnings("rawtypes")
155 class PCMMPSReqStateMan extends COPSPepReqStateMan {
157 public PCMMPSReqStateMan(short clientType, String clientHandle) {
158 super(clientType, clientHandle);
159 _process = new CmtsDataProcessor();
164 protected void processDecision(COPSDecisionMsg dMsg)
165 throws COPSPepException {
167 // COPSHandle handle = dMsg.getClientHandle();
168 Hashtable decisions = dMsg.getDecisions();
170 Hashtable<String, String> removeDecs = new Hashtable<String, String>(10);
171 Hashtable<String, String> installDecs = new Hashtable<String, String>(10);
172 Hashtable<String, String> errorDecs = new Hashtable<String, String>(10);
173 for (Enumeration e = decisions.keys(); e.hasMoreElements();) {
175 COPSContext context = (COPSContext) e.nextElement();
176 Vector v = (Vector) decisions.get(context);
177 Enumeration ee = v.elements();
178 COPSDecision cmddecision = (COPSDecision) ee.nextElement();
180 // cmddecision --> we must check whether it is an error!
182 if (cmddecision.isInstallDecision()) {
183 String prid = new String();
184 for (; ee.hasMoreElements();) {
185 COPSDecision decision = (COPSDecision) ee.nextElement();
186 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
187 switch (obj.getSNum()) {
188 // TODO when there is install request only the PR_PRID
189 // is git but the ClientSI object containing the PR_EPD
190 // is null??? this is why the tests fail and so I set
191 // the assertion to NOT true....
192 case COPSPrObjBase.PR_PRID:
193 prid = obj.getData().str();
195 case COPSPrObjBase.PR_EPD:
196 installDecs.put(prid, obj.getData().str());
203 if (cmddecision.isRemoveDecision()) {
204 String prid = new String();
205 for (; ee.hasMoreElements();) {
206 COPSDecision decision = (COPSDecision) ee.nextElement();
207 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
208 switch (obj.getSNum()) {
209 case COPSPrObjBase.PR_PRID:
210 prid = obj.getData().str();
212 case COPSPrObjBase.PR_EPD:
213 removeDecs.put(prid, obj.getData().str());
221 if (_process != null) {
222 // ** Apply decisions to the configuration
223 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
225 if (_process.isFailReport(this)) {
226 // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
227 _sender.sendFailReport(_process.getReportData(this));
229 // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
230 _sender.sendSuccessReport(_process.getReportData(this));
237 @SuppressWarnings("rawtypes")
238 class CmtsDataProcessor extends COPSPepDataProcess {
240 private Hashtable<String, String> removeDecs;
241 private Hashtable<String, String> installDecs;
242 private Hashtable<String, String> errorDecs;
243 private COPSPepReqStateMan stateManager;
245 public CmtsDataProcessor() {
246 setRemoveDecs(new Hashtable<String, String>(10));
247 setInstallDecs(new Hashtable<String, String>(10));
248 setErrorDecs(new Hashtable<String, String>(10));
251 @SuppressWarnings("unchecked")
253 public void setDecisions(COPSPepReqStateMan man, Hashtable removeDecs, Hashtable installDecs, Hashtable errorDecs) {
254 setRemoveDecs(removeDecs);
255 setInstallDecs(installDecs);
256 setErrorDecs(errorDecs);
257 setStateManager(man);
261 public boolean isFailReport(COPSPepReqStateMan man) {
262 return (errorDecs != null && errorDecs.size() > 0);
266 public Hashtable getReportData(COPSPepReqStateMan man) {
267 if (isFailReport(man)) {
270 ITransactionID transactionID = null;
272 Hashtable<String, String> siDataHashTable = new Hashtable<String, String>();
273 if (installDecs.size() > 0) {
275 for (String k : installDecs.keySet()) {
276 data = installDecs.get(k);
279 transactionID = new PCMMGateReq(new COPSData(data).getData()).getTransactionID();
280 IPCMMGate responseGate = new PCMMGateReq();
281 responseGate.setTransactionID(transactionID);
282 siDataHashTable.put(key, new String(responseGate.getData()));
284 return siDataHashTable;
289 public Hashtable getClientData(COPSPepReqStateMan man) {
290 // TODO Auto-generated method stub
291 return new Hashtable<String, String>();
295 public Hashtable getAcctData(COPSPepReqStateMan man) {
296 // TODO Auto-generated method stub
297 return new Hashtable<String, String>();
301 public void notifyClosedConnection(COPSPepReqStateMan man, COPSError error) {
306 public void notifyNoKAliveReceived(COPSPepReqStateMan man) {
307 // TODO Auto-generated method stub
312 public void closeRequestState(COPSPepReqStateMan man) {
313 // TODO Auto-generated method stub
318 public void newRequestState(COPSPepReqStateMan man) {
319 // TODO Auto-generated method stub
323 public Hashtable<String, String> getRemoveDecs() {
327 public void setRemoveDecs(Hashtable<String, String> removeDecs) {
328 this.removeDecs = removeDecs;
331 public Hashtable<String, String> getInstallDecs() {
335 public void setInstallDecs(Hashtable<String, String> installDecs) {
336 this.installDecs = installDecs;
339 public Hashtable<String, String> getErrorDecs() {
343 public void setErrorDecs(Hashtable<String, String> errorDecs) {
344 this.errorDecs = errorDecs;
347 public COPSPepReqStateMan getStateManager() {
351 public void setStateManager(COPSPepReqStateMan stateManager) {
352 this.stateManager = stateManager;