1c9f29cd3eca1dc1128e9deef4e0a0a6a5b7dadf
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / rcd / impl / CMTS.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.PCMMConstants;
8 import org.pcmm.PCMMProperties;
9 import org.pcmm.gates.IGateSpec.Direction;
10 import org.pcmm.rcd.ICMTS;
11
12 import java.io.IOException;
13 import java.net.Socket;
14 import java.util.*;
15 import java.util.concurrent.ConcurrentHashMap;
16
17 /**
18  * This class starts a mock CMTS that can be used for testing.
19  */
20 public class CMTS extends AbstractPCMMServer implements ICMTS {
21
22         /**
23          * Receives messages from the COPS client
24          */
25         private final Map<String, IPCMMClientHandler> handlerMap;
26
27         /**
28          * The configured gates
29          */
30         private final Map<Direction, Set<String>> gateConfig;
31
32         /**
33          * The connected CMTSs and whether or not they are up
34          */
35         private final Map<String, Boolean> cmStatus;
36
37         /**
38          * Constructor for having the server port automatically assigned
39          * Call getPort() after startServer() is called to determine the port number of the server
40          */
41         public CMTS(final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
42                 this(0, gateConfig, cmStatus);
43         }
44
45         /**
46          * Constructor for starting the server to a pre-defined port number
47          * @param port - the port number on which to start the server.
48          */
49         public CMTS(final int port, final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
50                 super(port);
51                 if (gateConfig == null || cmStatus == null) throw new IllegalArgumentException("Config must not be null");
52                 this.gateConfig = Collections.unmodifiableMap(gateConfig);
53                 this.cmStatus = Collections.unmodifiableMap(cmStatus);
54                 handlerMap = new ConcurrentHashMap<>();
55         }
56
57         @Override
58         public void stopServer() {
59                 for (final IPCMMClientHandler handler : handlerMap.values()) {
60                         handler.stop();
61                 }
62                 super.stopServer();
63         }
64
65         @Override
66         protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
67                 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
68                 if (handlerMap.get(key) == null) {
69                         final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, gateConfig, cmStatus);
70                         handler.connect();
71                         handlerMap.put(key, handler);
72                         return handler;
73                 } else return handlerMap.get(key);
74         }
75
76         /**
77          * To start a CMTS
78          * @param args - the arguments which will contain configuration information
79          * @throws IOException - should the server fail to start for reasons such as port contention.
80          */
81         public static void main(final String[] args) throws IOException {
82                 final CMTS cmts = new CMTS(PCMMProperties.get(PCMMConstants.PCMM_PORT, Integer.class),
83                                 new HashMap<Direction, Set<String>>(), new HashMap<String, Boolean>());
84                 cmts.startServer();
85         }
86
87 }