2 * Copyright (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.pcmm.rcd.impl;
11 import org.pcmm.gates.IGateSpec.Direction;
12 import org.pcmm.messages.impl.MessageFactory;
13 import org.pcmm.rcd.IPCMMServer.IPCMMClientHandler;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.umu.cops.prpep.COPSPepException;
17 import org.umu.cops.stack.*;
18 import org.umu.cops.stack.COPSHeader.OPCode;
20 import java.net.Socket;
21 import java.util.Collections;
23 import java.util.Properties;
25 import java.util.concurrent.Callable;
28 * This class was created by moving an anonymous inner class from CMTS.java and is responsible for creating a persistent
29 * connection with a PEP.
31 public class CmtsPcmmClientHandler extends AbstractPCMMClient implements IPCMMClientHandler {
33 private final static Logger logger = LoggerFactory.getLogger(CmtsPcmmClientHandler.class);
36 * The thread accepting PEP COPS messages
38 private transient Thread thread;
41 * The configured gates
43 private final Map<Direction, Set<String>> gateConfig;
46 * The connected cable modems and whether or not they are up
48 private final Map<String, Boolean> cmStatus;
51 * Constructor when a socket connection has not been established
52 * @param host - the host to connect
53 * @param port - the port to connect
54 * @param gateConfig - the configured gates
55 * @param cmStatus - the configured cable modem and their state
57 public CmtsPcmmClientHandler(final String host, final int port, final Map<Direction, Set<String>> gateConfig,
58 final Map<String, Boolean> cmStatus) {
60 this.gateConfig = Collections.unmodifiableMap(gateConfig);
61 this.cmStatus = Collections.unmodifiableMap(cmStatus);
65 * Constructor with a connected socket.
66 * @param socket - the socket connection
67 * @param gateConfig - the configured gates
68 * @param cmStatus - the configured cable modem and their state
70 public CmtsPcmmClientHandler(final Socket socket, final Map<Direction, Set<String>> gateConfig,
71 final Map<String, Boolean> cmStatus) {
73 this.gateConfig = Collections.unmodifiableMap(gateConfig);
74 this.cmStatus = Collections.unmodifiableMap(cmStatus);
78 if (thread != null && thread.isAlive())
85 logger.info("Send OPN message to the PS");
86 sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
89 final COPSMsg recvMsg = readMessage();
91 switch (recvMsg.getHeader().getOpCode()) {
93 final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) recvMsg;
94 logger.info("PS requested Client-Close" + closeMsg.getError().getDescription());
95 // send a CC message and close the socket
99 logger.info("received Client-Accept from PS");
100 final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
102 if (acceptMsg.getIntegrity() != null) {
103 throw new COPSPepException("Unsupported object (Integrity)");
107 final COPSKATimer kt = acceptMsg.getKATimer();
109 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
110 short kaTimeVal = kt.getTimerVal();
113 final COPSAcctTimer at = acceptMsg.getAcctTimer();
116 acctTimer = at.getTimerVal();
118 logger.info("Send a REQ message to the PS");
119 final Properties prop = new Properties();
120 final COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
121 final COPSHandle handle = ((COPSReqMsg) reqMsg).getClientHandle();
124 // Create the connection manager
125 final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), gateConfig,
127 conn.addRequestState(handle, new CmtsDataProcessor());
128 conn.setKaTimer(kaTimeVal);
129 conn.setAcctTimer(acctTimer);
131 logger.info(getClass().getName() + " Thread(conn).start");
132 thread = new Thread(conn);
136 throw new COPSPepException("Message not expected. Closing connection for " + getSocket().toString());
138 } catch (Exception e) {
139 logger.error(e.getMessage());
144 public void task(Callable<?> c) {
145 // TODO Auto-generated method stub
150 public void shouldWait(int t) {
151 // TODO Auto-generated method stub
157 // TODO Auto-generated method stub