f2e549a23f6ae6e2c7e52cb337a478df998adfb5
[packetcable.git] / packetcable-emulator / src / main / java / org / pcmm / rcd / impl / CmtsPcmmClientHandler.java
1 /*
2  * Copyright (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  *
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
7  */
8
9 package org.pcmm.rcd.impl;
10
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;
19
20 import java.net.Socket;
21 import java.util.Collections;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.concurrent.Callable;
26
27 /**
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.
30  */
31 public class CmtsPcmmClientHandler extends AbstractPCMMClient implements IPCMMClientHandler {
32
33     private final static Logger logger = LoggerFactory.getLogger(CmtsPcmmClientHandler.class);
34
35     /**
36      * The thread accepting PEP COPS messages
37      */
38     private transient Thread thread;
39
40     /**
41      * The configured gates
42      */
43     private final Map<Direction, Set<String>> gateConfig;
44
45     /**
46      * The connected cable modems and whether or not they are up
47      */
48     private final Map<String, Boolean> cmStatus;
49
50     /**
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
56      */
57     public CmtsPcmmClientHandler(final String host, final int port, final Map<Direction, Set<String>> gateConfig,
58                                  final Map<String, Boolean> cmStatus) {
59         super(host, port);
60         this.gateConfig = Collections.unmodifiableMap(gateConfig);
61         this.cmStatus = Collections.unmodifiableMap(cmStatus);
62     }
63
64     /**
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
69      */
70     public CmtsPcmmClientHandler(final Socket socket, final Map<Direction, Set<String>> gateConfig,
71                                  final Map<String, Boolean> cmStatus) {
72         super(socket);
73         this.gateConfig = Collections.unmodifiableMap(gateConfig);
74         this.cmStatus = Collections.unmodifiableMap(cmStatus);
75     }
76
77     public void stop() {
78         if (thread != null && thread.isAlive())
79             thread.interrupt();
80     }
81
82     @Override
83     public void run() {
84         try {
85             logger.info("Send OPN message to the PS");
86             sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
87
88             // wait for CAT
89             final COPSMsg recvMsg = readMessage();
90
91             switch (recvMsg.getHeader().getOpCode()) {
92                 case CC:
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
96                     disconnect();
97                     break;
98                 case CAT:
99                     logger.info("received Client-Accept from PS");
100                     final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
101                     // Support
102                     if (acceptMsg.getIntegrity() != null) {
103                         throw new COPSPepException("Unsupported object (Integrity)");
104                     }
105
106                     // Mandatory KATimer
107                     final COPSKATimer kt = acceptMsg.getKATimer();
108                     if (kt == null)
109                         throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
110                     short kaTimeVal = kt.getTimerVal();
111
112                     // ACTimer
113                     final COPSAcctTimer at = acceptMsg.getAcctTimer();
114                     short acctTimer = 0;
115                     if (at != null)
116                         acctTimer = at.getTimerVal();
117
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();
122                     sendRequest(reqMsg);
123
124                     // Create the connection manager
125                     final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), gateConfig,
126                             cmStatus);
127                     conn.addRequestState(handle, new CmtsDataProcessor());
128                     conn.setKaTimer(kaTimeVal);
129                     conn.setAcctTimer(acctTimer);
130
131                     logger.info(getClass().getName() + " Thread(conn).start");
132                     thread = new Thread(conn);
133                     thread.start();
134                     break;
135                 default:
136                     throw new COPSPepException("Message not expected. Closing connection for " + getSocket().toString());
137             }
138         } catch (Exception e) {
139             logger.error(e.getMessage());
140         }
141     }
142
143     @Override
144     public void task(Callable<?> c) {
145         // TODO Auto-generated method stub
146
147     }
148
149     @Override
150     public void shouldWait(int t) {
151         // TODO Auto-generated method stub
152
153     }
154
155     @Override
156     public void done() {
157         // TODO Auto-generated method stub
158
159     }
160
161 }