blob: 8cd83ff10a094e8bbae56b7c4785f8451ba01d6e [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"
Shad Ansari01b0e652018-04-05 21:02:53 +000030#include "indications.h"
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040031#include "stats_collection.h"
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -040032#include "state.h"
Shad Ansari01b0e652018-04-05 21:02:53 +000033
Shad Ansarib7b0ced2018-05-11 21:53:32 +000034#include <grpc++/grpc++.h>
Shad Ansari01b0e652018-04-05 21:02:53 +000035#include <openolt.grpc.pb.h>
36
37using grpc::Server;
38using grpc::ServerBuilder;
39using grpc::ServerContext;
40using grpc::ServerWriter;
Shad Ansarib7b0ced2018-05-11 21:53:32 +000041using grpc::Status;
Shad Ansari01b0e652018-04-05 21:02:53 +000042
43const char *serverPort = "0.0.0.0:9191";
nick7be062f2018-05-25 17:52:56 -040044int signature;
Shad Ansari01b0e652018-04-05 21:02:53 +000045
46class 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(),
71 request->pir());
Shad Ansari01b0e652018-04-05 21:02:53 +000072 }
73
Jonathan Davis70c21812018-07-19 15:32:10 -040074 Status DeactivateOnu(
75 ServerContext* context,
76 const openolt::Onu* request,
77 openolt::Empty* response) override {
78 return DeactivateOnu_(
79 request->intf_id(),
80 request->onu_id(),
81 ((request->serial_number()).vendor_id()).c_str(),
82 ((request->serial_number()).vendor_specific()).c_str());
83 }
84
85 Status DeleteOnu(
86 ServerContext* context,
87 const openolt::Onu* request,
88 openolt::Empty* response) override {
89 return DeleteOnu_(
90 request->intf_id(),
91 request->onu_id(),
92 ((request->serial_number()).vendor_id()).c_str(),
93 ((request->serial_number()).vendor_specific()).c_str());
94 }
95
Shad Ansari01b0e652018-04-05 21:02:53 +000096 Status OmciMsgOut(
97 ServerContext* context,
98 const openolt::OmciMsg* request,
99 openolt::Empty* response) override {
100 return OmciMsgOut_(
101 request->intf_id(),
102 request->onu_id(),
103 request->pkt());
104 }
105
Shad Ansarif2e27a42018-04-26 22:37:38 +0000106 Status OnuPacketOut(
107 ServerContext* context,
108 const openolt::OnuPacket* request,
109 openolt::Empty* response) override {
110 return OnuPacketOut_(
111 request->intf_id(),
112 request->onu_id(),
113 request->pkt());
114 }
115
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400116 Status UplinkPacketOut(
117 ServerContext* context,
118 const openolt::UplinkPacket* request,
119 openolt::Empty* response) override {
120 return UplinkPacketOut_(
121 request->intf_id(),
122 request->pkt());
123 }
124
Shad Ansari01b0e652018-04-05 21:02:53 +0000125 Status FlowAdd(
126 ServerContext* context,
127 const openolt::Flow* request,
128 openolt::Empty* response) override {
129 return FlowAdd_(
130 request->onu_id(),
131 request->flow_id(),
132 request->flow_type(),
133 request->access_intf_id(),
134 request->network_intf_id(),
135 request->gemport_id(),
Nicolas Palpacuerd6cf5aa2018-07-16 15:14:39 -0400136 request->priority(),
Shad Ansari01b0e652018-04-05 21:02:53 +0000137 request->classifier(),
138 request->action());
139 }
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
Shad Ansariedef2132018-08-10 22:14:50 +0000158 state.connect();
159
160 while (state.is_connected()) {
161 std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD);
162 if (ind.second == false) {
163 /* timeout - do lower priority periodic stuff like stats */
164 stats_collection();
165 continue;
166 }
167 openolt::Indication oltInd = ind.first;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400168 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400169 if (!isConnected) {
170 //Lost connectivity to this Voltha instance
171 //Put the indication back in the queue for next connecting instance
172 oltIndQ.push(oltInd);
Shad Ansariedef2132018-08-10 22:14:50 +0000173 state.disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400174 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000175 //oltInd.release_olt_ind()
176 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400177
Shad Ansari01b0e652018-04-05 21:02:53 +0000178 return Status::OK;
179 }
nick7be062f2018-05-25 17:52:56 -0400180
181 Status HeartbeatCheck(
182 ServerContext* context,
183 const openolt::Empty* request,
184 openolt::Heartbeat* response) override {
185 response->set_heartbeat_signature(signature);
186
187 return Status::OK;
188 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400189
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400190 Status EnablePonIf(
191 ServerContext* context,
192 const openolt::Interface* request,
193 openolt::Empty* response) override {
194
195 return EnablePonIf_(request->intf_id());
196 }
197
198 Status DisablePonIf(
199 ServerContext* context,
200 const openolt::Interface* request,
201 openolt::Empty* response) override {
202
203 return DisablePonIf_(request->intf_id());
204 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400205
Nicolas Palpacuer45180662018-08-02 14:01:51 -0400206 Status Reboot(
207 ServerContext* context,
208 const openolt::Empty* request,
209 openolt::Empty* response) override {
210
211 system("shutdown -r now");
212
213 return Status::OK;
214
215 }
216
217
Shad Ansari01b0e652018-04-05 21:02:53 +0000218};
219
220void RunServer() {
221 OpenoltService service;
222 std::string server_address(serverPort);
223 ServerBuilder builder;
224
225 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
226 builder.RegisterService(&service);
227
228 std::unique_ptr<Server> server(builder.BuildAndStart());
229
nick7be062f2018-05-25 17:52:56 -0400230 time_t now;
231 time(&now);
232 signature = (int)now;
233
234 std::cout << "Server listening on " << server_address
235 << ", connection signature : " << signature << std::endl;
236
Shad Ansari01b0e652018-04-05 21:02:53 +0000237
238 server->Wait();
239}