Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 1 | /* |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 2 | Copyright (C) 2018 Open Networking Foundation |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 3 | |
| 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> |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 21 | #include <time.h> |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 22 | #include <pthread.h> |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 23 | |
| 24 | #include "Queue.h" |
| 25 | #include <iostream> |
| 26 | #include <sstream> |
| 27 | |
| 28 | #include "server.h" |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 29 | #include "core.h" |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 30 | #include "state.h" |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 31 | |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 32 | #include <grpc++/grpc++.h> |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 33 | #include <openolt.grpc.pb.h> |
| 34 | |
| 35 | using grpc::Server; |
| 36 | using grpc::ServerBuilder; |
| 37 | using grpc::ServerContext; |
| 38 | using grpc::ServerWriter; |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 39 | using grpc::Status; |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 40 | |
| 41 | const char *serverPort = "0.0.0.0:9191"; |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 42 | int signature; |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 43 | |
Shad Ansari | 627b578 | 2018-08-13 22:49:32 +0000 | [diff] [blame] | 44 | Queue<openolt::Indication> oltIndQ; |
| 45 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 46 | class OpenoltService final : public openolt::Openolt::Service { |
| 47 | |
Nicolas Palpacuer | e3fc0d2 | 2018-08-02 16:51:05 -0400 | [diff] [blame] | 48 | 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 Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 62 | 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 Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 70 | ((request->serial_number()).vendor_specific()).c_str(), request->pir()); |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Jonathan Davis | 70c2181 | 2018-07-19 15:32:10 -0400 | [diff] [blame] | 73 | 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 Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 92 | ((request->serial_number()).vendor_specific()).c_str()); |
Jonathan Davis | 70c2181 | 2018-07-19 15:32:10 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 95 | 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 Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 105 | 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 Lutgen | 967a1d0 | 2018-11-27 10:41:51 -0600 | [diff] [blame] | 112 | request->port_no(), |
Shad Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 113 | request->pkt()); |
| 114 | } |
| 115 | |
Nicolas Palpacuer | b78def4 | 2018-06-07 12:55:26 -0400 | [diff] [blame] | 116 | 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 Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 125 | Status FlowAdd( |
| 126 | ServerContext* context, |
| 127 | const openolt::Flow* request, |
| 128 | openolt::Empty* response) override { |
| 129 | return FlowAdd_( |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 130 | request->access_intf_id(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 131 | request->onu_id(), |
Craig Lutgen | 967a1d0 | 2018-11-27 10:41:51 -0600 | [diff] [blame] | 132 | request->uni_id(), |
| 133 | request->port_no(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 134 | request->flow_id(), |
| 135 | request->flow_type(), |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 136 | request->alloc_id(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 137 | request->network_intf_id(), |
| 138 | request->gemport_id(), |
| 139 | request->classifier(), |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 140 | request->action(), |
Craig Lutgen | 967a1d0 | 2018-11-27 10:41:51 -0600 | [diff] [blame] | 141 | request->priority(), |
| 142 | request->cookie()); |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Palpacuer | edfaa0c | 2018-07-05 15:05:27 -0400 | [diff] [blame] | 145 | Status FlowRemove( |
| 146 | ServerContext* context, |
| 147 | const openolt::Flow* request, |
| 148 | openolt::Empty* response) override { |
| 149 | return FlowRemove_( |
| 150 | request->flow_id(), |
| 151 | request->flow_type()); |
| 152 | } |
| 153 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 154 | Status EnableIndication( |
| 155 | ServerContext* context, |
| 156 | const ::openolt::Empty* request, |
| 157 | ServerWriter<openolt::Indication>* writer) override { |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 158 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 159 | std::cout << "Connection to Voltha established. Indications enabled" |
| 160 | << std::endl; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 161 | |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 162 | if (state.previsouly_connected()) { |
| 163 | // Reconciliation / recovery case |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 164 | std::cout << "Reconciliation / Recovery case" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 165 | if (state.is_activated()){ |
| 166 | // Adding extra olt indication of current state |
| 167 | openolt::Indication ind; |
| 168 | openolt::OltIndication* oltInd = new openolt::OltIndication(); |
| 169 | if (state.is_activated()) { |
| 170 | oltInd->set_oper_state("up"); |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 171 | std::cout << "Extra OLT indication up" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 172 | } else { |
| 173 | oltInd->set_oper_state("down"); |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 174 | std::cout << "Extra OLT indication down" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 175 | } |
| 176 | ind.set_allocated_olt_ind(oltInd); |
| 177 | oltIndQ.push(ind); |
| 178 | } |
| 179 | } |
| 180 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 181 | state.connect(); |
| 182 | |
| 183 | while (state.is_connected()) { |
| 184 | std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD); |
| 185 | if (ind.second == false) { |
| 186 | /* timeout - do lower priority periodic stuff like stats */ |
| 187 | stats_collection(); |
| 188 | continue; |
| 189 | } |
| 190 | openolt::Indication oltInd = ind.first; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 191 | bool isConnected = writer->Write(oltInd); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 192 | if (!isConnected) { |
| 193 | //Lost connectivity to this Voltha instance |
| 194 | //Put the indication back in the queue for next connecting instance |
| 195 | oltIndQ.push(oltInd); |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 196 | state.disconnect(); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 197 | } |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 198 | //oltInd.release_olt_ind() |
| 199 | } |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 200 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 201 | return Status::OK; |
| 202 | } |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 203 | |
| 204 | Status HeartbeatCheck( |
| 205 | ServerContext* context, |
| 206 | const openolt::Empty* request, |
| 207 | openolt::Heartbeat* response) override { |
| 208 | response->set_heartbeat_signature(signature); |
| 209 | |
| 210 | return Status::OK; |
| 211 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 212 | |
Nicolas Palpacuer | 05ea0ea | 2018-07-06 11:47:21 -0400 | [diff] [blame] | 213 | Status EnablePonIf( |
| 214 | ServerContext* context, |
| 215 | const openolt::Interface* request, |
| 216 | openolt::Empty* response) override { |
| 217 | |
| 218 | return EnablePonIf_(request->intf_id()); |
| 219 | } |
| 220 | |
| 221 | Status DisablePonIf( |
| 222 | ServerContext* context, |
| 223 | const openolt::Interface* request, |
| 224 | openolt::Empty* response) override { |
| 225 | |
| 226 | return DisablePonIf_(request->intf_id()); |
| 227 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 228 | |
Nicolas Palpacuer | 65d0447 | 2018-09-06 15:53:37 -0400 | [diff] [blame] | 229 | Status CollectStatistics( |
| 230 | ServerContext* context, |
| 231 | const openolt::Empty* request, |
| 232 | openolt::Empty* response) override { |
| 233 | |
| 234 | stats_collection(); |
| 235 | |
| 236 | return Status::OK; |
| 237 | } |
| 238 | |
Nicolas Palpacuer | 4518066 | 2018-08-02 14:01:51 -0400 | [diff] [blame] | 239 | Status Reboot( |
| 240 | ServerContext* context, |
| 241 | const openolt::Empty* request, |
| 242 | openolt::Empty* response) override { |
| 243 | |
| 244 | system("shutdown -r now"); |
| 245 | |
| 246 | return Status::OK; |
| 247 | |
| 248 | } |
| 249 | |
Nicolas Palpacuer | dff9679 | 2018-09-06 14:59:32 -0400 | [diff] [blame] | 250 | Status GetDeviceInfo( |
| 251 | ServerContext* context, |
| 252 | const openolt::Empty* request, |
| 253 | openolt::DeviceInfo* response) override { |
| 254 | |
| 255 | GetDeviceInfo_(response); |
| 256 | |
| 257 | return Status::OK; |
| 258 | |
| 259 | } |
| 260 | |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 261 | Status CreateTconts( |
| 262 | ServerContext* context, |
| 263 | const openolt::Tconts* request, |
| 264 | openolt::Empty* response) override { |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 265 | CreateTconts_(request); |
| 266 | return Status::OK; |
| 267 | }; |
Nicolas Palpacuer | dff9679 | 2018-09-06 14:59:32 -0400 | [diff] [blame] | 268 | |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 269 | Status RemoveTconts( |
| 270 | ServerContext* context, |
| 271 | const openolt::Tconts* request, |
| 272 | openolt::Empty* response) override { |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 273 | RemoveTconts_(request); |
| 274 | return Status::OK; |
| 275 | }; |
Nicolas Palpacuer | 4518066 | 2018-08-02 14:01:51 -0400 | [diff] [blame] | 276 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | void RunServer() { |
| 280 | OpenoltService service; |
| 281 | std::string server_address(serverPort); |
| 282 | ServerBuilder builder; |
| 283 | |
| 284 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 285 | builder.RegisterService(&service); |
| 286 | |
| 287 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 288 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 289 | time_t now; |
| 290 | time(&now); |
| 291 | signature = (int)now; |
| 292 | |
| 293 | std::cout << "Server listening on " << server_address |
| 294 | << ", connection signature : " << signature << std::endl; |
| 295 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 296 | |
| 297 | server->Wait(); |
| 298 | } |