AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame^] | 1 | /** |
| 2 | * cController - PPPoE C_Controller |
| 3 | */ |
| 4 | var jsonServer = require('json-server'); |
| 5 | var Promise=require('bluebird'); |
| 6 | var fs=require('fs'); |
| 7 | var client = jsonServer.create(); |
| 8 | var middlewares = jsonServer.defaults(); |
| 9 | var execAsync=Promise.promisify(require('child_process').exec); |
| 10 | var checkPNI=require('./checkPNI'); |
| 11 | var getData=require('./getData'); |
| 12 | var os=require('os'); |
| 13 | var ifaces = os.networkInterfaces(); |
| 14 | client.use(middlewares) |
| 15 | |
| 16 | /*keep alive with onos */ |
| 17 | client.get('/',function(req,res){ |
| 18 | //no data back. |
| 19 | res.jsonp(); |
| 20 | }) |
| 21 | |
| 22 | client.get('/pppoe/device',function(req,res){ |
| 23 | var data={"type": "client"}; |
| 24 | console.log("data:"+data); |
| 25 | res.jsonp(data) |
| 26 | }) |
| 27 | |
| 28 | client.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 | |
| 47 | client.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 | |
| 72 | client.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 | |
| 96 | ifaces['eth0'].forEach(function(details){ |
| 97 | if (details.family=='IPv4') { |
| 98 | eh0ip = details.address; |
| 99 | console.log('eh0ip:'+ eh0ip); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | client.listen(3000, eh0ip, function (req,res) { |
| 104 | console.log('PPPoE c-Controller ' + eh0ip + ':3000 is running.'); |
| 105 | }); |