blob: 83a66f3c6b96a6f0089d6c80f4c87ff8ede76a05 [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;
Arthur Syu094df162022-04-21 17:50:06 +080045std::unique_ptr<Server> server;
Shad Ansari01b0e652018-04-05 21:02:53 +000046
Shad Ansari627b5782018-08-13 22:49:32 +000047Queue<openolt::Indication> oltIndQ;
48
Shad Ansari01b0e652018-04-05 21:02:53 +000049class OpenoltService final : public openolt::Openolt::Service {
50
Nicolas Palpacuere3fc0d22018-08-02 16:51:05 -040051 Status DisableOlt(
52 ServerContext* context,
53 const openolt::Empty* request,
54 openolt::Empty* response) override {
55 return Disable_();
56 }
57
58 Status ReenableOlt(
59 ServerContext* context,
60 const openolt::Empty* request,
61 openolt::Empty* response) override {
62 return Reenable_();
63 }
64
Shad Ansari01b0e652018-04-05 21:02:53 +000065 Status ActivateOnu(
66 ServerContext* context,
67 const openolt::Onu* request,
68 openolt::Empty* response) override {
69 return ActivateOnu_(
70 request->intf_id(),
71 request->onu_id(),
72 ((request->serial_number()).vendor_id()).c_str(),
kesavandc1f2db92020-08-31 15:32:06 +053073 ((request->serial_number()).vendor_specific()).c_str(), request->pir(), request->omcc_encryption());
Shad Ansari01b0e652018-04-05 21:02:53 +000074 }
75
Jonathan Davis70c21812018-07-19 15:32:10 -040076 Status DeactivateOnu(
77 ServerContext* context,
78 const openolt::Onu* request,
79 openolt::Empty* response) override {
80 return DeactivateOnu_(
81 request->intf_id(),
82 request->onu_id(),
83 ((request->serial_number()).vendor_id()).c_str(),
84 ((request->serial_number()).vendor_specific()).c_str());
85 }
86
87 Status DeleteOnu(
88 ServerContext* context,
89 const openolt::Onu* request,
90 openolt::Empty* response) override {
91 return DeleteOnu_(
92 request->intf_id(),
93 request->onu_id(),
94 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070095 ((request->serial_number()).vendor_specific()).c_str());
Jonathan Davis70c21812018-07-19 15:32:10 -040096 }
97
Shad Ansari01b0e652018-04-05 21:02:53 +000098 Status OmciMsgOut(
99 ServerContext* context,
100 const openolt::OmciMsg* request,
101 openolt::Empty* response) override {
102 return OmciMsgOut_(
103 request->intf_id(),
104 request->onu_id(),
105 request->pkt());
106 }
107
Shad Ansarif2e27a42018-04-26 22:37:38 +0000108 Status OnuPacketOut(
109 ServerContext* context,
110 const openolt::OnuPacket* request,
111 openolt::Empty* response) override {
112 return OnuPacketOut_(
113 request->intf_id(),
114 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600115 request->port_no(),
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800116 request->gemport_id(),
Shad Ansarif2e27a42018-04-26 22:37:38 +0000117 request->pkt());
118 }
119
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400120 Status UplinkPacketOut(
121 ServerContext* context,
122 const openolt::UplinkPacket* request,
123 openolt::Empty* response) override {
124 return UplinkPacketOut_(
125 request->intf_id(),
126 request->pkt());
127 }
128
Shad Ansari01b0e652018-04-05 21:02:53 +0000129 Status FlowAdd(
130 ServerContext* context,
131 const openolt::Flow* request,
132 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700133 return FlowAddWrapper_(request);
134
Shad Ansari01b0e652018-04-05 21:02:53 +0000135 }
136
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400137 Status FlowRemove(
138 ServerContext* context,
139 const openolt::Flow* request,
140 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700141 return FlowRemoveWrapper_(request);
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400142 }
143
Shad Ansari01b0e652018-04-05 21:02:53 +0000144 Status EnableIndication(
145 ServerContext* context,
146 const ::openolt::Empty* request,
147 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000148
nick7be062f2018-05-25 17:52:56 -0400149 std::cout << "Connection to Voltha established. Indications enabled"
150 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400151
Burak Gurdag30db4822021-03-10 21:30:01 +0000152 if (state.previously_connected()) {
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400153 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400154 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400155 if (state.is_activated()){
156 // Adding extra olt indication of current state
157 openolt::Indication ind;
158 openolt::OltIndication* oltInd = new openolt::OltIndication();
159 if (state.is_activated()) {
160 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400161 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400162 } else {
163 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400164 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400165 }
166 ind.set_allocated_olt_ind(oltInd);
167 oltIndQ.push(ind);
168 }
169 }
170
Shad Ansariedef2132018-08-10 22:14:50 +0000171 state.connect();
172
173 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530174 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000175 if (ind.second == false) {
176 /* timeout - do lower priority periodic stuff like stats */
177 stats_collection();
178 continue;
179 }
180 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400181 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400182 if (!isConnected) {
183 //Lost connectivity to this Voltha instance
184 //Put the indication back in the queue for next connecting instance
185 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000186 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400187 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000188 //oltInd.release_olt_ind()
189 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400190
Shad Ansari01b0e652018-04-05 21:02:53 +0000191 return Status::OK;
192 }
nick7be062f2018-05-25 17:52:56 -0400193
194 Status HeartbeatCheck(
195 ServerContext* context,
196 const openolt::Empty* request,
197 openolt::Heartbeat* response) override {
198 response->set_heartbeat_signature(signature);
199
200 return Status::OK;
201 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400202
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400203 Status EnablePonIf(
204 ServerContext* context,
205 const openolt::Interface* request,
206 openolt::Empty* response) override {
207
208 return EnablePonIf_(request->intf_id());
209 }
210
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000211 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000212 ServerContext* context,
213 const openolt::Interface* request,
214 openolt::IntfIndication* response) override {
215
216 // TODO - Return the oper status of the pon interface
217 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000218 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000219
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400220 Status DisablePonIf(
221 ServerContext* context,
222 const openolt::Interface* request,
223 openolt::Empty* response) override {
224
225 return DisablePonIf_(request->intf_id());
226 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400227
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400228 Status CollectStatistics(
229 ServerContext* context,
230 const openolt::Empty* request,
231 openolt::Empty* response) override {
232
233 stats_collection();
234
235 return Status::OK;
236 }
237
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400238 Status Reboot(
239 ServerContext* context,
240 const openolt::Empty* request,
241 openolt::Empty* response) override {
242
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000243 uint8_t ret = system("shutdown -r now");
Arthur Syu094df162022-04-21 17:50:06 +0800244 server->Shutdown();
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400245
246 return Status::OK;
247
248 }
249
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400250 Status GetDeviceInfo(
251 ServerContext* context,
252 const openolt::Empty* request,
253 openolt::DeviceInfo* response) override {
254
255 GetDeviceInfo_(response);
256
257 return Status::OK;
258
259 }
260
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800261 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700262 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800263 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700264 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000265 return CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700266 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400267
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800268 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700269 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800270 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700271 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000272 return RemoveTrafficSchedulers_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800273 };
274
275 Status CreateTrafficQueues(
276 ServerContext* context,
277 const tech_profile::TrafficQueues* request,
278 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000279 return CreateTrafficQueues_(request);
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800280 };
281
282 Status RemoveTrafficQueues(
283 ServerContext* context,
284 const tech_profile::TrafficQueues* request,
285 openolt::Empty* response) override {
Burak Gurdag9c039982021-11-23 11:23:57 +0000286 return RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700287 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400288
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000289 Status PerformGroupOperation(
290 ServerContext* context,
291 const openolt::Group* request,
292 openolt::Empty* response) override {
293 return PerformGroupOperation_(request);
294 };
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800295
Burak Gurdageb4ca2e2020-06-15 07:48:26 +0000296 Status DeleteGroup(
297 ServerContext* context,
298 const openolt::Group* request,
299 openolt::Empty* response) override {
300 return DeleteGroup_(request->group_id());
301 };
302
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800303 Status OnuItuPonAlarmSet(
304 ServerContext* context,
kesavandc1f2db92020-08-31 15:32:06 +0530305 const config::OnuItuPonAlarm* request,
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800306 openolt::Empty* response) override {
307 return OnuItuPonAlarmSet_(request);
308 };
Jason Huang1d9cfce2020-05-20 22:58:47 +0800309
310 Status GetLogicalOnuDistanceZero(
311 ServerContext* context,
312 const openolt::Onu* request,
313 openolt::OnuLogicalDistance* response) override {
314 return GetLogicalOnuDistanceZero_(
315 request->intf_id(),
316 response);
317 };
318
319 Status GetLogicalOnuDistance(
320 ServerContext* context,
321 const openolt::Onu* request,
322 openolt::OnuLogicalDistance* response) override {
323 return GetLogicalOnuDistance_(
324 request->intf_id(),
325 request->onu_id(),
326 response);
327 };
Burak Gurdag74e3ab82020-12-17 13:35:06 +0000328
329 Status GetOnuStatistics(
330 ServerContext* context,
331 const openolt::Onu* request,
332 openolt::OnuStatistics* response) override {
333 return GetOnuStatistics_(
334 request->intf_id(),
335 request->onu_id(),
336 response);
337 }
338
339 Status GetGemPortStatistics(
340 ServerContext* context,
341 const openolt::OnuPacket* request,
342 openolt::GemPortStatistics* response) override {
343 return GetGemPortStatistics_(
344 request->intf_id(),
345 request->gemport_id(),
346 response);
347 }
Orhan Kupusogluec57af02021-05-12 12:38:17 +0000348
349 Status GetPonRxPower(
350 ServerContext* context,
351 const openolt::Onu* request,
352 openolt::PonRxPowerData* response) override {
353 return GetPonRxPower_(
354 request->intf_id(),
355 request->onu_id(),
356 response);
357 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000358};
359
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700360bool RunServer(int argc, char** argv) {
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530361 std::string ipAddress = "0.0.0.0";
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700362 bool tls_enabled = false;
363 std::pair<grpc_ssl_client_certificate_request_type, bool> grpc_security;
364 std::shared_ptr<grpc::ServerCredentials> credentials;
Shad Ansari01b0e652018-04-05 21:02:53 +0000365
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530366 for (int i = 1; i < argc; ++i) {
367 if(strcmp(argv[i-1], "--interface") == 0 || (strcmp(argv[i-1], "--intf") == 0)) {
368 ipAddress = get_ip_address(argv[i]);
369 break;
370 }
371 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000372
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700373 for (int i = 1; i < argc; ++i) {
374 if (strcmp(argv[i-1], "--enable-tls") == 0) {
375 grpc_security = get_grpc_tls_option(argv[i]);
376 if (grpc_security.second) {
377 tls_enabled = true;
378 } else {
379 std::cerr << "unknown security option: \"" << argv[i-1] << " " << argv[i] << "\"\n";
380 return false;
381 };
382 break;
383 }
384 }
385
386 if (tls_enabled) {
387 std::string dir_cert{"./keystore"};
388 auto read_root_crt = read_from_txt_file(dir_cert + "/root.crt");
389 auto read_server_key = read_from_txt_file(dir_cert + "/server.key");
390 auto read_server_crt = read_from_txt_file(dir_cert + "/server.crt");
391
392 if (read_root_crt.second && read_server_key.second && read_server_crt.second) {
393 std::cout << "certificate files read successfully\n";
394 } else {
395 std::cerr << std::boolalpha << "certificate files read failed - root.crt: " << read_root_crt.second
396 << ", server.key: " << read_server_key.second
397 << ", server.crt: " << read_server_crt.second << '\n';
398 return false;
399 }
400
401 std::string root_crt = read_root_crt.first;
402 std::string server_key = read_server_key.first;
403 std::string server_crt = read_server_crt.first;
404
405 grpc::SslServerCredentialsOptions ssl_opts{grpc_security.first};
406 ssl_opts.pem_root_certs = root_crt;
407 grpc::SslServerCredentialsOptions::PemKeyCertPair keycert = {server_key, server_crt};
408 ssl_opts.pem_key_cert_pairs.push_back(keycert);
409 credentials = grpc::SslServerCredentials(ssl_opts);
410 } else {
411 credentials = grpc::InsecureServerCredentials();
412 }
413
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530414 serverPort = ipAddress.append(":9191").c_str();
415 OpenoltService service;
416 std::string server_address(serverPort);
Girish Gowdra252f4972020-09-07 21:24:01 -0700417 ::ServerBuilder builder;
418 ::ResourceQuota quota;
419 quota.SetMaxThreads(GRPC_THREAD_POOL_SIZE);
420 builder.SetResourceQuota(quota);
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700421 builder.AddListeningPort(server_address, credentials);
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530422 builder.RegisterService(&service);
nick7be062f2018-05-25 17:52:56 -0400423
Arthur Syu094df162022-04-21 17:50:06 +0800424 server = builder.BuildAndStart();
nick7be062f2018-05-25 17:52:56 -0400425
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530426 time_t now;
427 time(&now);
428 signature = (int)now;
Shad Ansari01b0e652018-04-05 21:02:53 +0000429
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530430 std::cout << "Server listening on " << server_address
431 << ", connection signature : " << signature << std::endl;
432
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700433#ifdef TEST_MODE
434 server->Shutdown();
435#else
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530436 server->Wait();
Orhan Kupusoglu1fd77072021-03-23 08:13:25 -0700437#endif
438
439 return true;
Shad Ansari01b0e652018-04-05 21:02:53 +0000440}