Moving fix by Tony Tkacik in the stable/lithium branch which caused an error with...
[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.InetAddress;
14 import java.net.Socket;
15 import java.util.*;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 /**
19  * This class starts a mock CMTS that can be used for testing.
20  */
21 public class CMTS extends AbstractPCMMServer implements ICMTS {
22
23         /**
24          * Receives messages from the COPS client
25          */
26         private final Map<String, IPCMMClientHandler> handlerMap;
27
28         /**
29          * The configured gates
30          */
31         private final Map<Direction, Set<String>> gateConfig;
32
33         /**
34          * The connected CMTSs and whether or not they are up
35          */
36         private final Map<String, Boolean> cmStatus;
37
38         /**
39          * Constructor for having the server port automatically assigned
40          * Call getPort() after startServer() is called to determine the port number of the server
41          */
42         public CMTS(final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
43                 this(0, gateConfig, cmStatus);
44         }
45
46         /**
47          * Constructor for starting the server to a pre-defined port number
48          * @param port - the port number on which to start the server.
49          */
50         public CMTS(final int port, final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
51                 super(port);
52                 if (gateConfig == null || cmStatus == null) throw new IllegalArgumentException("Config must not be null");
53                 this.gateConfig = Collections.unmodifiableMap(gateConfig);
54                 this.cmStatus = Collections.unmodifiableMap(cmStatus);
55                 handlerMap = new ConcurrentHashMap<>();
56         }
57
58         @Override
59         public void stopServer() {
60                 for (final IPCMMClientHandler handler : handlerMap.values()) {
61                         handler.stop();
62                 }
63                 super.stopServer();
64         }
65
66         @Override
67         protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
68                 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
69                 if (handlerMap.get(key) == null) {
70                         final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, gateConfig, cmStatus);
71                         handler.connect();
72                         handlerMap.put(key, handler);
73                         return handler;
74                 } else return handlerMap.get(key);
75         }
76
77         /**
78          * To start a CMTS
79          * @param args - the arguments which will contain configuration information
80          * @throws IOException - should the server fail to start for reasons such as port contention.
81          */
82         public static void main(final String[] args) throws IOException {
83                 final Set<String> upGates = new HashSet<>();
84                 upGates.add("extrm_up");
85                 final Set<String> dnGates = new HashSet<>();
86                 dnGates.add("extrm_dn");
87                 final Map<Direction, Set<String>> gates = new HashMap<>();
88                 gates.put(Direction.UPSTREAM, upGates);
89                 gates.put(Direction.DOWNSTREAM, dnGates);
90
91                 final Map<String, Boolean> cmStatus = new HashMap<>();
92                 final InetAddress invalidCmAddrInet = InetAddress.getByAddress(new byte[] {99, 99, 99, 99});
93                 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 180}).getHostAddress(), true);
94                 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 179}).getHostAddress(), true);
95                 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 178}).getHostAddress(), true);
96                 cmStatus.put(invalidCmAddrInet.getHostAddress(), false);
97
98                 final CMTS cmts = new CMTS(PCMMProperties.get(PCMMConstants.PCMM_PORT, Integer.class), gates, cmStatus);
99                 cmts.startServer();
100         }
101
102 }