blob: 507bfc1ab83c917f35b51eb008c41f420f6d04c6 [file] [log] [blame]
Shad Ansari01b0e652018-04-05 21:02:53 +00001/*
Girish Gowdraa707e7c2019-11-07 11:36:13 +05302 * Copyright 2018-present Open Networking Foundation
Shad Ansari01b0e652018-04-05 21:02:53 +00003
Girish Gowdraa707e7c2019-11-07 11:36:13 +05304 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Shad Ansari01b0e652018-04-05 21:02:53 +00007
Girish Gowdraa707e7c2019-11-07 11:36:13 +05308 * http://www.apache.org/licenses/LICENSE-2.0
Shad Ansari01b0e652018-04-05 21:02:53 +00009
Girish Gowdraa707e7c2019-11-07 11:36:13 +053010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Shad Ansari01b0e652018-04-05 21:02:53 +000016
17#include <iostream>
18#include <memory>
19#include <string>
nick7be062f2018-05-25 17:52:56 -040020#include <time.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040021#include <pthread.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000022
23#include "Queue.h"
24#include <iostream>
25#include <sstream>
26
27#include "server.h"
Shad Ansarib7b0ced2018-05-11 21:53:32 +000028#include "core.h"
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -040029#include "state.h"
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -070030#include "../src/core_utils.h"
Shad Ansari01b0e652018-04-05 21:02:53 +000031
Shad Ansarib7b0ced2018-05-11 21:53:32 +000032#include <grpc++/grpc++.h>
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000033#include <voltha_protos/openolt.grpc.pb.h>
Akash Reddy Kankanaladec6e7e2025-05-22 09:49:28 +053034#include <voltha_protos/common.grpc.pb.h>
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000035#include <voltha_protos/tech_profile.grpc.pb.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000036
37using grpc::Server;
38using grpc::ServerBuilder;
Girish Gowdra252f4972020-09-07 21:24:01 -070039using grpc::ResourceQuota;
Shad Ansari01b0e652018-04-05 21:02:53 +000040using grpc::ServerContext;
41using grpc::ServerWriter;
Shad Ansarib7b0ced2018-05-11 21:53:32 +000042using grpc::Status;
Shad Ansari01b0e652018-04-05 21:02:53 +000043
44const char *serverPort = "0.0.0.0:9191";
nick7be062f2018-05-25 17:52:56 -040045int signature;
Arthur Syu094df162022-04-21 17:50:06 +080046std::unique_ptr<Server> server;
Shad Ansari01b0e652018-04-05 21:02:53 +000047
Shad Ansari627b5782018-08-13 22:49:32 +000048Queue<openolt::Indication> oltIndQ;
49
Shad Ansari01b0e652018-04-05 21:02:53 +000050class OpenoltService final : public openolt::Openolt::Service {
51
Nicolas Palpacuere3fc0d22018-08-02 16:51:05 -040052 Status DisableOlt(
53 ServerContext* context,
54 const openolt::Empty* request,
55 openolt::Empty* response) override {
56 return Disable_();
57 }
58
59 Status ReenableOlt(
60 ServerContext* context,
61 const openolt::Empty* request,
62 openolt::Empty* response) override {
63 return Reenable_();
64 }
65
Shad Ansari01b0e652018-04-05 21:02:53 +000066 Status ActivateOnu(
67 ServerContext* context,
68 const openolt::Onu* request,
69 openolt::Empty* response) override {
70 return ActivateOnu_(
71 request->intf_id(),
72 request->onu_id(),
73 ((request->serial_number()).vendor_id()).c_str(),
kesavandc1f2db92020-08-31 15:32:06 +053074 ((request->serial_number()).vendor_specific()).c_str(), request->pir(), request->omcc_encryption());
Shad Ansari01b0e652018-04-05 21:02:53 +000075 }
76
Jonathan Davis70c21812018-07-19 15:32:10 -040077 Status DeactivateOnu(
78 ServerContext* context,
79 const openolt::Onu* request,
80 openolt::Empty* response) override {
81 return DeactivateOnu_(
82 request->intf_id(),
83 request->onu_id(),
84 ((request->serial_number()).vendor_id()).c_str(),
85 ((request->serial_number()).vendor_specific()).c_str());
86 }
87
88 Status DeleteOnu(
89 ServerContext* context,
90 const openolt::Onu* request,
91 openolt::Empty* response) override {
92 return DeleteOnu_(
93 request->intf_id(),
94 request->onu_id(),
95 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070096 ((request->serial_number()).vendor_specific()).c_str());
Jonathan Davis70c21812018-07-19 15:32:10 -040097 }
98
Shad Ansari01b0e652018-04-05 21:02:53 +000099 Status OmciMsgOut(
100 ServerContext* context,
101 const openolt::OmciMsg* request,
102 openolt::Empty* response) override {
103 return OmciMsgOut_(
104 request->intf_id(),
105 request->onu_id(),
106 request->pkt());
107 }
108
Shad Ansarif2e27a42018-04-26 22:37:38 +0000109 Status OnuPacketOut(
110 ServerContext* context,
111 const openolt::OnuPacket* request,
112 openolt::Empty* response) override {
113 return OnuPacketOut_(
114 request->intf_id(),
115 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600116 request->port_no(),
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800117 request->gemport_id(),
Shad Ansarif2e27a42018-04-26 22:37:38 +0000118 request->pkt());
119 }
120
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400121 Status UplinkPacketOut(
122 ServerContext* context,
123 const openolt::UplinkPacket* request,
124 openolt::Empty* response) override {
125 return UplinkPacketOut_(
126 request->intf_id(),
127 request->pkt());
128 }
129
Shad Ansari01b0e652018-04-05 21:02:53 +0000130 Status FlowAdd(
131 ServerContext* context,
132 const openolt::Flow* request,
133 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700134 return FlowAddWrapper_(request);
135
Shad Ansari01b0e652018-04-05 21:02:53 +0000136 }
137
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400138 Status FlowRemove(
139 ServerContext* context,
140 const openolt::Flow* request,
141 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700142 return FlowRemoveWrapper_(request);
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400143 }
144
Shad Ansari01b0e652018-04-05 21:02:53 +0000145 Status EnableIndication(
146 ServerContext* context,
147 const ::openolt::Empty* request,
148 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000149
nick7be062f2018-05-25 17:52:56 -0400150 std::cout << "Connection to Voltha established. Indications enabled"
151 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400152
Burak Gurdag30db4822021-03-10 21:30:01 +0000153 if (state.previously_connected()) {
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400154 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400155 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400156 if (state.is_activated()){
157 // Adding extra olt indication of current state
158 openolt::Indication ind;
159 openolt::OltIndication* oltInd = new openolt::OltIndication();
160 if (state.is_activated()) {
161 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400162 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400163 } else {
164 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400165 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400166 }
167 ind.set_allocated_olt_ind(oltInd);
168 oltIndQ.push(ind);
169 }
170 }
171
Shad Ansariedef2132018-08-10 22:14:50 +0000172 state.connect();
173
174 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530175 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000176 if (ind.second == false) {
177 /* timeout - do lower priority periodic stuff like stats */
178 stats_collection();
179 continue;
180 }
181 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400182 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400183 if (!isConnected) {
184 //Lost connectivity to this Voltha instance
185 //Put the indication back in the queue for next connecting instance
186 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000187 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400188 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000189 //oltInd.release_olt_ind()
190 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400191
Shad Ansari01b0e652018-04-05 21:02:53 +0000192 return Status::OK;
193 }
nick7be062f2018-05-25 17:52:56 -0400194
195 Status HeartbeatCheck(
196 ServerContext* context,
197 const openolt::Empty* request,
198 openolt::Heartbeat* response) override {
199 response->set_heartbeat_signature(signature);
200
201 return Status::OK;
202 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400203
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400204 Status EnablePonIf(
205 ServerContext* context,
206 const openolt::Interface* request,
207 openolt::Empty* response) override {
208
209 return EnablePonIf_(request->intf_id());
210 }
211
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000212 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000213 ServerContext* context,
214 const openolt::Interface* request,
215 openolt::IntfIndication* response) override {
216
217 // TODO - Return the oper status of the pon interface
218 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000219 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000220
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400221 Status DisablePonIf(
222 ServerContext* context,
223 const openolt::Interface* request,
224 openolt::Empty* response) override {
225
226 return DisablePonIf_(request->intf_id());
227 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400228
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400229 Status CollectStatistics(
230 ServerContext* context,
231 const openolt::Empty* request,
232 openolt::Empty* response) override {
233
234 stats_collection();
235
236 return Status::OK;
237 }
238
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400239 Status Reboot(
240 ServerContext* context,
241 const openolt::Empty* request,
242 openolt::Empty* response) override {
243
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000244 uint8_t ret = system("shutdown -r now");
Arthur Syu094df162022-04-21 17:50:06 +0800245 server->Shutdown();
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400246
247 return Status::OK;
248
249 }
250
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400251 Status GetDeviceInfo(
252 ServerContext* context,
253 const openolt::Empty* request,
254 openolt::DeviceInfo* response) override {
255
256 GetDeviceInfo_(response);
257
258 return Status::OK;
259
260 }
261
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800262 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700263 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800264 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700265 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000266 return CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700267 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400268
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800269 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700270 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800271 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700272 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000273 return RemoveTrafficSchedulers_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800274 };
275
276 Status CreateTrafficQueues(
277 ServerContext* context,
278 const tech_profile::TrafficQueues* request,
279 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000280 return CreateTrafficQueues_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800281 };
282
283 Status RemoveTrafficQueues(
284 ServerContext* context,
285 const tech_profile::TrafficQueues* request,
286 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000287 return RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700288 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400289
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000290 Status PerformGroupOperation(
291 ServerContext* context,
292 const openolt::Group* request,
293 openolt::Empty* response) override {
294 return PerformGroupOperation_(request);
295 };
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800296
Burak Gurdageb4ca2e2020-06-15 07:48:26 +0000297 Status DeleteGroup(
298 ServerContext* context,
299 const openolt::Group* request,
300 openolt::Empty* response) override {
301 return DeleteGroup_(request->group_id());
302 };
303
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800304 Status OnuItuPonAlarmSet(
305 ServerContext* context,
kesavandc1f2db92020-08-31 15:32:06 +0530306 const config::OnuItuPonAlarm* request,
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800307 openolt::Empty* response) override {
308 return OnuItuPonAlarmSet_(request);
309 };
Jason Huang1d9cfce2020-05-20 22:58:47 +0800310
311 Status GetLogicalOnuDistanceZero(
312 ServerContext* context,
313 const openolt::Onu* request,
314 openolt::OnuLogicalDistance* response) override {
315 return GetLogicalOnuDistanceZero_(
316 request->intf_id(),
317 response);
318 };
319
320 Status GetLogicalOnuDistance(
321 ServerContext* context,
322 const openolt::Onu* request,
323 openolt::OnuLogicalDistance* response) override {
324 return GetLogicalOnuDistance_(
325 request->intf_id(),
326 request->onu_id(),
327 response);
328 };
Burak Gurdag74e3ab82020-12-17 13:35:06 +0000329
330 Status GetOnuStatistics(
331 ServerContext* context,
332 const openolt::Onu* request,
333 openolt::OnuStatistics* response) override {
334 return GetOnuStatistics_(
335 request->intf_id(),
336 request->onu_id(),
337 response);
338 }
339
340 Status GetGemPortStatistics(
341 ServerContext* context,
342 const openolt::OnuPacket* request,
343 openolt::GemPortStatistics* response) override {
344 return GetGemPortStatistics_(
345 request->intf_id(),
346 request->gemport_id(),
347 response);
348 }
Orhan Kupusogluec57af02021-05-12 12:38:17 +0000349
350 Status GetPonRxPower(
351 ServerContext* context,
352 const openolt::Onu* request,
353 openolt::PonRxPowerData* response) override {
354 return GetPonRxPower_(
355 request->intf_id(),
356 request->onu_id(),
357 response);
358 }
nikesh.krishnan331d38c2023-04-06 03:24:53 +0530359
360 Status GetOnuInfo(
361 ServerContext* context,
362 const openolt::Onu* request,
363 openolt::OnuInfo* response) override {
364 return GetOnuInfo_(
365 request->intf_id(),
366 request->onu_id(),
367 response);
368 }
369
370
371 Status GetPonInterfaceInfo(
372 ServerContext* context,
373 const openolt::Interface* request,
374 openolt::PonIntfInfo* response) override {
375 return GetPonInterfaceInfo_(
376 request->intf_id(),
377 response);
378 }
379
380
Akash Reddy Kankanaladec6e7e2025-05-22 09:49:28 +0530381 Status GetPonPortStatistics(
382 ServerContext* context,
383 const openolt::Interface* request,
384 common::PortStatistics* response) override {
385 return GetPonPortStatistics_(
386 request->intf_id(),
387 response);
388 }
389
390 Status GetNniPortStatistics(
391 ServerContext* context,
392 const openolt::Interface* request,
393 common::PortStatistics* response) override {
394 return GetNniPortStatistics_(
395 request->intf_id(),
396 response);
397 }
398
399 Status GetAllocIdStatistics(
400 ServerContext* context,
401 const openolt::OnuPacket* request,
402 openolt::OnuAllocIdStatistics* response) override {
403 return GetAllocIdStatistics_(
404 request->intf_id(),
405 request->alloc_id(),
406 response);
407 }
nikesh.krishnan331d38c2023-04-06 03:24:53 +0530408
409
Shad Ansari01b0e652018-04-05 21:02:53 +0000410};
411
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700412bool RunServer(int argc, char** argv) {
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530413 std::string ipAddress = "0.0.0.0";
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700414 bool tls_enabled = false;
415 std::pair<grpc_ssl_client_certificate_request_type, bool> grpc_security;
416 std::shared_ptr<grpc::ServerCredentials> credentials;
Shad Ansari01b0e652018-04-05 21:02:53 +0000417
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530418 for (int i = 1; i < argc; ++i) {
419 if(strcmp(argv[i-1], "--interface") == 0 || (strcmp(argv[i-1], "--intf") == 0)) {
420 ipAddress = get_ip_address(argv[i]);
421 break;
422 }
423 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000424
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700425 for (int i = 1; i < argc; ++i) {
426 if (strcmp(argv[i-1], "--enable-tls") == 0) {
427 grpc_security = get_grpc_tls_option(argv[i]);
428 if (grpc_security.second) {
429 tls_enabled = true;
430 } else {
431 std::cerr << "unknown security option: \"" << argv[i-1] << " " << argv[i] << "\"\n";
432 return false;
433 };
434 break;
435 }
436 }
437
438 if (tls_enabled) {
439 std::string dir_cert{"./keystore"};
440 auto read_root_crt = read_from_txt_file(dir_cert + "/root.crt");
441 auto read_server_key = read_from_txt_file(dir_cert + "/server.key");
442 auto read_server_crt = read_from_txt_file(dir_cert + "/server.crt");
443
444 if (read_root_crt.second && read_server_key.second && read_server_crt.second) {
445 std::cout << "certificate files read successfully\n";
446 } else {
447 std::cerr << std::boolalpha << "certificate files read failed - root.crt: " << read_root_crt.second
448 << ", server.key: " << read_server_key.second
449 << ", server.crt: " << read_server_crt.second << '\n';
450 return false;
451 }
452
453 std::string root_crt = read_root_crt.first;
454 std::string server_key = read_server_key.first;
455 std::string server_crt = read_server_crt.first;
456
457 grpc::SslServerCredentialsOptions ssl_opts{grpc_security.first};
458 ssl_opts.pem_root_certs = root_crt;
459 grpc::SslServerCredentialsOptions::PemKeyCertPair keycert = {server_key, server_crt};
460 ssl_opts.pem_key_cert_pairs.push_back(keycert);
461 credentials = grpc::SslServerCredentials(ssl_opts);
462 } else {
463 credentials = grpc::InsecureServerCredentials();
464 }
465
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530466 serverPort = ipAddress.append(":9191").c_str();
467 OpenoltService service;
468 std::string server_address(serverPort);
Girish Gowdra252f4972020-09-07 21:24:01 -0700469 ::ServerBuilder builder;
470 ::ResourceQuota quota;
471 quota.SetMaxThreads(GRPC_THREAD_POOL_SIZE);
472 builder.SetResourceQuota(quota);
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700473 builder.AddListeningPort(server_address, credentials);
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530474 builder.RegisterService(&service);
nick7be062f2018-05-25 17:52:56 -0400475
Arthur Syu094df162022-04-21 17:50:06 +0800476 server = builder.BuildAndStart();
nick7be062f2018-05-25 17:52:56 -0400477
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530478 time_t now;
479 time(&now);
480 signature = (int)now;
Shad Ansari01b0e652018-04-05 21:02:53 +0000481
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530482 std::cout << "Server listening on " << server_address
483 << ", connection signature : " << signature << std::endl;
484
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700485#ifdef TEST_MODE
486 server->Shutdown();
487#else
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530488 server->Wait();
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700489#endif
490
491 return true;
Shad Ansari01b0e652018-04-05 21:02:53 +0000492}