Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 19 | /** |
| 20 | * cController - PPPoE C_Controller |
| 21 | */ |
| 22 | var jsonServer = require('json-server'); |
| 23 | var Promise=require('bluebird'); |
| 24 | var fs=require('fs'); |
| 25 | var client = jsonServer.create(); |
| 26 | var middlewares = jsonServer.defaults(); |
| 27 | var execAsync=Promise.promisify(require('child_process').exec); |
| 28 | var checkPNI=require('./checkPNI'); |
| 29 | var getData=require('./getData'); |
| 30 | var os=require('os'); |
| 31 | var ifaces = os.networkInterfaces(); |
| 32 | client.use(middlewares) |
| 33 | |
| 34 | /*keep alive with onos */ |
| 35 | client.get('/',function(req,res){ |
| 36 | //no data back. |
| 37 | res.jsonp(); |
| 38 | }) |
| 39 | |
| 40 | client.get('/pppoe/device',function(req,res){ |
| 41 | var data={"type": "client"}; |
| 42 | console.log("data:"+data); |
| 43 | res.jsonp(data) |
| 44 | }) |
| 45 | |
| 46 | client.get('/pppoe/session',function(req,res){ |
| 47 | var data = ""; |
| 48 | var cat = "ifconfig ppp0 "; |
| 49 | execAsync(cat).then(function (result) { |
| 50 | console.log(cat + " done"); |
| 51 | var data = getData.get(result); |
| 52 | res.jsonp(data) |
| 53 | }, function (err) { |
| 54 | var data = { |
| 55 | "ip": "0.0.0.0", |
| 56 | "rx-packets": 0, |
| 57 | "tx-packets": 0, |
| 58 | "rx-bytes": 0, |
| 59 | "tx-bytes": 0 |
| 60 | }; |
| 61 | res.jsonp(data) |
| 62 | }); |
| 63 | }) |
| 64 | |
| 65 | client.put('/pppoe/config',function(req,res){ |
| 66 | var adminState; |
| 67 | var endSession = false; |
| 68 | req.on('data', function (data) { |
| 69 | try{ |
| 70 | var dataJson = JSON.parse(data.toString()); |
| 71 | adminState = dataJson['admin-state']; |
| 72 | endSession = dataJson['end-session']; |
| 73 | } |
| 74 | catch(e){ |
| 75 | console.log('error.'); |
| 76 | res.jsonp(false) |
| 77 | } |
| 78 | console.log("adminState:"+adminState); |
| 79 | console.log("endSession:"+endSession); |
| 80 | var data = checkPNI.check(adminState, endSession); |
| 81 | data.then(function (result){ |
| 82 | |
| 83 | console.log("result:"+result); |
| 84 | res.jsonp(result) |
| 85 | |
| 86 | }); |
| 87 | }) |
| 88 | }) |
| 89 | |
| 90 | client.get('/pppoe/info',function(req,res){ |
| 91 | execAsync("cat adminState.txt ").then(function (result) { |
| 92 | var state = result.slice(result.indexOf("admin-state")+13,result.indexOf(",")); |
| 93 | var infoStr = fs.readFileSync('info.txt').toString(); |
| 94 | var strarray = infoStr.match(/\d{1,4}/g); |
| 95 | var svlan = strarray[0]; |
| 96 | var cvlan = strarray[1]; |
| 97 | console.log(infoStr); |
| 98 | var data = { |
| 99 | "admin-state" : JSON.parse(state), |
| 100 | "s-vlan" : JSON.parse(svlan), |
| 101 | "c-vlan" : JSON.parse(cvlan) |
| 102 | }; |
| 103 | res.jsonp(data); |
| 104 | }, function (err) { |
| 105 | var data = { |
| 106 | "admin-state" : "0", |
| 107 | "s-vlan": 0, |
| 108 | "c-vlan": 0 |
| 109 | } |
| 110 | res.jsonp(data); |
| 111 | }); |
| 112 | }) |
| 113 | |
| 114 | ifaces['eth0'].forEach(function(details){ |
| 115 | if (details.family=='IPv4') { |
| 116 | eh0ip = details.address; |
| 117 | console.log('eh0ip:'+ eh0ip); |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | client.listen(3000, eh0ip, function (req,res) { |
| 122 | console.log('PPPoE c-Controller ' + eh0ip + ':3000 is running.'); |
| 123 | }); |