Bump to odlparent 3.1.0 and yangtools 2.0.3
[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 static com.google.common.base.Preconditions.checkNotNull;
12
13 import org.pcmm.gates.IGateSpec.Direction;
14 import org.pcmm.messages.impl.MessageFactory;
15 import org.pcmm.rcd.IPCMMServer.IPCMMClientHandler;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.umu.cops.prpep.COPSPepException;
19 import org.umu.cops.stack.*;
20 import org.umu.cops.stack.COPSHeader.OPCode;
21
22 import java.net.Socket;
23 import java.util.Collections;
24 import java.util.Map;
25 import java.util.Properties;
26 import java.util.Set;
27 import java.util.concurrent.Callable;
28
29 /**
30  * This class was created by moving an anonymous inner class from CMTS.java and is responsible for creating a persistent
31  * connection with a PEP.
32  */
33 public class CmtsPcmmClientHandler extends AbstractPCMMClient implements IPCMMClientHandler {
34
35     private static final Logger logger = LoggerFactory.getLogger(CmtsPcmmClientHandler.class);
36
37     /**
38      * The thread accepting PEP COPS messages
39      */
40     private transient Thread thread;
41
42     /**
43      * Emulator configuration
44      */
45     private final CMTSConfig config;
46
47     /**
48      * Constructor when a socket connection has not been established
49      * @param host - the host to connect
50      * @param port - the port to connect
51      * @param config - emulator configuration
52      */
53     public CmtsPcmmClientHandler(final String host, final int port, final CMTSConfig config) {
54         super(host, port);
55         this.config = checkNotNull(config);
56     }
57
58     /**
59      * Constructor with a connected socket.
60      * @param socket - the socket connection
61      * @param config - emulator configuration
62      */
63     public CmtsPcmmClientHandler(final Socket socket, final CMTSConfig config) {
64         super(socket);
65         this.config = checkNotNull(config);
66     }
67
68     public void stop() {
69         if (thread != null && thread.isAlive())
70             thread.interrupt();
71     }
72
73     @Override
74     public void run() {
75         try {
76             logger.info("Send OPN message to the PS");
77             sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
78
79             // wait for CAT
80             final COPSMsg recvMsg = readMessage();
81
82             switch (recvMsg.getHeader().getOpCode()) {
83                 case CC:
84                     final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) recvMsg;
85                     logger.info("PS requested Client-Close" + closeMsg.getError().getDescription());
86                     // send a CC message and close the socket
87                     disconnect();
88                     break;
89                 case CAT:
90                     logger.info("received Client-Accept from PS");
91                     final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
92                     // Support
93                     if (acceptMsg.getIntegrity() != null) {
94                         throw new COPSPepException("Unsupported object (Integrity)");
95                     }
96
97                     // Mandatory KATimer
98                     final COPSKATimer kt = acceptMsg.getKATimer();
99                     if (kt == null)
100                         throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
101                     final short kaTimeVal = kt.getTimerVal();
102
103                     // ACTimer
104                     final COPSAcctTimer at = acceptMsg.getAcctTimer();
105                     short acctTimer = 0;
106                     if (at != null)
107                         acctTimer = at.getTimerVal();
108
109                     logger.info("Send a REQ message to the PS");
110                     final Properties prop = new Properties();
111                     final COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
112                     final COPSHandle handle = ((COPSReqMsg) reqMsg).getClientHandle();
113                     sendRequest(reqMsg);
114
115                     // Create the connection manager
116                     final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), config);
117                     conn.addRequestState(handle, new CmtsDataProcessor());
118                     conn.setKaTimer(kaTimeVal);
119                     conn.setAcctTimer(acctTimer);
120
121                     logger.info(getClass().getName() + " Thread(conn).start");
122                     thread = new Thread(conn);
123                     thread.start();
124                     break;
125                 default:
126                     throw new COPSPepException("Message not expected. Closing connection for " + getSocket().toString());
127             }
128         } catch (Exception e) {
129             logger.error(e.getMessage());
130         }
131     }
132
133     @Override
134     public void task(Callable<?> c) {
135         // TODO Auto-generated method stub
136
137     }
138
139     @Override
140     public void shouldWait(int t) {
141         // TODO Auto-generated method stub
142
143     }
144
145     @Override
146     public void done() {
147         // TODO Auto-generated method stub
148
149     }
150
151 }