blob: e33938b765b03adbfce38048c21e883e24a12541 [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(),
144 request->group_id());
Shad Ansari01b0e652018-04-05 21:02:53 +0000145 }
146
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400147 Status FlowRemove(
148 ServerContext* context,
149 const openolt::Flow* request,
150 openolt::Empty* response) override {
151 return FlowRemove_(
152 request->flow_id(),
153 request->flow_type());
154 }
155
Shad Ansari01b0e652018-04-05 21:02:53 +0000156 Status EnableIndication(
157 ServerContext* context,
158 const ::openolt::Empty* request,
159 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000160
nick7be062f2018-05-25 17:52:56 -0400161 std::cout << "Connection to Voltha established. Indications enabled"
162 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400163
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400164 if (state.previsouly_connected()) {
165 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400166 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400167 if (state.is_activated()){
168 // Adding extra olt indication of current state
169 openolt::Indication ind;
170 openolt::OltIndication* oltInd = new openolt::OltIndication();
171 if (state.is_activated()) {
172 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400173 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400174 } else {
175 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400176 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400177 }
178 ind.set_allocated_olt_ind(oltInd);
179 oltIndQ.push(ind);
180 }
181 }
182
Shad Ansariedef2132018-08-10 22:14:50 +0000183 state.connect();
184
185 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530186 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000187 if (ind.second == false) {
188 /* timeout - do lower priority periodic stuff like stats */
189 stats_collection();
190 continue;
191 }
192 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400193 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400194 if (!isConnected) {
195 //Lost connectivity to this Voltha instance
196 //Put the indication back in the queue for next connecting instance
197 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000198 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400199 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000200 //oltInd.release_olt_ind()
201 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400202
Shad Ansari01b0e652018-04-05 21:02:53 +0000203 return Status::OK;
204 }
nick7be062f2018-05-25 17:52:56 -0400205
206 Status HeartbeatCheck(
207 ServerContext* context,
208 const openolt::Empty* request,
209 openolt::Heartbeat* response) override {
210 response->set_heartbeat_signature(signature);
211
212 return Status::OK;
213 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400214
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400215 Status EnablePonIf(
216 ServerContext* context,
217 const openolt::Interface* request,
218 openolt::Empty* response) override {
219
220 return EnablePonIf_(request->intf_id());
221 }
222
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000223 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000224 ServerContext* context,
225 const openolt::Interface* request,
226 openolt::IntfIndication* response) override {
227
228 // TODO - Return the oper status of the pon interface
229 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000230 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000231
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400232 Status DisablePonIf(
233 ServerContext* context,
234 const openolt::Interface* request,
235 openolt::Empty* response) override {
236
237 return DisablePonIf_(request->intf_id());
238 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400239
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400240 Status CollectStatistics(
241 ServerContext* context,
242 const openolt::Empty* request,
243 openolt::Empty* response) override {
244
245 stats_collection();
246
247 return Status::OK;
248 }
249
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400250 Status Reboot(
251 ServerContext* context,
252 const openolt::Empty* request,
253 openolt::Empty* response) override {
254
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000255 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400256
257 return Status::OK;
258
259 }
260
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400261 Status GetDeviceInfo(
262 ServerContext* context,
263 const openolt::Empty* request,
264 openolt::DeviceInfo* response) override {
265
266 GetDeviceInfo_(response);
267
268 return Status::OK;
269
270 }
271
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800272 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700273 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800274 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700275 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800276 CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700277 return Status::OK;
278 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400279
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800280 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700281 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800282 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700283 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800284 RemoveTrafficSchedulers_(request);
285 return Status::OK;
286 };
287
288 Status CreateTrafficQueues(
289 ServerContext* context,
290 const tech_profile::TrafficQueues* request,
291 openolt::Empty* response) override {
292 CreateTrafficQueues_(request);
293 return Status::OK;
294 };
295
296 Status RemoveTrafficQueues(
297 ServerContext* context,
298 const tech_profile::TrafficQueues* request,
299 openolt::Empty* response) override {
300 RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700301 return Status::OK;
302 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400303
Burak Gurdagc78b9e12019-11-29 11:14:51 +0000304 Status PerformGroupOperation(
305 ServerContext* context,
306 const openolt::Group* request,
307 openolt::Empty* response) override {
308 return PerformGroupOperation_(request);
309 };
Shad Ansari01b0e652018-04-05 21:02:53 +0000310};
311
312void RunServer() {
313 OpenoltService service;
314 std::string server_address(serverPort);
315 ServerBuilder builder;
316
317 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
318 builder.RegisterService(&service);
319
320 std::unique_ptr<Server> server(builder.BuildAndStart());
321
nick7be062f2018-05-25 17:52:56 -0400322 time_t now;
323 time(&now);
324 signature = (int)now;
325
326 std::cout << "Server listening on " << server_address
327 << ", connection signature : " << signature << std::endl;
328
Shad Ansari01b0e652018-04-05 21:02:53 +0000329
330 server->Wait();
331}