blob: 3199a51edc7c54ce21d254bf8def65e1fb5ae2d4 [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>
34#include <voltha_protos/tech_profile.grpc.pb.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000035
36using grpc::Server;
37using grpc::ServerBuilder;
Girish Gowdra252f4972020-09-07 21:24:01 -070038using grpc::ResourceQuota;
Shad Ansari01b0e652018-04-05 21:02:53 +000039using grpc::ServerContext;
40using grpc::ServerWriter;
Shad Ansarib7b0ced2018-05-11 21:53:32 +000041using grpc::Status;
Shad Ansari01b0e652018-04-05 21:02:53 +000042
43const char *serverPort = "0.0.0.0:9191";
nick7be062f2018-05-25 17:52:56 -040044int signature;
Shad Ansari01b0e652018-04-05 21:02:53 +000045
Shad Ansari627b5782018-08-13 22:49:32 +000046Queue<openolt::Indication> oltIndQ;
47
Shad Ansari01b0e652018-04-05 21:02:53 +000048class OpenoltService final : public openolt::Openolt::Service {
49
Nicolas Palpacuere3fc0d22018-08-02 16:51:05 -040050 Status DisableOlt(
51 ServerContext* context,
52 const openolt::Empty* request,
53 openolt::Empty* response) override {
54 return Disable_();
55 }
56
57 Status ReenableOlt(
58 ServerContext* context,
59 const openolt::Empty* request,
60 openolt::Empty* response) override {
61 return Reenable_();
62 }
63
Shad Ansari01b0e652018-04-05 21:02:53 +000064 Status ActivateOnu(
65 ServerContext* context,
66 const openolt::Onu* request,
67 openolt::Empty* response) override {
68 return ActivateOnu_(
69 request->intf_id(),
70 request->onu_id(),
71 ((request->serial_number()).vendor_id()).c_str(),
kesavandc1f2db92020-08-31 15:32:06 +053072 ((request->serial_number()).vendor_specific()).c_str(), request->pir(), request->omcc_encryption());
Shad Ansari01b0e652018-04-05 21:02:53 +000073 }
74
Jonathan Davis70c21812018-07-19 15:32:10 -040075 Status DeactivateOnu(
76 ServerContext* context,
77 const openolt::Onu* request,
78 openolt::Empty* response) override {
79 return DeactivateOnu_(
80 request->intf_id(),
81 request->onu_id(),
82 ((request->serial_number()).vendor_id()).c_str(),
83 ((request->serial_number()).vendor_specific()).c_str());
84 }
85
86 Status DeleteOnu(
87 ServerContext* context,
88 const openolt::Onu* request,
89 openolt::Empty* response) override {
90 return DeleteOnu_(
91 request->intf_id(),
92 request->onu_id(),
93 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070094 ((request->serial_number()).vendor_specific()).c_str());
Jonathan Davis70c21812018-07-19 15:32:10 -040095 }
96
Shad Ansari01b0e652018-04-05 21:02:53 +000097 Status OmciMsgOut(
98 ServerContext* context,
99 const openolt::OmciMsg* request,
100 openolt::Empty* response) override {
101 return OmciMsgOut_(
102 request->intf_id(),
103 request->onu_id(),
104 request->pkt());
105 }
106
Shad Ansarif2e27a42018-04-26 22:37:38 +0000107 Status OnuPacketOut(
108 ServerContext* context,
109 const openolt::OnuPacket* request,
110 openolt::Empty* response) override {
111 return OnuPacketOut_(
112 request->intf_id(),
113 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600114 request->port_no(),
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800115 request->gemport_id(),
Shad Ansarif2e27a42018-04-26 22:37:38 +0000116 request->pkt());
117 }
118
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400119 Status UplinkPacketOut(
120 ServerContext* context,
121 const openolt::UplinkPacket* request,
122 openolt::Empty* response) override {
123 return UplinkPacketOut_(
124 request->intf_id(),
125 request->pkt());
126 }
127
Shad Ansari01b0e652018-04-05 21:02:53 +0000128 Status FlowAdd(
129 ServerContext* context,
130 const openolt::Flow* request,
131 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700132 return FlowAddWrapper_(request);
133
Shad Ansari01b0e652018-04-05 21:02:53 +0000134 }
135
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400136 Status FlowRemove(
137 ServerContext* context,
138 const openolt::Flow* request,
139 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700140 return FlowRemoveWrapper_(request);
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400141 }
142
Shad Ansari01b0e652018-04-05 21:02:53 +0000143 Status EnableIndication(
144 ServerContext* context,
145 const ::openolt::Empty* request,
146 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000147
nick7be062f2018-05-25 17:52:56 -0400148 std::cout << "Connection to Voltha established. Indications enabled"
149 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400150
Burak Gurdag30db4822021-03-10 21:30:01 +0000151 if (state.previously_connected()) {
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400152 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400153 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400154 if (state.is_activated()){
155 // Adding extra olt indication of current state
156 openolt::Indication ind;
157 openolt::OltIndication* oltInd = new openolt::OltIndication();
158 if (state.is_activated()) {
159 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400160 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400161 } else {
162 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400163 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400164 }
165 ind.set_allocated_olt_ind(oltInd);
166 oltIndQ.push(ind);
167 }
168 }
169
Shad Ansariedef2132018-08-10 22:14:50 +0000170 state.connect();
171
172 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530173 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000174 if (ind.second == false) {
175 /* timeout - do lower priority periodic stuff like stats */
176 stats_collection();
177 continue;
178 }
179 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400180 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400181 if (!isConnected) {
182 //Lost connectivity to this Voltha instance
183 //Put the indication back in the queue for next connecting instance
184 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000185 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400186 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000187 //oltInd.release_olt_ind()
188 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400189
Shad Ansari01b0e652018-04-05 21:02:53 +0000190 return Status::OK;
191 }
nick7be062f2018-05-25 17:52:56 -0400192
193 Status HeartbeatCheck(
194 ServerContext* context,
195 const openolt::Empty* request,
196 openolt::Heartbeat* response) override {
197 response->set_heartbeat_signature(signature);
198
199 return Status::OK;
200 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400201
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400202 Status EnablePonIf(
203 ServerContext* context,
204 const openolt::Interface* request,
205 openolt::Empty* response) override {
206
207 return EnablePonIf_(request->intf_id());
208 }
209
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000210 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000211 ServerContext* context,
212 const openolt::Interface* request,
213 openolt::IntfIndication* response) override {
214
215 // TODO - Return the oper status of the pon interface
216 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000217 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000218
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400219 Status DisablePonIf(
220 ServerContext* context,
221 const openolt::Interface* request,
222 openolt::Empty* response) override {
223
224 return DisablePonIf_(request->intf_id());
225 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400226
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400227 Status CollectStatistics(
228 ServerContext* context,
229 const openolt::Empty* request,
230 openolt::Empty* response) override {
231
232 stats_collection();
233
234 return Status::OK;
235 }
236
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400237 Status Reboot(
238 ServerContext* context,
239 const openolt::Empty* request,
240 openolt::Empty* response) override {
241
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000242 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400243
244 return Status::OK;
245
246 }
247
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400248 Status GetDeviceInfo(
249 ServerContext* context,
250 const openolt::Empty* request,
251 openolt::DeviceInfo* response) override {
252
253 GetDeviceInfo_(response);
254
255 return Status::OK;
256
257 }
258
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800259 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700260 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800261 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700262 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000263 return CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700264 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400265
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800266 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700267 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800268 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700269 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000270 return RemoveTrafficSchedulers_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800271 };
272
273 Status CreateTrafficQueues(
274 ServerContext* context,
275 const tech_profile::TrafficQueues* request,
276 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000277 return CreateTrafficQueues_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800278 };
279
280 Status RemoveTrafficQueues(
281 ServerContext* context,
282 const tech_profile::TrafficQueues* request,
283 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000284 return RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700285 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400286
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000287 Status PerformGroupOperation(
288 ServerContext* context,
289 const openolt::Group* request,
290 openolt::Empty* response) override {
291 return PerformGroupOperation_(request);
292 };
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800293
Burak Gurdageb4ca2e2020-06-15 07:48:26 +0000294 Status DeleteGroup(
295 ServerContext* context,
296 const openolt::Group* request,
297 openolt::Empty* response) override {
298 return DeleteGroup_(request->group_id());
299 };
300
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800301 Status OnuItuPonAlarmSet(
302 ServerContext* context,
kesavandc1f2db92020-08-31 15:32:06 +0530303 const config::OnuItuPonAlarm* request,
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800304 openolt::Empty* response) override {
305 return OnuItuPonAlarmSet_(request);
306 };
Jason Huang1d9cfce2020-05-20 22:58:47 +0800307
308 Status GetLogicalOnuDistanceZero(
309 ServerContext* context,
310 const openolt::Onu* request,
311 openolt::OnuLogicalDistance* response) override {
312 return GetLogicalOnuDistanceZero_(
313 request->intf_id(),
314 response);
315 };
316
317 Status GetLogicalOnuDistance(
318 ServerContext* context,
319 const openolt::Onu* request,
320 openolt::OnuLogicalDistance* response) override {
321 return GetLogicalOnuDistance_(
322 request->intf_id(),
323 request->onu_id(),
324 response);
325 };
Burak Gurdag74e3ab82020-12-17 13:35:06 +0000326
327 Status GetOnuStatistics(
328 ServerContext* context,
329 const openolt::Onu* request,
330 openolt::OnuStatistics* response) override {
331 return GetOnuStatistics_(
332 request->intf_id(),
333 request->onu_id(),
334 response);
335 }
336
337 Status GetGemPortStatistics(
338 ServerContext* context,
339 const openolt::OnuPacket* request,
340 openolt::GemPortStatistics* response) override {
341 return GetGemPortStatistics_(
342 request->intf_id(),
343 request->gemport_id(),
344 response);
345 }
Orhan Kupusogluec57af02021-05-12 12:38:17 +0000346
347 Status GetPonRxPower(
348 ServerContext* context,
349 const openolt::Onu* request,
350 openolt::PonRxPowerData* response) override {
351 return GetPonRxPower_(
352 request->intf_id(),
353 request->onu_id(),
354 response);
355 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000356};
357
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700358bool RunServer(int argc, char** argv) {
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530359 std::string ipAddress = "0.0.0.0";
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700360 bool tls_enabled = false;
361 std::pair<grpc_ssl_client_certificate_request_type, bool> grpc_security;
362 std::shared_ptr<grpc::ServerCredentials> credentials;
Shad Ansari01b0e652018-04-05 21:02:53 +0000363
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530364 for (int i = 1; i < argc; ++i) {
365 if(strcmp(argv[i-1], "--interface") == 0 || (strcmp(argv[i-1], "--intf") == 0)) {
366 ipAddress = get_ip_address(argv[i]);
367 break;
368 }
369 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000370
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700371 for (int i = 1; i < argc; ++i) {
372 if (strcmp(argv[i-1], "--enable-tls") == 0) {
373 grpc_security = get_grpc_tls_option(argv[i]);
374 if (grpc_security.second) {
375 tls_enabled = true;
376 } else {
377 std::cerr << "unknown security option: \"" << argv[i-1] << " " << argv[i] << "\"\n";
378 return false;
379 };
380 break;
381 }
382 }
383
384 if (tls_enabled) {
385 std::string dir_cert{"./keystore"};
386 auto read_root_crt = read_from_txt_file(dir_cert + "/root.crt");
387 auto read_server_key = read_from_txt_file(dir_cert + "/server.key");
388 auto read_server_crt = read_from_txt_file(dir_cert + "/server.crt");
389
390 if (read_root_crt.second && read_server_key.second && read_server_crt.second) {
391 std::cout << "certificate files read successfully\n";
392 } else {
393 std::cerr << std::boolalpha << "certificate files read failed - root.crt: " << read_root_crt.second
394 << ", server.key: " << read_server_key.second
395 << ", server.crt: " << read_server_crt.second << '\n';
396 return false;
397 }
398
399 std::string root_crt = read_root_crt.first;
400 std::string server_key = read_server_key.first;
401 std::string server_crt = read_server_crt.first;
402
403 grpc::SslServerCredentialsOptions ssl_opts{grpc_security.first};
404 ssl_opts.pem_root_certs = root_crt;
405 grpc::SslServerCredentialsOptions::PemKeyCertPair keycert = {server_key, server_crt};
406 ssl_opts.pem_key_cert_pairs.push_back(keycert);
407 credentials = grpc::SslServerCredentials(ssl_opts);
408 } else {
409 credentials = grpc::InsecureServerCredentials();
410 }
411
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530412 serverPort = ipAddress.append(":9191").c_str();
413 OpenoltService service;
414 std::string server_address(serverPort);
Girish Gowdra252f4972020-09-07 21:24:01 -0700415 ::ServerBuilder builder;
416 ::ResourceQuota quota;
417 quota.SetMaxThreads(GRPC_THREAD_POOL_SIZE);
418 builder.SetResourceQuota(quota);
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700419 builder.AddListeningPort(server_address, credentials);
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530420 builder.RegisterService(&service);
nick7be062f2018-05-25 17:52:56 -0400421
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530422 std::unique_ptr<Server> server(builder.BuildAndStart());
nick7be062f2018-05-25 17:52:56 -0400423
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530424 time_t now;
425 time(&now);
426 signature = (int)now;
Shad Ansari01b0e652018-04-05 21:02:53 +0000427
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530428 std::cout << "Server listening on " << server_address
429 << ", connection signature : " << signature << std::endl;
430
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700431#ifdef TEST_MODE
432 server->Shutdown();
433#else
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530434 server->Wait();
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700435#endif
436
437 return true;
Shad Ansari01b0e652018-04-05 21:02:53 +0000438}