blob: 606e9cb0912ffd621e3d18179279d579a4ca3a1e [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
48 Status ActivateOnu(
49 ServerContext* context,
50 const openolt::Onu* request,
51 openolt::Empty* response) override {
52 return ActivateOnu_(
53 request->intf_id(),
54 request->onu_id(),
55 ((request->serial_number()).vendor_id()).c_str(),
56 ((request->serial_number()).vendor_specific()).c_str());
57 }
58
59 Status OmciMsgOut(
60 ServerContext* context,
61 const openolt::OmciMsg* request,
62 openolt::Empty* response) override {
63 return OmciMsgOut_(
64 request->intf_id(),
65 request->onu_id(),
66 request->pkt());
67 }
68
Shad Ansarif2e27a42018-04-26 22:37:38 +000069 Status OnuPacketOut(
70 ServerContext* context,
71 const openolt::OnuPacket* request,
72 openolt::Empty* response) override {
73 return OnuPacketOut_(
74 request->intf_id(),
75 request->onu_id(),
76 request->pkt());
77 }
78
Nicolas Palpacuerb78def42018-06-07 12:55:26 -040079 Status UplinkPacketOut(
80 ServerContext* context,
81 const openolt::UplinkPacket* request,
82 openolt::Empty* response) override {
83 return UplinkPacketOut_(
84 request->intf_id(),
85 request->pkt());
86 }
87
Shad Ansari01b0e652018-04-05 21:02:53 +000088 Status FlowAdd(
89 ServerContext* context,
90 const openolt::Flow* request,
91 openolt::Empty* response) override {
92 return FlowAdd_(
93 request->onu_id(),
94 request->flow_id(),
95 request->flow_type(),
96 request->access_intf_id(),
97 request->network_intf_id(),
98 request->gemport_id(),
99 request->classifier(),
100 request->action());
101 }
102
103 Status EnableIndication(
104 ServerContext* context,
105 const ::openolt::Empty* request,
106 ServerWriter<openolt::Indication>* writer) override {
nick7be062f2018-05-25 17:52:56 -0400107 std::cout << "Connection to Voltha established. Indications enabled"
108 << std::endl;
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400109 state::connect();
110
111 while (state::is_connected) {
Shad Ansari01b0e652018-04-05 21:02:53 +0000112 auto oltInd = oltIndQ.pop();
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400113 bool isConnected = writer->Write(oltInd);
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400114 if (!isConnected) {
115 //Lost connectivity to this Voltha instance
116 //Put the indication back in the queue for next connecting instance
117 oltIndQ.push(oltInd);
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400118 state::disconnect();
Nicolas Palpacuer58d252c2018-06-06 11:19:04 -0400119 }
Shad Ansari01b0e652018-04-05 21:02:53 +0000120 //oltInd.release_olt_ind()
121 }
Nicolas Palpacuer3cad49d2018-07-02 14:03:24 -0400122
Shad Ansari01b0e652018-04-05 21:02:53 +0000123 return Status::OK;
124 }
nick7be062f2018-05-25 17:52:56 -0400125
126 Status HeartbeatCheck(
127 ServerContext* context,
128 const openolt::Empty* request,
129 openolt::Heartbeat* response) override {
130 response->set_heartbeat_signature(signature);
131
132 return Status::OK;
133 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400134
Nicolas Palpacuer05ea0ea2018-07-06 11:47:21 -0400135 Status EnablePonIf(
136 ServerContext* context,
137 const openolt::Interface* request,
138 openolt::Empty* response) override {
139
140 return EnablePonIf_(request->intf_id());
141 }
142
143 Status DisablePonIf(
144 ServerContext* context,
145 const openolt::Interface* request,
146 openolt::Empty* response) override {
147
148 return DisablePonIf_(request->intf_id());
149 }
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400150
Shad Ansari01b0e652018-04-05 21:02:53 +0000151};
152
153void RunServer() {
154 OpenoltService service;
155 std::string server_address(serverPort);
156 ServerBuilder builder;
157
158 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
159 builder.RegisterService(&service);
160
161 std::unique_ptr<Server> server(builder.BuildAndStart());
162
nick7be062f2018-05-25 17:52:56 -0400163 time_t now;
164 time(&now);
165 signature = (int)now;
166
167 std::cout << "Server listening on " << server_address
168 << ", connection signature : " << signature << std::endl;
169
Shad Ansari01b0e652018-04-05 21:02:53 +0000170
171 server->Wait();
172}