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