blob: 0294a34f5dcc93243b111be1db5365226d0041d3 [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"
Shad Ansari01b0e652018-04-05 21:02:53 +000030
Shad Ansarib7b0ced2018-05-11 21:53:32 +000031#include <grpc++/grpc++.h>
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +000032#include <voltha_protos/openolt.grpc.pb.h>
33#include <voltha_protos/tech_profile.grpc.pb.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000034
35using grpc::Server;
36using grpc::ServerBuilder;
Girish Gowdra252f4972020-09-07 21:24:01 -070037using grpc::ResourceQuota;
Shad Ansari01b0e652018-04-05 21:02:53 +000038using 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(),
kesavandc1f2db92020-08-31 15:32:06 +053071 ((request->serial_number()).vendor_specific()).c_str(), request->pir(), request->omcc_encryption());
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(),
124 request->pkt());
125 }
126
Shad Ansari01b0e652018-04-05 21:02:53 +0000127 Status FlowAdd(
128 ServerContext* context,
129 const openolt::Flow* request,
130 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700131 return FlowAddWrapper_(request);
132
Shad Ansari01b0e652018-04-05 21:02:53 +0000133 }
134
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400135 Status FlowRemove(
136 ServerContext* context,
137 const openolt::Flow* request,
138 openolt::Empty* response) override {
Girish Gowdra252f4972020-09-07 21:24:01 -0700139 return FlowRemoveWrapper_(request);
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400140 }
141
Shad Ansari01b0e652018-04-05 21:02:53 +0000142 Status EnableIndication(
143 ServerContext* context,
144 const ::openolt::Empty* request,
145 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000146
nick7be062f2018-05-25 17:52:56 -0400147 std::cout << "Connection to Voltha established. Indications enabled"
148 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400149
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400150 if (state.previsouly_connected()) {
151 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400152 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400153 if (state.is_activated()){
154 // Adding extra olt indication of current state
155 openolt::Indication ind;
156 openolt::OltIndication* oltInd = new openolt::OltIndication();
157 if (state.is_activated()) {
158 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400159 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400160 } else {
161 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400162 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400163 }
164 ind.set_allocated_olt_ind(oltInd);
165 oltIndQ.push(ind);
166 }
167 }
168
Shad Ansariedef2132018-08-10 22:14:50 +0000169 state.connect();
170
171 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530172 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000173 if (ind.second == false) {
174 /* timeout - do lower priority periodic stuff like stats */
175 stats_collection();
176 continue;
177 }
178 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400179 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400180 if (!isConnected) {
181 //Lost connectivity to this Voltha instance
182 //Put the indication back in the queue for next connecting instance
183 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000184 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400185 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000186 //oltInd.release_olt_ind()
187 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400188
Shad Ansari01b0e652018-04-05 21:02:53 +0000189 return Status::OK;
190 }
nick7be062f2018-05-25 17:52:56 -0400191
192 Status HeartbeatCheck(
193 ServerContext* context,
194 const openolt::Empty* request,
195 openolt::Heartbeat* response) override {
196 response->set_heartbeat_signature(signature);
197
198 return Status::OK;
199 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400200
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400201 Status EnablePonIf(
202 ServerContext* context,
203 const openolt::Interface* request,
204 openolt::Empty* response) override {
205
206 return EnablePonIf_(request->intf_id());
207 }
208
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000209 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000210 ServerContext* context,
211 const openolt::Interface* request,
212 openolt::IntfIndication* response) override {
213
214 // TODO - Return the oper status of the pon interface
215 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000216 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000217
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400218 Status DisablePonIf(
219 ServerContext* context,
220 const openolt::Interface* request,
221 openolt::Empty* response) override {
222
223 return DisablePonIf_(request->intf_id());
224 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400225
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400226 Status CollectStatistics(
227 ServerContext* context,
228 const openolt::Empty* request,
229 openolt::Empty* response) override {
230
231 stats_collection();
232
233 return Status::OK;
234 }
235
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400236 Status Reboot(
237 ServerContext* context,
238 const openolt::Empty* request,
239 openolt::Empty* response) override {
240
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000241 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400242
243 return Status::OK;
244
245 }
246
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400247 Status GetDeviceInfo(
248 ServerContext* context,
249 const openolt::Empty* request,
250 openolt::DeviceInfo* response) override {
251
252 GetDeviceInfo_(response);
253
254 return Status::OK;
255
256 }
257
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800258 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700259 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800260 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700261 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800262 CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700263 return Status::OK;
264 };
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 {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800270 RemoveTrafficSchedulers_(request);
271 return Status::OK;
272 };
273
274 Status CreateTrafficQueues(
275 ServerContext* context,
276 const tech_profile::TrafficQueues* request,
277 openolt::Empty* response) override {
278 CreateTrafficQueues_(request);
279 return Status::OK;
280 };
281
282 Status RemoveTrafficQueues(
283 ServerContext* context,
284 const tech_profile::TrafficQueues* request,
285 openolt::Empty* response) override {
286 RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700287 return Status::OK;
288 };
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 };
Shad Ansari01b0e652018-04-05 21:02:53 +0000329};
330
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530331void RunServer(int argc, char** argv) {
332 std::string ipAddress = "0.0.0.0";
Shad Ansari01b0e652018-04-05 21:02:53 +0000333
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530334 for (int i = 1; i < argc; ++i) {
335 if(strcmp(argv[i-1], "--interface") == 0 || (strcmp(argv[i-1], "--intf") == 0)) {
336 ipAddress = get_ip_address(argv[i]);
337 break;
338 }
339 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000340
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530341 serverPort = ipAddress.append(":9191").c_str();
342 OpenoltService service;
343 std::string server_address(serverPort);
Girish Gowdra252f4972020-09-07 21:24:01 -0700344 ::ServerBuilder builder;
345 ::ResourceQuota quota;
346 quota.SetMaxThreads(GRPC_THREAD_POOL_SIZE);
347 builder.SetResourceQuota(quota);
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530348 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
349 builder.RegisterService(&service);
nick7be062f2018-05-25 17:52:56 -0400350
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530351 std::unique_ptr<Server> server(builder.BuildAndStart());
nick7be062f2018-05-25 17:52:56 -0400352
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530353 time_t now;
354 time(&now);
355 signature = (int)now;
Shad Ansari01b0e652018-04-05 21:02:53 +0000356
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530357 std::cout << "Server listening on " << server_address
358 << ", connection signature : " << signature << std::endl;
359
360 server->Wait();
Shad Ansari01b0e652018-04-05 21:02:53 +0000361}