2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.rcd.impl;
7 import org.pcmm.gates.IGateSpec.Direction;
8 import org.pcmm.messages.impl.MessageFactory;
9 import org.pcmm.rcd.IPCMMServer.IPCMMClientHandler;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.umu.cops.prpep.COPSPepException;
13 import org.umu.cops.stack.*;
14 import org.umu.cops.stack.COPSHeader.OPCode;
16 import java.net.Socket;
17 import java.util.Collections;
19 import java.util.Properties;
21 import java.util.concurrent.Callable;
24 * This class was created by moving an anonymous inner class from CMTS.java and is responsible for creating a persistent
25 * connection with a PEP.
27 public class CmtsPcmmClientHandler extends AbstractPCMMClient implements IPCMMClientHandler {
29 private final static Logger logger = LoggerFactory.getLogger(CmtsPcmmClientHandler.class);
32 * The thread accepting PEP COPS messages
34 private transient Thread thread;
37 * The configured gates
39 private final Map<Direction, Set<String>> gateConfig;
42 * The connected cable modems and whether or not they are up
44 private final Map<String, Boolean> cmStatus;
47 * Constructor when a socket connection has not been established
48 * @param host - the host to connect
49 * @param port - the port to connect
50 * @param gateConfig - the configured gates
51 * @param cmStatus - the configured cable modem and their state
53 public CmtsPcmmClientHandler(final String host, final int port, final Map<Direction, Set<String>> gateConfig,
54 final Map<String, Boolean> cmStatus) {
56 this.gateConfig = Collections.unmodifiableMap(gateConfig);
57 this.cmStatus = Collections.unmodifiableMap(cmStatus);
61 * Constructor with a connected socket.
62 * @param socket - the socket connection
63 * @param gateConfig - the configured gates
64 * @param cmStatus - the configured cable modem and their state
66 public CmtsPcmmClientHandler(final Socket socket, final Map<Direction, Set<String>> gateConfig,
67 final Map<String, Boolean> cmStatus) {
69 this.gateConfig = Collections.unmodifiableMap(gateConfig);
70 this.cmStatus = Collections.unmodifiableMap(cmStatus);
74 if (thread != null && thread.isAlive())
81 logger.info("Send OPN message to the PS");
82 sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
85 final COPSMsg recvMsg = readMessage();
87 switch (recvMsg.getHeader().getOpCode()) {
89 final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) recvMsg;
90 logger.info("PS requested Client-Close" + closeMsg.getError().getDescription());
91 // send a CC message and close the socket
95 logger.info("received Client-Accept from PS");
96 final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
98 if (acceptMsg.getIntegrity() != null) {
99 throw new COPSPepException("Unsupported object (Integrity)");
103 final COPSKATimer kt = acceptMsg.getKATimer();
105 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
106 short kaTimeVal = kt.getTimerVal();
109 final COPSAcctTimer at = acceptMsg.getAcctTimer();
112 acctTimer = at.getTimerVal();
114 logger.info("Send a REQ message to the PS");
115 final Properties prop = new Properties();
116 final COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
117 final COPSHandle handle = ((COPSReqMsg) reqMsg).getClientHandle();
120 // Create the connection manager
121 final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), gateConfig,
123 conn.addRequestState(handle, new CmtsDataProcessor());
124 conn.setKaTimer(kaTimeVal);
125 conn.setAcctTimer(acctTimer);
127 logger.info(getClass().getName() + " Thread(conn).start");
128 thread = new Thread(conn);
132 throw new COPSPepException("Message not expected. Closing connection for " + getSocket().toString());
134 } catch (Exception e) {
135 logger.error(e.getMessage());
140 public void task(Callable<?> c) {
141 // TODO Auto-generated method stub
146 public void shouldWait(int t) {
147 // TODO Auto-generated method stub
153 // TODO Auto-generated method stub