11package com .paraam .cpeagent ;
22
33import java .io .File ;
4+ import java .io .StringReader ;
45import java .util .ArrayList ;
6+ import java .util .Arrays ;
57import java .util .Map ;
68
79import com .bazaarvoice .dropwizard .assets .ConfiguredAssetsBundle ;
1517import com .yammer .dropwizard .config .Environment ;
1618import com .yammer .dropwizard .views .ViewBundle ;
1719
20+ import com .opencsv .CSVParser ;
21+ import com .opencsv .CSVParserBuilder ;
22+
1823public class SimulatorService extends Service <SimulatorConfiguration > {
1924 // names of environment variables provided for Dockerfile.
2025 private static final String PI_INTERVAL = "PI_INTERVAL" ;
@@ -29,6 +34,7 @@ public class SimulatorService extends Service<SimulatorConfiguration> {
2934 private static final String SERIAL_NUMBER_FORMAT = "SERIAL_NUMBER_FMT" ;
3035 private static final String SERIAL_NUMBER = "SERIAL_NUMBER" ;
3136 private static final String IP_ADDRESS = "IP_ADDRESS" ;
37+ private static final String STRANGE_ACS = "STRANGE_ACS" ;
3238
3339 public static void main (final String [] args ) throws Exception {
3440 try {
@@ -46,100 +52,133 @@ public void initialize(final Bootstrap<SimulatorConfiguration> bootstrap) {
4652 bootstrap .addBundle (GuiceBundle .newBuilder ()
4753 .addModule (new SimulatorModule ())
4854 .enableAutoConfig (getClass ().getPackage ().getName ())
49- .build ()
50- );
55+ .build ());
5156 }
5257
5358 @ Override
5459 public void run (final SimulatorConfiguration configuration , final Environment environment ) {
55- final AgentConfig config = this .readAgentConfig ();
56- final String ipadr = config .getIpAddress ();
57- final CPEWorker worker = new CPEWorker (ipadr , config .getConnectionRequestPort (), config .getAcsUrl (),
58- config .getConnectionRequestPath (), config .getPeriodicInformInterval (),
59- config .getSimulatorLocation (), config .getAuthUserName (), config .getAuthPassword (),
60- config .getAuthType (), config .getUserAgent (), config .getXmlFormat (),
61- config .getSerialNumberFmt (), config .getSerialNumber ());
62- final CPEHttpServer httpserver = worker .getNewHttpServer ();
63- Thread serverthread = new Thread (httpserver , "Http_Server" );
64- serverthread .start ();
65- final CPEPeriodicInform periodicInform = worker .getNewPeriodicInform ();
66- Thread informthread = new Thread (periodicInform , "Periodic_Inform" );
67- informthread .start ();
68- final Thread cpthread = new Thread (worker , "WorkerThread_" + 0 );
69- cpthread .start ();
60+ final ArrayList <AgentConfig > agentConfigs = this .readAgentConfig ();
61+ CPEUtil util = new CPEUtil ();
62+ int threadcnt = 0 ;
63+
64+ for (AgentConfig config : agentConfigs ) {
65+ ArrayList <String > iplist = new ArrayList <String >();
66+ if (!util .isValidIPAddress (config .getStartIpAddress ())) {
67+ continue ;
68+ }
69+ if (!util .isValidIPAddress (config .getEndIpAddress ())) {
70+ iplist .add (config .getStartIpAddress ());
71+ } else {
72+ iplist = util .range2iplist (config .getStartIpAddress (), config .getEndIpAddress ());
73+ }
74+
75+ for (String ipaddr : iplist ) {
76+ final CPEWorker worker = new CPEWorker (ipaddr , config .getConnectionRequestPort () + threadcnt , config .getAcsUrl (),
77+ config .getConnectionRequestPath (), config .getPeriodicInformInterval (),
78+ config .getSimulatorLocation (), config .getAuthUserName (), config .getAuthPassword (),
79+ config .getAuthType (), config .getUserAgent (), config .getXmlFormat (),
80+ config .getSerialNumberFmt (), config .getSerialNumber () + threadcnt , config .getStrangeAcs ());
81+ final CPEHttpServer httpserver = worker .getNewHttpServer ();
82+ Thread serverthread = new Thread (httpserver , "Http_Server" );
83+ serverthread .start ();
84+ final CPEPeriodicInform periodicInform = worker .getNewPeriodicInform ();
85+ Thread informthread = new Thread (periodicInform , "Periodic_Inform" );
86+ informthread .start ();
87+ final Thread cpthread = new Thread (worker , "WorkerThread_" + threadcnt );
88+ cpthread .start ();
89+ threadcnt ++;
90+ }
91+ }
7092 }
7193
72- private AgentConfig readAgentConfig () {
94+ private ArrayList < AgentConfig > readAgentConfig () {
7395 final String confDir = "." + File .separator + "conf" ;
7496 final String filepath = confDir + File .separator + "agent.csv" ;
75- //System.out.println("Current Filepath Checking >>>> " + filepath );
97+ // System.out.println("Current Filepath Checking >>>> " + filepath );
7698 final File userfile = new File (filepath );
77- AgentConfig config = userfile .exists () ? this .readAgentFile (filepath ) : null ;
99+ ArrayList < AgentConfig > config = userfile .exists () ? this .readAgentFile (filepath ) : null ;
78100 if (config == null ) {
79- config = this .readAgentEnvironment ();
101+ config . add ( this .readAgentEnvironment () );
80102 }
81103 return config ;
82104 }
83105
84- private AgentConfig readAgentFile (final String filepath ) {
106+ private ArrayList <AgentConfig > readAgentFile (final String filepath ) {
107+ CSVParser parser = new CSVParserBuilder ()
108+ .withSeparator (',' )
109+ .withIgnoreQuotations (false )
110+ .build ();
85111 final CPEUtil util = new CPEUtil ();
86112 final ArrayList <String > csvlist = util .parseFile (filepath );
87113 final ArrayList <String []> tokenized = new ArrayList <String []>();
88114 for (final String line : csvlist ) {
89- final String [] tokens = line .split ("," );
90- if ((tokens .length > 0 ) && util .isValidIPAddress (tokens [0 ].trim ()))
91- tokenized .add (tokens );
115+ try {
116+ String [] tokens = parser .parseLine (line );
117+ if ((tokens .length > 0 ) && util .isValidIPAddress (tokens [0 ].trim ())) {
118+ tokenized .add (tokens );
119+ }
120+ } catch (Exception e ) {
121+ e .printStackTrace ();
122+ }
92123 }
93124 if (tokenized .isEmpty ()) {
94125 return null ;
95126 }
96127
97- final AgentConfig config = new AgentConfig ();
98- final String [] csvline = tokenized .get (0 );
99-
100- //System.out.println("CSV Line >>>>>>>>> " + csvline);
101- if (csvline .length >= 7 ) {
102- config .setIpAddress (csvline [0 ].trim ());
103- config .setAcsUrl (csvline [2 ].trim ());
104- config .setConnectionRequestPath (csvline [3 ].trim ());
105- final String crPortVal = csvline [4 ].trim ();
106- config .setConnectionRequestPort (Integer .parseInt (crPortVal ));
107- final String informInterval = csvline [5 ].trim ();
108- config .setPeriodicInformInterval (Integer .parseInt (informInterval ));
109- config .setSimulatorLocation (csvline [6 ].trim ());
110- }
111- // defaults if not set in the file
112- config .setUserAgent ("tr069-simulator" );
113- config .setXmlFormat ("" );
114- config .setAuthType ("" );
115- config .setAuthUserName ("user" );
116- config .setAuthPassword ("" );
117-
118- if (csvline .length >= 10 ) {
119- config .setAuthUserName (csvline [7 ].trim ());
120- config .setAuthPassword (csvline [8 ].trim ());
121- config .setAuthType (csvline [9 ].trim ());
122- }
123- if (csvline .length >= 11 ) {
124- config .setUserAgent (csvline [10 ].trim ());
125- }
126- if (csvline .length >= 12 ) {
127- config .setXmlFormat (csvline [11 ].trim ());
128- }
129-
130- if (csvline .length >= 13 ) {
131- config .setSerialNumberFmt (csvline [12 ].trim ());
132- config .setSerialNumber (0 );
133- }
134- if (csvline .length >= 14 ) {
135- config .setSerialNumber (Integer .parseInt (csvline [13 ].trim ()));
128+ final ArrayList <AgentConfig > agentConfigs = new ArrayList <AgentConfig >();
129+
130+ for (String [] csvline : tokenized ) {
131+ final AgentConfig config = new AgentConfig ();
132+
133+ // System.out.println("CSV Line >>>>>>>>> " + csvline);
134+ if (csvline .length >= 7 ) {
135+ config .setStartIpAddress (csvline [0 ].trim ());
136+ config .setEndIpAddress (csvline [1 ].trim ());
137+ config .setAcsUrl (csvline [2 ].trim ());
138+ config .setConnectionRequestPath (csvline [3 ].trim ());
139+ final String crPortVal = csvline [4 ].trim ();
140+ config .setConnectionRequestPort (Integer .parseInt (crPortVal ));
141+ final String informInterval = csvline [5 ].trim ();
142+ config .setPeriodicInformInterval (Integer .parseInt (informInterval ));
143+ config .setSimulatorLocation (csvline [6 ].trim ());
144+ }
145+ // defaults if not set in the file
146+ config .setUserAgent ("tr069-simulator" );
147+ config .setXmlFormat ("" );
148+ config .setAuthType ("" );
149+ config .setAuthUserName ("user" );
150+ config .setAuthPassword ("" );
151+
152+ if (csvline .length >= 10 ) {
153+ config .setAuthUserName (csvline [7 ].trim ());
154+ config .setAuthPassword (csvline [8 ].trim ());
155+ config .setAuthType (csvline [9 ].trim ());
156+ }
157+ if (csvline .length >= 11 ) {
158+ config .setUserAgent (csvline [10 ].trim ());
159+ }
160+ if (csvline .length >= 12 ) {
161+ config .setXmlFormat (csvline [11 ].trim ());
162+ }
163+ if (csvline .length >= 13 ) {
164+ config .setSerialNumberFmt (csvline [12 ].trim ());
165+ config .setSerialNumber (0 );
166+ }
167+ if (csvline .length >= 14 ) {
168+ config .setSerialNumber (Integer .parseInt (csvline [13 ].trim ()));
169+ }
170+ if (csvline .length >= 15 ) {
171+ config .setStrangeAcs (Boolean .parseBoolean (csvline [14 ].trim ()));
172+ }
173+ agentConfigs .add (config );
136174 }
137- return config ;
175+ return agentConfigs ;
138176 }
139177
140178 /**
141179 * Intended to read external properties from environment variables. Mostly
142- * Useful in docker containers where this is an easier way to convey configuration.
180+ * Useful in docker containers where this is an easier way to convey
181+ * configuration.
143182 *
144183 * @return Agent configuration object.
145184 */
@@ -149,7 +188,7 @@ private AgentConfig readAgentEnvironment() {
149188 config .setXmlFormat ("" );
150189
151190 final Map <String , String > environment = System .getenv ();
152- config .setIpAddress (this .getOrDefault (SimulatorService .IP_ADDRESS , environment , "127.0.0.1" ));
191+ config .setStartIpAddress (this .getOrDefault (SimulatorService .IP_ADDRESS , environment , "127.0.0.1" ));
153192 final String piIntervalInSec = this .getOrDefault (SimulatorService .PI_INTERVAL , environment , "600" );
154193 config .setPeriodicInformInterval (Integer .parseInt (piIntervalInSec ));
155194 config .setAuthType (this .getOrDefault (SimulatorService .AUTH_TYPE , environment , "" ));
@@ -165,6 +204,8 @@ private AgentConfig readAgentEnvironment() {
165204 config .setSerialNumberFmt (this .getOrDefault (SimulatorService .SERIAL_NUMBER_FORMAT , environment , "%08d" ));
166205 final String serialNumberValue = this .getOrDefault (SimulatorService .SERIAL_NUMBER , environment , "0" );
167206 config .setSerialNumber (Integer .parseInt (serialNumberValue ));
207+ final String strangeAcsValue = this .getOrDefault (SimulatorService .STRANGE_ACS , environment , "false" );
208+ config .setStrangeAcs (Boolean .getBoolean (strangeAcsValue ));
168209
169210 return config ;
170211 }
0 commit comments