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