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(), |
Shad Ansari | 0610195 | 2018-07-25 00:22:09 +0000 | [diff] [blame] | 70 | ((request->serial_number()).vendor_specific()).c_str(), |
Girish Gowdru | 0ffa48b | 2018-09-26 01:55:29 -0700 | [diff] [blame] | 71 | request->pir(), request->alloc_id()); |
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 | 1cdf6ce | 2018-08-27 02:43:02 -0700 | [diff] [blame] | 93 | ((request->serial_number()).vendor_specific()).c_str(), |
Girish Gowdru | 0ffa48b | 2018-09-26 01:55:29 -0700 | [diff] [blame] | 94 | request->alloc_id()); |
Jonathan Davis | 70c2181 | 2018-07-19 15:32:10 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 97 | Status OmciMsgOut( |
| 98 | ServerContext* context, |
| 99 | const openolt::OmciMsg* request, |
| 100 | openolt::Empty* response) override { |
| 101 | return OmciMsgOut_( |
| 102 | request->intf_id(), |
| 103 | request->onu_id(), |
| 104 | request->pkt()); |
| 105 | } |
| 106 | |
Shad Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 107 | Status OnuPacketOut( |
| 108 | ServerContext* context, |
| 109 | const openolt::OnuPacket* request, |
| 110 | openolt::Empty* response) override { |
| 111 | return OnuPacketOut_( |
| 112 | request->intf_id(), |
| 113 | request->onu_id(), |
| 114 | request->pkt()); |
| 115 | } |
| 116 | |
Nicolas Palpacuer | b78def4 | 2018-06-07 12:55:26 -0400 | [diff] [blame] | 117 | Status UplinkPacketOut( |
| 118 | ServerContext* context, |
| 119 | const openolt::UplinkPacket* request, |
| 120 | openolt::Empty* response) override { |
| 121 | return UplinkPacketOut_( |
| 122 | request->intf_id(), |
| 123 | request->pkt()); |
| 124 | } |
| 125 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 126 | Status FlowAdd( |
| 127 | ServerContext* context, |
| 128 | const openolt::Flow* request, |
| 129 | openolt::Empty* response) override { |
| 130 | return FlowAdd_( |
| 131 | request->onu_id(), |
| 132 | request->flow_id(), |
| 133 | request->flow_type(), |
| 134 | request->access_intf_id(), |
| 135 | request->network_intf_id(), |
| 136 | request->gemport_id(), |
Girish Gowdru | 1cdf6ce | 2018-08-27 02:43:02 -0700 | [diff] [blame] | 137 | request->alloc_id(), |
Nicolas Palpacuer | d6cf5aa | 2018-07-16 15:14:39 -0400 | [diff] [blame] | 138 | request->priority(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 139 | request->classifier(), |
| 140 | request->action()); |
| 141 | } |
| 142 | |
Nicolas Palpacuer | edfaa0c | 2018-07-05 15:05:27 -0400 | [diff] [blame] | 143 | Status FlowRemove( |
| 144 | ServerContext* context, |
| 145 | const openolt::Flow* request, |
| 146 | openolt::Empty* response) override { |
| 147 | return FlowRemove_( |
| 148 | request->flow_id(), |
| 149 | request->flow_type()); |
| 150 | } |
| 151 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 152 | Status EnableIndication( |
| 153 | ServerContext* context, |
| 154 | const ::openolt::Empty* request, |
| 155 | ServerWriter<openolt::Indication>* writer) override { |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 156 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 157 | std::cout << "Connection to Voltha established. Indications enabled" |
| 158 | << std::endl; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 159 | |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 160 | if (state.previsouly_connected()) { |
| 161 | // Reconciliation / recovery case |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 162 | std::cout << "Reconciliation / Recovery case" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 163 | if (state.is_activated()){ |
| 164 | // Adding extra olt indication of current state |
| 165 | openolt::Indication ind; |
| 166 | openolt::OltIndication* oltInd = new openolt::OltIndication(); |
| 167 | if (state.is_activated()) { |
| 168 | oltInd->set_oper_state("up"); |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 169 | std::cout << "Extra OLT indication up" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 170 | } else { |
| 171 | oltInd->set_oper_state("down"); |
Nicolas Palpacuer | 135ce81 | 2018-08-30 09:04:34 -0400 | [diff] [blame] | 172 | std::cout << "Extra OLT indication down" << std::endl; |
Nicolas Palpacuer | fbc0d7d | 2018-08-23 14:46:42 -0400 | [diff] [blame] | 173 | } |
| 174 | ind.set_allocated_olt_ind(oltInd); |
| 175 | oltIndQ.push(ind); |
| 176 | } |
| 177 | } |
| 178 | |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 179 | state.connect(); |
| 180 | |
| 181 | while (state.is_connected()) { |
| 182 | std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD); |
| 183 | if (ind.second == false) { |
| 184 | /* timeout - do lower priority periodic stuff like stats */ |
| 185 | stats_collection(); |
| 186 | continue; |
| 187 | } |
| 188 | openolt::Indication oltInd = ind.first; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 189 | bool isConnected = writer->Write(oltInd); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 190 | if (!isConnected) { |
| 191 | //Lost connectivity to this Voltha instance |
| 192 | //Put the indication back in the queue for next connecting instance |
| 193 | oltIndQ.push(oltInd); |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 194 | state.disconnect(); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 195 | } |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 196 | //oltInd.release_olt_ind() |
| 197 | } |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 198 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 199 | return Status::OK; |
| 200 | } |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 201 | |
| 202 | Status HeartbeatCheck( |
| 203 | ServerContext* context, |
| 204 | const openolt::Empty* request, |
| 205 | openolt::Heartbeat* response) override { |
| 206 | response->set_heartbeat_signature(signature); |
| 207 | |
| 208 | return Status::OK; |
| 209 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 210 | |
Nicolas Palpacuer | 05ea0ea | 2018-07-06 11:47:21 -0400 | [diff] [blame] | 211 | Status EnablePonIf( |
| 212 | ServerContext* context, |
| 213 | const openolt::Interface* request, |
| 214 | openolt::Empty* response) override { |
| 215 | |
| 216 | return EnablePonIf_(request->intf_id()); |
| 217 | } |
| 218 | |
| 219 | Status DisablePonIf( |
| 220 | ServerContext* context, |
| 221 | const openolt::Interface* request, |
| 222 | openolt::Empty* response) override { |
| 223 | |
| 224 | return DisablePonIf_(request->intf_id()); |
| 225 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 226 | |
Nicolas Palpacuer | 65d0447 | 2018-09-06 15:53:37 -0400 | [diff] [blame] | 227 | Status CollectStatistics( |
| 228 | ServerContext* context, |
| 229 | const openolt::Empty* request, |
| 230 | openolt::Empty* response) override { |
| 231 | |
| 232 | stats_collection(); |
| 233 | |
| 234 | return Status::OK; |
| 235 | } |
| 236 | |
Nicolas Palpacuer | 4518066 | 2018-08-02 14:01:51 -0400 | [diff] [blame] | 237 | Status Reboot( |
| 238 | ServerContext* context, |
| 239 | const openolt::Empty* request, |
| 240 | openolt::Empty* response) override { |
| 241 | |
| 242 | system("shutdown -r now"); |
| 243 | |
| 244 | return Status::OK; |
| 245 | |
| 246 | } |
| 247 | |
Nicolas Palpacuer | dff9679 | 2018-09-06 14:59:32 -0400 | [diff] [blame] | 248 | Status GetDeviceInfo( |
| 249 | ServerContext* context, |
| 250 | const openolt::Empty* request, |
| 251 | openolt::DeviceInfo* response) override { |
| 252 | |
| 253 | GetDeviceInfo_(response); |
| 254 | |
| 255 | return Status::OK; |
| 256 | |
| 257 | } |
| 258 | |
| 259 | |
Nicolas Palpacuer | 4518066 | 2018-08-02 14:01:51 -0400 | [diff] [blame] | 260 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | void RunServer() { |
| 264 | OpenoltService service; |
| 265 | std::string server_address(serverPort); |
| 266 | ServerBuilder builder; |
| 267 | |
| 268 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 269 | builder.RegisterService(&service); |
| 270 | |
| 271 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 272 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 273 | time_t now; |
| 274 | time(&now); |
| 275 | signature = (int)now; |
| 276 | |
| 277 | std::cout << "Server listening on " << server_address |
| 278 | << ", connection signature : " << signature << std::endl; |
| 279 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 280 | |
| 281 | server->Wait(); |
| 282 | } |