blob: 4494e899e48079db8ca71c1d95f8338f77435a0a [file] [log] [blame]
TamasBakai9b780332019-02-15 08:38:16 +00001var http = require('http');
2var https = require('https');
3
4var express = require('express');
5const stream = require('stream');
6var app = express();
7var fs = require("fs");
8var path = require('path');
BjornMagnussonXAf4e18362019-04-10 13:04:08 +00009var ArgumentParser = require('argparse').ArgumentParser;
TamasBakai9b780332019-02-15 08:38:16 +000010var privateKey = fs.readFileSync('cert/private.key', 'utf8');
11var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
12var credentials = {key: privateKey, cert: certificate};
13
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000014
TamasBakai9b780332019-02-15 08:38:16 +000015var bodyParser = require('body-parser')
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000016var startTime = Date.now();
17
18var dr_callback_ip = '192.168.100.2'; //IP for DR when running as container. Can be changed by env DR_SIM_IP
19
20//Counters
21var ctr_publish_requests = 0;
22var ctr_publish_responses = 0;
23var lastPublish = "";
24var dwl_volume = 0;
25
26var parser = new ArgumentParser({
27 version: '0.0.1',
28 addHelp:true,
29 description: 'Datarouter redirect simulator'
30 });
31
32parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
33parser.addArgument('--printtc' ,
34 {
35 help: 'Print complete usage help',
36 action: 'storeTrue'
37 }
38 );
39
40var args = parser.parseArgs();
41const tc_normal = "normal";
42const tc_no_publish ="no_publish"
43const tc_10p_no_response = "10p_no_response";
44const tc_10first_no_response = "10first_no_response";
45const tc_100first_no_response = "100first_no_response";
46const tc_all_delay_10s = "all_delay_10s";
47const tc_10p_delay_10s = "10p_delay_10s";
48const tc_10p_error_response = "10p_error_response";
49const tc_10first_error_response = "10first_error_response";
50const tc_100first_error_response = "100first_error_response";
51
52if (args.tc==tc_normal) {
53 console.log("TC: " + args.tc)
54
55} else if (args.tc==tc_no_publish) {
56 console.log("TC: " + args.tc)
57
58} else if (args.tc==tc_10p_no_response) {
59 console.log("TC: " + args.tc)
60
61} else if (args.tc==tc_10first_no_response) {
62 console.log("TC: " + args.tc)
63
64} else if (args.tc==tc_100first_no_response) {
65 console.log("TC: " + args.tc)
66
67} else if (args.tc==tc_all_delay_10s) {
68 console.log("TC: " + args.tc)
69
70} else if (args.tc==tc_10p_delay_10s) {
71 console.log("TC: " + args.tc)
72
73} else if (args.tc==tc_10p_error_response) {
74 console.log("TC: " + args.tc)
75
76} else if (args.tc==tc_10first_error_response) {
77 console.log("TC: " + args.tc)
78
79} else if (args.tc==tc_100first_error_response) {
80 console.log("TC: " + args.tc)
81} else {
82 console.log("No TC specified, use: --tc <tc-id>");
83 process.exit(0);
84}
85
86if (args.printtc) {
87 console.log("TC " + tc_normal + ": Normal case, all files publish and DR updated");
88 console.log("TC " + tc_no_publish + ": Ok response but no files published");
89 console.log("TC " + tc_10p_no_response + ": 10% % no response (file not published)");
90 console.log("TC " + tc_10first_no_response + ": 10 first requests give no response (files not published)");
91 console.log("TC " + tc_100first_no_response + ": 100 first requests give no response (files not published)");
92 console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s, normal publish");
93 console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, normal publish");
94 console.log("TC " + tc_10p_error_response + ": 10% error response (file not published)");
95 console.log("TC " + tc_10first_error_response + ": 10 first requests give error response (file not published)");
96 console.log("TC " + tc_100first_error_response + ": 100 first requests give error responses (file not published)");
97
98 process.exit(0);
99}
TamasBakai9b780332019-02-15 08:38:16 +0000100
101// parse application/x-www-form-urlencoded
102app.use(bodyParser.urlencoded({ extended: false }))
103
104// parse application/json
105app.use(bodyParser.json())
106
107// parse application/vnd.api+json as json
108app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
109
110// parse some custom thing into a Buffer
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000111app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' }))
TamasBakai9b780332019-02-15 08:38:16 +0000112
113// parse an HTML body into a string
114app.use(bodyParser.text({ type: 'text/html' }))
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000115
116//Formatting
117function fmtMSS(s){
118 return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff to mm:ss
119}
120function fmtLargeNumber(x) {
121 return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "); //Format large with space, eg: 1 000 000
122}
123
124//I'm alive function
TamasBakai9b780332019-02-15 08:38:16 +0000125app.get("/",function(req, res){
126 res.send("ok");
127})
128
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000129//Counter readout
130app.get("/ctr_publish_requests",function(req, res){
131 res.send(""+ctr_publish_requests);
TamasBakai9b780332019-02-15 08:38:16 +0000132})
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000133app.get("/ctr_publish_responses",function(req, res){
134 res.send(""+ctr_publish_responses);
135})
136app.get("/execution_time",function(req, res){
137 diff = fmtMSS(Math.floor((Date.now()-startTime)/1000));
138 res.send(""+diff);
139})
140app.get("/time_lastpublish",function(req, res){
141 res.send(""+lastPublish);
142})
143app.get("/dwl_volume",function(req, res){
144 res.send(""+fmtLargeNumber(dwl_volume));
145})
146app.get("/tc_info",function(req, res){
147 res.send(args.tc);
148})
149
150app.put('/publish/1/:filename', function (req, res) {
151 console.log(req.url);
152 console.log("First 25 bytes of body: " + req.body.slice(0,25))
153 console.log(req.headers)
154 ctr_publish_requests++;
155 if (args.tc == tc_no_publish) {
156 tr_publish_responses++;
157 res.send("ok")
158 return;
159 } else if (args.tc==tc_10p_no_response && (ctr_publish_requests%10)==0) {
160 return;
161 } else if (args.tc==tc_10first_no_response && ctr_publish_requests<11) {
162 return;
163 } else if (args.tc==tc_100first_no_response && ctr_publish_requests<101) {
164 return;
165 } else if (args.tc==tc_10p_error_response && (ctr_publish_requests%10)==0) {
166 tr_publish_responses++;
167 res.send(400, "");
168 return;
169 } else if (args.tc==tc_10first_error_response && ctr_publish_requests<11) {
170 tr_publish_responses++;
171 res.send(400, "");
172 return;
173 } else if (args.tc==tc_100first_error_response && ctr_publish_requests<101) {
174 tr_publish_responses++;
175 res.send(400, "");
176 return;
177 } else if (args.tc==tc_10p_delay_10s && (ctr_publish_requests%10)==0) {
178 console.log("sleep begin");
179 timer(10000).then(_=>console.log("sleeping done"));
180 } else if (args.tc==tc_all_delay_10s) {
181 //var sleep = require('sleep');
182 console.log("sleep begin");
183 //sleep.sleep(10);
184 timer(10000).then(_=>console.log("sleeping done"));
185 }
186
187 //Remaining part if normal file publish
188
189 var filename = req.params.filename;
190 console.log(filename);
191 //Create filename (appending file size to name) to store
192 var storedFilename = path.resolve(__dirname, filename+"-"+req.body.length);
193 fs.writeFile(storedFilename, "", function (error) { //Store file with zero size
194 if (error) { console.error(error); }
195 });
196
197 //Make callback to update list of publish files in DR sim
198 //Note the hard code ip-adress, DR sim get this ip if simulators started from the
199 //script in the 'simulatorgroup' dir.
200 //Work around: Could not get a normal http put to work from nodejs, using curl instead
201 var util = require('util');
202 var exec = require('child_process').exec;
203
204 var command = 'curl -s -X PUT http://' + dr_callback_ip + ':3906/dr_redir_publish/' +req.params.filename;
205
206 console.log("Callback to DR sim to report file published, cmd: " + command);
207 child = exec(command, function(error, stdout, stderr){
208 console.log('stdout: ' + stdout);
209 console.log('stderr: ' + stderr);
210 if(error !== null) {
211 console.log('exec error: ' + error);
212 }
213
214 });
215
216 //Update status variables
217 ctr_publish_responses++;
218 lastPublish = fmtMSS(Math.floor((Date.now()-startTime)/1000));
219 dwl_volume = dwl_volume + req.body.length;
220
221 res.send("ok")
222});
223
224
TamasBakai9b780332019-02-15 08:38:16 +0000225var httpServer = http.createServer(app);
226var httpsServer = https.createServer(credentials, app);
227
228var httpPort=3908
229var httpsPort=3909
230httpServer.listen(httpPort);
231console.log("DR-simulator listening (http) at "+httpPort)
232httpsServer.listen(httpsPort);
233console.log("DR-simulator listening (https) at "+httpsPort)
234
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000235if (process.env.DR_SIM_IP) {
236 dr_callback_ip=process.env.DR_SIM_IP;
237}
238console.log("Using IP " + dr_callback_ip + " for callback to DR sim");