blob: 1c67137c77f3acb3c7a5b3f580e57c78ae986553 [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(),
143 request->cookie());
Shad Ansari01b0e652018-04-05 21:02:53 +0000144 }
145
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400146 Status FlowRemove(
147 ServerContext* context,
148 const openolt::Flow* request,
149 openolt::Empty* response) override {
150 return FlowRemove_(
151 request->flow_id(),
152 request->flow_type());
153 }
154
Shad Ansari01b0e652018-04-05 21:02:53 +0000155 Status EnableIndication(
156 ServerContext* context,
157 const ::openolt::Empty* request,
158 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000159
nick7be062f2018-05-25 17:52:56 -0400160 std::cout << "Connection to Voltha established. Indications enabled"
161 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400162
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400163 if (state.previsouly_connected()) {
164 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400165 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400166 if (state.is_activated()){
167 // Adding extra olt indication of current state
168 openolt::Indication ind;
169 openolt::OltIndication* oltInd = new openolt::OltIndication();
170 if (state.is_activated()) {
171 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400172 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400173 } else {
174 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400175 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400176 }
177 ind.set_allocated_olt_ind(oltInd);
178 oltIndQ.push(ind);
179 }
180 }
181
Shad Ansariedef2132018-08-10 22:14:50 +0000182 state.connect();
183
184 while (state.is_connected()) {
Girish Gowdra96461052019-11-22 20:13:59 +0530185 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000);
Shad Ansariedef2132018-08-10 22:14:50 +0000186 if (ind.second == false) {
187 /* timeout - do lower priority periodic stuff like stats */
188 stats_collection();
189 continue;
190 }
191 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400192 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400193 if (!isConnected) {
194 //Lost connectivity to this Voltha instance
195 //Put the indication back in the queue for next connecting instance
196 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000197 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400198 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000199 //oltInd.release_olt_ind()
200 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400201
Shad Ansari01b0e652018-04-05 21:02:53 +0000202 return Status::OK;
203 }
nick7be062f2018-05-25 17:52:56 -0400204
205 Status HeartbeatCheck(
206 ServerContext* context,
207 const openolt::Empty* request,
208 openolt::Heartbeat* response) override {
209 response->set_heartbeat_signature(signature);
210
211 return Status::OK;
212 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400213
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400214 Status EnablePonIf(
215 ServerContext* context,
216 const openolt::Interface* request,
217 openolt::Empty* response) override {
218
219 return EnablePonIf_(request->intf_id());
220 }
221
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000222 /*Status GetPonIf(
Shad Ansaricb208782019-07-02 20:53:40 +0000223 ServerContext* context,
224 const openolt::Interface* request,
225 openolt::IntfIndication* response) override {
226
227 // TODO - Return the oper status of the pon interface
228 return Status::OK;
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000229 }*/
Shad Ansaricb208782019-07-02 20:53:40 +0000230
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400231 Status DisablePonIf(
232 ServerContext* context,
233 const openolt::Interface* request,
234 openolt::Empty* response) override {
235
236 return DisablePonIf_(request->intf_id());
237 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400238
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400239 Status CollectStatistics(
240 ServerContext* context,
241 const openolt::Empty* request,
242 openolt::Empty* response) override {
243
244 stats_collection();
245
246 return Status::OK;
247 }
248
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400249 Status Reboot(
250 ServerContext* context,
251 const openolt::Empty* request,
252 openolt::Empty* response) override {
253
Thiyagarajan Subramani89fffc02019-05-13 21:33:20 +0000254 uint8_t ret = system("shutdown -r now");
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400255
256 return Status::OK;
257
258 }
259
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400260 Status GetDeviceInfo(
261 ServerContext* context,
262 const openolt::Empty* request,
263 openolt::DeviceInfo* response) override {
264
265 GetDeviceInfo_(response);
266
267 return Status::OK;
268
269 }
270
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800271 Status CreateTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700272 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800273 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700274 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800275 CreateTrafficSchedulers_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700276 return Status::OK;
277 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400278
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800279 Status RemoveTrafficSchedulers(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700280 ServerContext* context,
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800281 const tech_profile::TrafficSchedulers* request,
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700282 openolt::Empty* response) override {
Girish Gowdruc8ed2ef2019-02-13 08:18:44 -0800283 RemoveTrafficSchedulers_(request);
284 return Status::OK;
285 };
286
287 Status CreateTrafficQueues(
288 ServerContext* context,
289 const tech_profile::TrafficQueues* request,
290 openolt::Empty* response) override {
291 CreateTrafficQueues_(request);
292 return Status::OK;
293 };
294
295 Status RemoveTrafficQueues(
296 ServerContext* context,
297 const tech_profile::TrafficQueues* request,
298 openolt::Empty* response) override {
299 RemoveTrafficQueues_(request);
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700300 return Status::OK;
301 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400302
Shad Ansari01b0e652018-04-05 21:02:53 +0000303};
304
305void RunServer() {
306 OpenoltService service;
307 std::string server_address(serverPort);
308 ServerBuilder builder;
309
310 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
311 builder.RegisterService(&service);
312
313 std::unique_ptr<Server> server(builder.BuildAndStart());
314
nick7be062f2018-05-25 17:52:56 -0400315 time_t now;
316 time(&now);
317 signature = (int)now;
318
319 std::cout << "Server listening on " << server_address
320 << ", connection signature : " << signature << std::endl;
321
Shad Ansari01b0e652018-04-05 21:02:53 +0000322
323 server->Wait();
324}