blob: 433ecd54c61123e9aff251a64eb8ff320997419c [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(),
Shad Ansari06101952018-07-25 00:22:09 +000070 ((request->serial_number()).vendor_specific()).c_str(),
Girish Gowdru1cdf6ce2018-08-27 02:43:02 -070071 request->pir(), request->agg_port_id(),
72 request->sched_id());
Shad Ansari01b0e652018-04-05 21:02:53 +000073 }
74
Jonathan Davis70c21812018-07-19 15:32:10 -040075 Status DeactivateOnu(
76 ServerContext* context,
77 const openolt::Onu* request,
78 openolt::Empty* response) override {
79 return DeactivateOnu_(
80 request->intf_id(),
81 request->onu_id(),
82 ((request->serial_number()).vendor_id()).c_str(),
83 ((request->serial_number()).vendor_specific()).c_str());
84 }
85
86 Status DeleteOnu(
87 ServerContext* context,
88 const openolt::Onu* request,
89 openolt::Empty* response) override {
90 return DeleteOnu_(
91 request->intf_id(),
92 request->onu_id(),
93 ((request->serial_number()).vendor_id()).c_str(),
Girish Gowdru1cdf6ce2018-08-27 02:43:02 -070094 ((request->serial_number()).vendor_specific()).c_str(),
95 request->agg_port_id(), request->sched_id());
Jonathan Davis70c21812018-07-19 15:32:10 -040096 }
97
Shad Ansari01b0e652018-04-05 21:02:53 +000098 Status OmciMsgOut(
99 ServerContext* context,
100 const openolt::OmciMsg* request,
101 openolt::Empty* response) override {
102 return OmciMsgOut_(
103 request->intf_id(),
104 request->onu_id(),
105 request->pkt());
106 }
107
Shad Ansarif2e27a42018-04-26 22:37:38 +0000108 Status OnuPacketOut(
109 ServerContext* context,
110 const openolt::OnuPacket* request,
111 openolt::Empty* response) override {
112 return OnuPacketOut_(
113 request->intf_id(),
114 request->onu_id(),
115 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 {
131 return FlowAdd_(
132 request->onu_id(),
133 request->flow_id(),
134 request->flow_type(),
135 request->access_intf_id(),
136 request->network_intf_id(),
137 request->gemport_id(),
Girish Gowdru1cdf6ce2018-08-27 02:43:02 -0700138 request->alloc_id(),
Nicolas Palpacuerd6cf5aa2018-07-16 15:14:39 -0400139 request->priority(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000140 request->classifier(),
141 request->action());
142 }
143
Nicolas Palpacueredfaa0c2018-07-05 15:05:27 -0400144 Status FlowRemove(
145 ServerContext* context,
146 const openolt::Flow* request,
147 openolt::Empty* response) override {
148 return FlowRemove_(
149 request->flow_id(),
150 request->flow_type());
151 }
152
Shad Ansari01b0e652018-04-05 21:02:53 +0000153 Status EnableIndication(
154 ServerContext* context,
155 const ::openolt::Empty* request,
156 ServerWriter<openolt::Indication>* writer) override {
Shad Ansariedef2132018-08-10 22:14:50 +0000157
nick7be062f2018-05-25 17:52:56 -0400158 std::cout << "Connection to Voltha established. Indications enabled"
159 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400160
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400161 if (state.previsouly_connected()) {
162 // Reconciliation / recovery case
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400163 std::cout << "Reconciliation / Recovery case" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400164 if (state.is_activated()){
165 // Adding extra olt indication of current state
166 openolt::Indication ind;
167 openolt::OltIndication* oltInd = new openolt::OltIndication();
168 if (state.is_activated()) {
169 oltInd->set_oper_state("up");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400170 std::cout << "Extra OLT indication up" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400171 } else {
172 oltInd->set_oper_state("down");
Nicolas Palpacuer135ce812018-08-30 09:04:34 -0400173 std::cout << "Extra OLT indication down" << std::endl;
Nicolas Palpacuerfbc0d7d2018-08-23 14:46:42 -0400174 }
175 ind.set_allocated_olt_ind(oltInd);
176 oltIndQ.push(ind);
177 }
178 }
179
Shad Ansariedef2132018-08-10 22:14:50 +0000180 state.connect();
181
182 while (state.is_connected()) {
183 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD);
184 if (ind.second == false) {
185 /* timeout - do lower priority periodic stuff like stats */
186 stats_collection();
187 continue;
188 }
189 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400190 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400191 if (!isConnected) {
192 //Lost connectivity to this Voltha instance
193 //Put the indication back in the queue for next connecting instance
194 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000195 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400196 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000197 //oltInd.release_olt_ind()
198 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400199
Shad Ansari01b0e652018-04-05 21:02:53 +0000200 return Status::OK;
201 }
nick7be062f2018-05-25 17:52:56 -0400202
203 Status HeartbeatCheck(
204 ServerContext* context,
205 const openolt::Empty* request,
206 openolt::Heartbeat* response) override {
207 response->set_heartbeat_signature(signature);
208
209 return Status::OK;
210 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400211
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400212 Status EnablePonIf(
213 ServerContext* context,
214 const openolt::Interface* request,
215 openolt::Empty* response) override {
216
217 return EnablePonIf_(request->intf_id());
218 }
219
220 Status DisablePonIf(
221 ServerContext* context,
222 const openolt::Interface* request,
223 openolt::Empty* response) override {
224
225 return DisablePonIf_(request->intf_id());
226 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400227
Nicolas Palpacuer65d04472018-09-06 15:53:37 -0400228 Status CollectStatistics(
229 ServerContext* context,
230 const openolt::Empty* request,
231 openolt::Empty* response) override {
232
233 stats_collection();
234
235 return Status::OK;
236 }
237
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400238 Status Reboot(
239 ServerContext* context,
240 const openolt::Empty* request,
241 openolt::Empty* response) override {
242
243 system("shutdown -r now");
244
245 return Status::OK;
246
247 }
248
Nicolas Palpacuerdff96792018-09-06 14:59:32 -0400249 Status GetDeviceInfo(
250 ServerContext* context,
251 const openolt::Empty* request,
252 openolt::DeviceInfo* response) override {
253
254 GetDeviceInfo_(response);
255
256 return Status::OK;
257
258 }
259
260
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400261
Shad Ansari01b0e652018-04-05 21:02:53 +0000262};
263
264void RunServer() {
265 OpenoltService service;
266 std::string server_address(serverPort);
267 ServerBuilder builder;
268
269 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
270 builder.RegisterService(&service);
271
272 std::unique_ptr<Server> server(builder.BuildAndStart());
273
nick7be062f2018-05-25 17:52:56 -0400274 time_t now;
275 time(&now);
276 signature = (int)now;
277
278 std::cout << "Server listening on " << server_address
279 << ", connection signature : " << signature << std::endl;
280
Shad Ansari01b0e652018-04-05 21:02:53 +0000281
282 server->Wait();
283}