blob: e6803f9f09291b5010ad509346fd735103c5e67b [file] [log] [blame]
AyumuUeha76a01bc2017-05-18 13:34:13 +09001/**
2 * cController - PPPoE C_Controller
3 */
4var jsonServer = require('json-server');
5var Promise=require('bluebird');
6var fs=require('fs');
7var client = jsonServer.create();
8var middlewares = jsonServer.defaults();
9var execAsync=Promise.promisify(require('child_process').exec);
10var checkPNI=require('./checkPNI');
11var getData=require('./getData');
12var os=require('os');
13var ifaces = os.networkInterfaces();
14client.use(middlewares)
15
16/*keep alive with onos */
17client.get('/',function(req,res){
18 //no data back.
19 res.jsonp();
20})
21
22client.get('/pppoe/device',function(req,res){
23 var data={"type": "client"};
24 console.log("data:"+data);
25 res.jsonp(data)
26})
27
28client.get('/pppoe/session',function(req,res){
29 var data = "";
30 var cat = "ifconfig ppp0 ";
31 execAsync(cat).then(function (result) {
32 console.log(cat + " done");
33 var data = getData.get(result);
34 res.jsonp(data)
35 }, function (err) {
36 var data = {
37 "ip": "0.0.0.0",
38 "rx-packets": 0,
39 "tx-packets": 0,
40 "rx-bytes": 0,
41 "tx-bytes": 0
42 };
43 res.jsonp(data)
44 });
45})
46
47client.put('/pppoe/config',function(req,res){
48 var adminState;
49 var endSession = false;
50 req.on('data', function (data) {
51 try{
52 var dataJson = JSON.parse(data.toString());
53 adminState = dataJson['admin-state'];
54 endSession = dataJson['end-session'];
55 }
56 catch(e){
57 console.log('error.');
58 res.jsonp(false)
59 }
60 console.log("adminState:"+adminState);
61 console.log("endSession:"+endSession);
62 var data = checkPNI.check(adminState, endSession);
63 data.then(function (result){
64
65 console.log("result:"+result);
66 res.jsonp(result)
67
68 });
69 })
70})
71
72client.get('/pppoe/info',function(req,res){
73 execAsync("cat adminState.txt ").then(function (result) {
74 var state = result.slice(result.indexOf("admin-state")+13,result.indexOf(","));
75 var infoStr = fs.readFileSync('info.txt').toString();
76 var strarray = infoStr.match(/\d{1,4}/g);
77 var svlan = strarray[0];
78 var cvlan = strarray[1];
79 console.log(infoStr);
80 var data = {
81 "admin-state" : JSON.parse(state),
82 "s-vlan" : JSON.parse(svlan),
83 "c-vlan" : JSON.parse(cvlan)
84 };
85 res.jsonp(data);
86 }, function (err) {
87 var data = {
88 "admin-state" : "0",
89 "s-vlan": 0,
90 "c-vlan": 0
91 }
92 res.jsonp(data);
93 });
94})
95
96ifaces['eth0'].forEach(function(details){
97 if (details.family=='IPv4') {
98 eh0ip = details.address;
99 console.log('eh0ip:'+ eh0ip);
100 }
101});
102
103client.listen(3000, eh0ip, function (req,res) {
104 console.log('PPPoE c-Controller ' + eh0ip + ':3000 is running.');
105});