blob: a3f9a231b963349796a68f1b619b15d74729cc39 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -07001
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
AyumuUeha76a01bc2017-05-18 13:34:13 +090019/**
20 * cController - PPPoE C_Controller
21 */
22var jsonServer = require('json-server');
23var Promise=require('bluebird');
24var fs=require('fs');
25var client = jsonServer.create();
26var middlewares = jsonServer.defaults();
27var execAsync=Promise.promisify(require('child_process').exec);
28var checkPNI=require('./checkPNI');
29var getData=require('./getData');
30var os=require('os');
31var ifaces = os.networkInterfaces();
32client.use(middlewares)
33
34/*keep alive with onos */
35client.get('/',function(req,res){
36 //no data back.
37 res.jsonp();
38})
39
40client.get('/pppoe/device',function(req,res){
41 var data={"type": "client"};
42 console.log("data:"+data);
43 res.jsonp(data)
44})
45
46client.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
65client.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
90client.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
114ifaces['eth0'].forEach(function(details){
115 if (details.family=='IPv4') {
116 eh0ip = details.address;
117 console.log('eh0ip:'+ eh0ip);
118 }
119});
120
121client.listen(3000, eh0ip, function (req,res) {
122 console.log('PPPoE c-Controller ' + eh0ip + ':3000 is running.');
123});