Bump to odlparent 3.1.0 and yangtools 2.0.3
[packetcable.git] / packetcable-emulator / src / main / java / org / pcmm / rcd / impl / CMTS.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 com.google.common.collect.Maps;
14 import java.io.IOException;
15 import java.net.Socket;
16 import java.util.Map;
17 import org.pcmm.rcd.ICMTS;
18
19 /**
20  * Mock CMTS that can be used for testing. startServer() is called to start required threads after instantiation.
21  */
22 public class CMTS extends AbstractPCMMServer implements ICMTS {
23
24     /**
25      * Emulator configuration
26      */
27     private final CMTSConfig config;
28
29         /**
30          * Receives messages from the COPS client
31          */
32         private final Map<String, IPCMMClientHandler> handlerMap;
33
34         /**
35          * Constructor for having the server port automatically assigned
36          * Call getPort() after startServer() is called to determine the port number of the server
37          */
38         public CMTS(final CMTSConfig config) {
39         super(checkNotNull(config, "config must not be null").getPort());
40         this.config = config;
41         handlerMap = Maps.newConcurrentMap();
42     }
43
44         @Override
45         public void stopServer() {
46                 for (final IPCMMClientHandler handler : handlerMap.values()) {
47                         handler.stop();
48                 }
49                 super.stopServer();
50         }
51
52         @Override
53         protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
54                 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
55                 if (handlerMap.get(key) == null) {
56                         final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, config);
57                         handler.connect();
58                         handlerMap.put(key, handler);
59                         return handler;
60                 } else {
61             return handlerMap.get(key);
62         }
63         }
64
65         /**
66          * To start a CMTS
67          * @param args - the arguments which will contain configuration information
68          * @throws IOException - should the server fail to start for reasons such as port contention.
69          */
70         public static void main(final String[] args) throws IOException {
71                 if (args.length != 1) {
72             throw new IllegalArgumentException("expected arguments: <cmts_yaml_config_file>");
73         }
74
75                 final CMTSConfig config = CMTSConfig.loadConfig(args[0]);
76                 final CMTS cmts = new CMTS(config);
77                 cmts.startServer();
78         }
79
80 }