blob: 5a30f3732bf7824b72bcfdf1854c05d955e49750 [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;
37using grpc::ServerContext;
38using grpc::ServerWriter;
Shad Ansarib7b0ced2018-05-11 21:53:32 +000039using grpc::Status;
Shad Ansari01b0e652018-04-05 21:02:53 +000040
41const char *serverPort = "0.0.0.0:9191";
nick7be062f2018-05-25 17:52:56 -040042int signature;
Shad Ansari01b0e652018-04-05 21:02:53 +000043
Shad Ansari627b5782018-08-13 22:49:32 +000044Queue<openolt::Indication> oltIndQ;
45
Shad Ansari01b0e652018-04-05 21:02:53 +000046class OpenoltService final : public openolt::Openolt::Service {
47
Nicolas Palpacuere3fc0d22018-08-02 16:51:05 -040048 Status DisableOlt(
49 ServerContext* context,
50 const openolt::Empty* request,
51 openolt::Empty* response) override {
52 return Disable_();
53 }
54
55 Status ReenableOlt(
56 ServerContext* context,
57 const openolt::Empty* request,
58 openolt::Empty* response) override {
59 return Reenable_();
60 }
61
Shad Ansari01b0e652018-04-05 21:02:53 +000062 Status ActivateOnu(
63 ServerContext* context,
64 const openolt::Onu* request,
65 openolt::Empty* response) override {
66 return ActivateOnu_(
67 request->intf_id(),
68 request->onu_id(),
69 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070070 ((request->serial_number()).vendor_specific()).c_str(), request->pir());
Shad Ansari01b0e652018-04-05 21:02:53 +000071 }
72
Jonathan Davis70c21812018-07-19 15:32:10 -040073 Status DeactivateOnu(
74 ServerContext* context,
75 const openolt::Onu* request,
76 openolt::Empty* response) override {
77 return DeactivateOnu_(
78 request->intf_id(),
79 request->onu_id(),
80 ((request->serial_number()).vendor_id()).c_str(),
81 ((request->serial_number()).vendor_specific()).c_str());
82 }
83
84 Status DeleteOnu(
85 ServerContext* context,
86 const openolt::Onu* request,
87 openolt::Empty* response) override {
88 return DeleteOnu_(
89 request->intf_id(),
90 request->onu_id(),
91 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -070092 ((request->serial_number()).vendor_specific()).c_str());
Jonathan Davis70c21812018-07-19 15:32:10 -040093 }
94
Shad Ansari01b0e652018-04-05 21:02:53 +000095 Status OmciMsgOut(
96 ServerContext* context,
97 const openolt::OmciMsg* request,
98 openolt::Empty* response) override {
99 return OmciMsgOut_(
100 request->intf_id(),
101 request->onu_id(),
102 request->pkt());
103 }
104
Shad Ansarif2e27a42018-04-26 22:37:38 +0000105 Status OnuPacketOut(
106 ServerContext* context,
107 const openolt::OnuPacket* request,
108 openolt::Empty* response) override {
109 return OnuPacketOut_(
110 request->intf_id(),
111 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600112 request->port_no(),
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800113 request->gemport_id(),
Shad Ansarif2e27a42018-04-26 22:37:38 +0000114 request->pkt());
115 }
116
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400117 Status UplinkPacketOut(
118 ServerContext* context,
119 const openolt::UplinkPacket* request,
120 openolt::Empty* response) override {
121 return UplinkPacketOut_(
122 request->intf_id(),
123 request->pkt());
124 }
125
Shad Ansari01b0e652018-04-05 21:02:53 +0000126 Status FlowAdd(
127 ServerContext* context,
128 const openolt::Flow* request,
129 openolt::Empty* response) override {
130 return FlowAdd_(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700131 request->access_intf_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000132 request->onu_id(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600133 request->uni_id(),
134 request->port_no(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000135 request->flow_id(),
136 request->flow_type(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700137 request->alloc_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000138 request->network_intf_id(),
139 request->gemport_id(),
140 request->classifier(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700141 request->action(),
Craig Lutgen967a1d02018-11-27 10:41:51 -0600142 request->priority(),
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000143 request->cookie(),
Burak Gurdag2f2618c2020-04-23 13:20:30 +0000144 request->group_id(),
145 request->tech_profile_id());
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()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530187 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000188 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
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000224 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000225 ServerContext* context,
226 const openolt::Interface* request,
227 openolt::IntfIndication* response) override {
228
229 // TODO - Return the oper status of the pon interface
230 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000231 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000232
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400233 Status DisablePonIf(
234 ServerContext* context,
235 const openolt::Interface* request,
236 openolt::Empty* response) override {
237
238 return DisablePonIf_(request->intf_id());
239 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400240
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400241 Status CollectStatistics(
242 ServerContext* context,
243 const openolt::Empty* request,
244 openolt::Empty* response) override {
245
246 stats_collection();
247
248 return Status::OK;
249 }
250
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400251 Status Reboot(
252 ServerContext* context,
253 const openolt::Empty* request,
254 openolt::Empty* response) override {
255
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000256 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400257
258 return Status::OK;
259
260 }
261
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400262 Status GetDeviceInfo(
263 ServerContext* context,
264 const openolt::Empty* request,
265 openolt::DeviceInfo* response) override {
266
267 GetDeviceInfo_(response);
268
269 return Status::OK;
270
271 }
272
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800273 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700274 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800275 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700276 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800277 CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700278 return Status::OK;
279 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400280
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800281 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700282 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800283 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700284 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800285 RemoveTrafficSchedulers_(request);
286 return Status::OK;
287 };
288
289 Status CreateTrafficQueues(
290 ServerContext* context,
291 const tech_profile::TrafficQueues* request,
292 openolt::Empty* response) override {
293 CreateTrafficQueues_(request);
294 return Status::OK;
295 };
296
297 Status RemoveTrafficQueues(
298 ServerContext* context,
299 const tech_profile::TrafficQueues* request,
300 openolt::Empty* response) override {
301 RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700302 return Status::OK;
303 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400304
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000305 Status PerformGroupOperation(
306 ServerContext* context,
307 const openolt::Group* request,
308 openolt::Empty* response) override {
309 return PerformGroupOperation_(request);
310 };
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800311
Burak Gurdageb4ca2e2020-06-15 07:48:26 +0000312 Status DeleteGroup(
313 ServerContext* context,
314 const openolt::Group* request,
315 openolt::Empty* response) override {
316 return DeleteGroup_(request->group_id());
317 };
318
Jason Huang5d9ab1a2020-04-15 16:53:49 +0800319 Status OnuItuPonAlarmSet(
320 ServerContext* context,
321 const openolt::OnuItuPonAlarm* request,
322 openolt::Empty* response) override {
323 return OnuItuPonAlarmSet_(request);
324 };
Jason Huang1d9cfce2020-05-20 22:58:47 +0800325
326 Status GetLogicalOnuDistanceZero(
327 ServerContext* context,
328 const openolt::Onu* request,
329 openolt::OnuLogicalDistance* response) override {
330 return GetLogicalOnuDistanceZero_(
331 request->intf_id(),
332 response);
333 };
334
335 Status GetLogicalOnuDistance(
336 ServerContext* context,
337 const openolt::Onu* request,
338 openolt::OnuLogicalDistance* response) override {
339 return GetLogicalOnuDistance_(
340 request->intf_id(),
341 request->onu_id(),
342 response);
343 };
Shad Ansari01b0e652018-04-05 21:02:53 +0000344};
345
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530346void RunServer(int argc, char** argv) {
347 std::string ipAddress = "0.0.0.0";
Shad Ansari01b0e652018-04-05 21:02:53 +0000348
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530349 for (int i = 1; i < argc; ++i) {
350 if(strcmp(argv[i-1], "--interface") == 0 || (strcmp(argv[i-1], "--intf") == 0)) {
351 ipAddress = get_ip_address(argv[i]);
352 break;
353 }
354 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000355
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530356 serverPort = ipAddress.append(":9191").c_str();
357 OpenoltService service;
358 std::string server_address(serverPort);
359 ServerBuilder builder;
Shad Ansari01b0e652018-04-05 21:02:53 +0000360
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530361 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
362 builder.RegisterService(&service);
nick7be062f2018-05-25 17:52:56 -0400363
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530364 std::unique_ptr<Server> server(builder.BuildAndStart());
nick7be062f2018-05-25 17:52:56 -0400365
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530366 time_t now;
367 time(&now);
368 signature = (int)now;
Shad Ansari01b0e652018-04-05 21:02:53 +0000369
Thiyagarajan Subramani03bc66f2020-04-01 15:58:53 +0530370 std::cout << "Server listening on " << server_address
371 << ", connection signature : " << signature << std::endl;
372
373 server->Wait();
Shad Ansari01b0e652018-04-05 21:02:53 +0000374}