blob: a77f006a2d8d4471c32b0e1bab2f0b8d9766040a [file] [log] [blame]
Shad Ansari01b0e652018-04-05 21:02:53 +00001/*
nick7be062f2018-05-25 17:52:56 -04002 Copyright (C) 2018 Open Networking Foundation
Shad Ansari01b0e652018-04-05 21:02:53 +00003
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <iostream>
19#include <memory>
20#include <string>
nick7be062f2018-05-25 17:52:56 -040021#include <time.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040022#include <pthread.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000023
24#include "Queue.h"
25#include <iostream>
26#include <sstream>
27
28#include "server.h"
Shad Ansarib7b0ced2018-05-11 21:53:32 +000029#include "core.h"
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -040030#include "state.h"
Shad Ansari01b0e652018-04-05 21:02:53 +000031
Shad Ansarib7b0ced2018-05-11 21:53:32 +000032#include <grpc++/grpc++.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000033#include <openolt.grpc.pb.h>
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -080034#include <tech_profile.grpc.pb.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000035
36using grpc::Server;
37using grpc::ServerBuilder;
38using grpc::ServerContext;
39using grpc::ServerWriter;
Shad Ansarib7b0ced2018-05-11 21:53:32 +000040using grpc::Status;
Shad Ansari01b0e652018-04-05 21:02:53 +000041
42const char *serverPort = "0.0.0.0:9191";
nick7be062f2018-05-25 17:52:56 -040043int signature;
Shad Ansari01b0e652018-04-05 21:02:53 +000044
Shad Ansari627b5782018-08-13 22:49:32 +000045Queue<openolt::Indication> oltIndQ;
46
Shad Ansari01b0e652018-04-05 21:02:53 +000047class OpenoltService final : public openolt::Openolt::Service {
48
Nicolas Palpacuere3fc0d22018-08-02 16:51:05 -040049 Status DisableOlt(
50 ServerContext* context,
51 const openolt::Empty* request,
52 openolt::Empty* response) override {
53 return Disable_();
54 }
55
56 Status ReenableOlt(
57 ServerContext* context,
58 const openolt::Empty* request,
59 openolt::Empty* response) override {
60 return Reenable_();
61 }
62
Shad Ansari01b0e652018-04-05 21:02:53 +000063 Status ActivateOnu(
64 ServerContext* context,
65 const openolt::Onu* request,
66 openolt::Empty* response) override {
67 return ActivateOnu_(
68 request->intf_id(),
69 request->onu_id(),
70 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070071 ((request->serial_number()).vendor_specific()).c_str(), request->pir());
Shad Ansari01b0e652018-04-05 21:02:53 +000072 }
73
Jonathan Davis70c21812018-07-19 15:32:10 -040074 Status DeactivateOnu(
75 ServerContext* context,
76 const openolt::Onu* request,
77 openolt::Empty* response) override {
78 return DeactivateOnu_(
79 request->intf_id(),
80 request->onu_id(),
81 ((request->serial_number()).vendor_id()).c_str(),
82 ((request->serial_number()).vendor_specific()).c_str());
83 }
84
85 Status DeleteOnu(
86 ServerContext* context,
87 const openolt::Onu* request,
88 openolt::Empty* response) override {
89 return DeleteOnu_(
90 request->intf_id(),
91 request->onu_id(),
92 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070093 ((request->serial_number()).vendor_specific()).c_str());
Jonathan Davis70c21812018-07-19 15:32:10 -040094 }
95
Shad Ansari01b0e652018-04-05 21:02:53 +000096 Status OmciMsgOut(
97 ServerContext* context,
98 const openolt::OmciMsg* request,
99 openolt::Empty* response) override {
100 return OmciMsgOut_(
101 request->intf_id(),
102 request->onu_id(),
103 request->pkt());
104 }
105
Shad Ansarif2e27a42018-04-26 22:37:38 +0000106 Status OnuPacketOut(
107 ServerContext* context,
108 const openolt::OnuPacket* request,
109 openolt::Empty* response) override {
110 return OnuPacketOut_(
111 request->intf_id(),
112 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600113 request->port_no(),
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800114 request->gemport_id(),
Shad Ansarif2e27a42018-04-26 22:37:38 +0000115 request->pkt());
116 }
117
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400118 Status UplinkPacketOut(
119 ServerContext* context,
120 const openolt::UplinkPacket* request,
121 openolt::Empty* response) override {
122 return UplinkPacketOut_(
123 request->intf_id(),
Jason Huangd33b4d82019-05-15 18:22:57 +0800124 request->pkt(),
125 request->flow_id());
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400126 }
127
Shad Ansari01b0e652018-04-05 21:02:53 +0000128 Status FlowAdd(
129 ServerContext* context,
130 const openolt::Flow* request,
131 openolt::Empty* response) override {
132 return FlowAdd_(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700133 request->access_intf_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000134 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600135 request->uni_id(),
136 request->port_no(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000137 request->flow_id(),
138 request->flow_type(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700139 request->alloc_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000140 request->network_intf_id(),
141 request->gemport_id(),
142 request->classifier(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700143 request->action(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600144 request->priority(),
145 request->cookie());
Shad Ansari01b0e652018-04-05 21:02:53 +0000146 }
147
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400148 Status FlowRemove(
149 ServerContext* context,
150 const openolt::Flow* request,
151 openolt::Empty* response) override {
152 return FlowRemove_(
153 request->flow_id(),
154 request->flow_type());
155 }
156
Shad Ansari01b0e652018-04-05 21:02:53 +0000157 Status EnableIndication(
158 ServerContext* context,
159 const ::openolt::Empty* request,
160 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000161
nick7be062f2018-05-25 17:52:56 -0400162 std::cout << "Connection to Voltha established. Indications enabled"
163 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400164
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400165 if (state.previsouly_connected()) {
166 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400167 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400168 if (state.is_activated()){
169 // Adding extra olt indication of current state
170 openolt::Indication ind;
171 openolt::OltIndication* oltInd = new openolt::OltIndication();
172 if (state.is_activated()) {
173 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400174 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400175 } else {
176 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400177 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400178 }
179 ind.set_allocated_olt_ind(oltInd);
180 oltIndQ.push(ind);
181 }
182 }
183
Shad Ansariedef2132018-08-10 22:14:50 +0000184 state.connect();
185
186 while (state.is_connected()) {
187 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD);
188 if (ind.second == false) {
189 /* timeout - do lower priority periodic stuff like stats */
190 stats_collection();
191 continue;
192 }
193 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400194 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400195 if (!isConnected) {
196 //Lost connectivity to this Voltha instance
197 //Put the indication back in the queue for next connecting instance
198 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000199 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400200 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000201 //oltInd.release_olt_ind()
202 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400203
Shad Ansari01b0e652018-04-05 21:02:53 +0000204 return Status::OK;
205 }
nick7be062f2018-05-25 17:52:56 -0400206
207 Status HeartbeatCheck(
208 ServerContext* context,
209 const openolt::Empty* request,
210 openolt::Heartbeat* response) override {
211 response->set_heartbeat_signature(signature);
212
213 return Status::OK;
214 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400215
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400216 Status EnablePonIf(
217 ServerContext* context,
218 const openolt::Interface* request,
219 openolt::Empty* response) override {
220
221 return EnablePonIf_(request->intf_id());
222 }
223
224 Status DisablePonIf(
225 ServerContext* context,
226 const openolt::Interface* request,
227 openolt::Empty* response) override {
228
229 return DisablePonIf_(request->intf_id());
230 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400231
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400232 Status CollectStatistics(
233 ServerContext* context,
234 const openolt::Empty* request,
235 openolt::Empty* response) override {
236
237 stats_collection();
238
239 return Status::OK;
240 }
241
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400242 Status Reboot(
243 ServerContext* context,
244 const openolt::Empty* request,
245 openolt::Empty* response) override {
246
Jason Huangd33b4d82019-05-15 18:22:57 +0800247 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400248
249 return Status::OK;
250
251 }
252
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400253 Status GetDeviceInfo(
254 ServerContext* context,
255 const openolt::Empty* request,
256 openolt::DeviceInfo* response) override {
257
258 GetDeviceInfo_(response);
259
260 return Status::OK;
261
262 }
263
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800264 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700265 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800266 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700267 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800268 CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700269 return Status::OK;
270 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400271
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800272 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700273 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800274 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700275 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800276 RemoveTrafficSchedulers_(request);
277 return Status::OK;
278 };
279
280 Status CreateTrafficQueues(
281 ServerContext* context,
282 const tech_profile::TrafficQueues* request,
283 openolt::Empty* response) override {
284 CreateTrafficQueues_(request);
285 return Status::OK;
286 };
287
288 Status RemoveTrafficQueues(
289 ServerContext* context,
290 const tech_profile::TrafficQueues* request,
291 openolt::Empty* response) override {
292 RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700293 return Status::OK;
294 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400295
Shad Ansari01b0e652018-04-05 21:02:53 +0000296};
297
298void RunServer() {
299 OpenoltService service;
300 std::string server_address(serverPort);
301 ServerBuilder builder;
302
303 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
304 builder.RegisterService(&service);
305
306 std::unique_ptr<Server> server(builder.BuildAndStart());
307
nick7be062f2018-05-25 17:52:56 -0400308 time_t now;
309 time(&now);
310 signature = (int)now;
311
312 std::cout << "Server listening on " << server_address
313 << ", connection signature : " << signature << std::endl;
314
Shad Ansari01b0e652018-04-05 21:02:53 +0000315
316 server->Wait();
317}