blob: b556e06c385f92886ac58ba00de82f94721bc849 [file] [log] [blame]
Shad Ansari01b0e652018-04-05 21:02:53 +00001/*
nick7be062f2018-05-25 17:52:56 -04002 Copyright (C) 2018 Open Networking Foundation
Shad Ansari01b0e652018-04-05 21:02:53 +00003
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <iostream>
19#include <memory>
20#include <string>
nick7be062f2018-05-25 17:52:56 -040021#include <time.h>
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040022#include <pthread.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000023
24#include "Queue.h"
25#include <iostream>
26#include <sstream>
27
28#include "server.h"
Shad Ansarib7b0ced2018-05-11 21:53:32 +000029#include "core.h"
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -040030#include "state.h"
Shad Ansari01b0e652018-04-05 21:02:53 +000031
Shad Ansarib7b0ced2018-05-11 21:53:32 +000032#include <grpc++/grpc++.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000033#include <openolt.grpc.pb.h>
34
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(),
112 request->pkt());
113 }
114
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400115 Status UplinkPacketOut(
116 ServerContext* context,
117 const openolt::UplinkPacket* request,
118 openolt::Empty* response) override {
119 return UplinkPacketOut_(
120 request->intf_id(),
121 request->pkt());
122 }
123
Shad Ansari01b0e652018-04-05 21:02:53 +0000124 Status FlowAdd(
125 ServerContext* context,
126 const openolt::Flow* request,
127 openolt::Empty* response) override {
128 return FlowAdd_(
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700129 request->access_intf_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000130 request->onu_id(),
131 request->flow_id(),
132 request->flow_type(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700133 request->alloc_id(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000134 request->network_intf_id(),
135 request->gemport_id(),
136 request->classifier(),
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700137 request->action(),
138 request->priority());
Shad Ansari01b0e652018-04-05 21:02:53 +0000139 }
140
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400141 Status FlowRemove(
142 ServerContext* context,
143 const openolt::Flow* request,
144 openolt::Empty* response) override {
145 return FlowRemove_(
146 request->flow_id(),
147 request->flow_type());
148 }
149
Shad Ansari01b0e652018-04-05 21:02:53 +0000150 Status EnableIndication(
151 ServerContext* context,
152 const ::openolt::Empty* request,
153 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000154
nick7be062f2018-05-25 17:52:56 -0400155 std::cout << "Connection to Voltha established. Indications enabled"
156 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400157
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400158 if (state.previsouly_connected()) {
159 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400160 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400161 if (state.is_activated()){
162 // Adding extra olt indication of current state
163 openolt::Indication ind;
164 openolt::OltIndication* oltInd = new openolt::OltIndication();
165 if (state.is_activated()) {
166 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400167 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400168 } else {
169 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400170 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400171 }
172 ind.set_allocated_olt_ind(oltInd);
173 oltIndQ.push(ind);
174 }
175 }
176
Shad Ansariedef2132018-08-10 22:14:50 +0000177 state.connect();
178
179 while (state.is_connected()) {
180 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD);
181 if (ind.second == false) {
182 /* timeout - do lower priority periodic stuff like stats */
183 stats_collection();
184 continue;
185 }
186 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400187 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400188 if (!isConnected) {
189 //Lost connectivity to this Voltha instance
190 //Put the indication back in the queue for next connecting instance
191 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000192 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400193 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000194 //oltInd.release_olt_ind()
195 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400196
Shad Ansari01b0e652018-04-05 21:02:53 +0000197 return Status::OK;
198 }
nick7be062f2018-05-25 17:52:56 -0400199
200 Status HeartbeatCheck(
201 ServerContext* context,
202 const openolt::Empty* request,
203 openolt::Heartbeat* response) override {
204 response->set_heartbeat_signature(signature);
205
206 return Status::OK;
207 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400208
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400209 Status EnablePonIf(
210 ServerContext* context,
211 const openolt::Interface* request,
212 openolt::Empty* response) override {
213
214 return EnablePonIf_(request->intf_id());
215 }
216
217 Status DisablePonIf(
218 ServerContext* context,
219 const openolt::Interface* request,
220 openolt::Empty* response) override {
221
222 return DisablePonIf_(request->intf_id());
223 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400224
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400225 Status CollectStatistics(
226 ServerContext* context,
227 const openolt::Empty* request,
228 openolt::Empty* response) override {
229
230 stats_collection();
231
232 return Status::OK;
233 }
234
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400235 Status Reboot(
236 ServerContext* context,
237 const openolt::Empty* request,
238 openolt::Empty* response) override {
239
240 system("shutdown -r now");
241
242 return Status::OK;
243
244 }
245
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400246 Status GetDeviceInfo(
247 ServerContext* context,
248 const openolt::Empty* request,
249 openolt::DeviceInfo* response) override {
250
251 GetDeviceInfo_(response);
252
253 return Status::OK;
254
255 }
256
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700257 Status CreateTconts(
258 ServerContext* context,
259 const openolt::Tconts* request,
260 openolt::Empty* response) override {
261 std::cout << "create tconts";
262 CreateTconts_(request);
263 return Status::OK;
264 };
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400265
Girish Gowdru7c4ec2d2018-10-25 00:29:54 -0700266 Status RemoveTconts(
267 ServerContext* context,
268 const openolt::Tconts* request,
269 openolt::Empty* response) override {
270 std::cout << "remove tconts";
271 RemoveTconts_(request);
272 return Status::OK;
273 };
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400274
Shad Ansari01b0e652018-04-05 21:02:53 +0000275};
276
277void RunServer() {
278 OpenoltService service;
279 std::string server_address(serverPort);
280 ServerBuilder builder;
281
282 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
283 builder.RegisterService(&service);
284
285 std::unique_ptr<Server> server(builder.BuildAndStart());
286
nick7be062f2018-05-25 17:52:56 -0400287 time_t now;
288 time(&now);
289 signature = (int)now;
290
291 std::cout << "Server listening on " << server_address
292 << ", connection signature : " << signature << std::endl;
293
Shad Ansari01b0e652018-04-05 21:02:53 +0000294
295 server->Wait();
296}