Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 1 | /* |
Girish Gowdra | a707e7c | 2019-11-07 11:36:13 +0530 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 3 | |
Girish Gowdra | a707e7c | 2019-11-07 11:36:13 +0530 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 7 | |
Girish Gowdra | a707e7c | 2019-11-07 11:36:13 +0530 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 9 | |
Girish Gowdra | a707e7c | 2019-11-07 11:36:13 +0530 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 16 | |
| 17 | #include <iostream> |
| 18 | #include <memory> |
| 19 | #include <string> |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 20 | #include <time.h> |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 21 | #include <pthread.h> |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 22 | |
| 23 | #include "Queue.h" |
| 24 | #include <iostream> |
| 25 | #include <sstream> |
| 26 | |
| 27 | #include "server.h" |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 28 | #include "core.h" |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 29 | #include "state.h" |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 30 | |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 31 | #include <grpc++/grpc++.h> |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 32 | #include <voltha_protos/openolt.grpc.pb.h> |
| 33 | #include <voltha_protos/tech_profile.grpc.pb.h> |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 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(), |
Girish Gowdru | c8ed2ef | 2019-02-13 08:18:44 -0800 | [diff] [blame] | 113 | request->gemport_id(), |
Shad Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 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_( |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 131 | request->access_intf_id(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 132 | request->onu_id(), |
Craig Lutgen | 967a1d0 | 2018-11-27 10:41:51 -0600 | [diff] [blame] | 133 | request->uni_id(), |
| 134 | request->port_no(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 135 | request->flow_id(), |
| 136 | request->flow_type(), |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 137 | request->alloc_id(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 138 | request->network_intf_id(), |
| 139 | request->gemport_id(), |
| 140 | request->classifier(), |
Girish Gowdru | 7c4ec2d | 2018-10-25 00:29:54 -0700 | [diff] [blame] | 141 | request->action(), |
Craig Lutgen | 967a1d0 | 2018-11-27 10:41:51 -0600 | [diff] [blame] | 142 | request->priority(), |
Burak Gurdag | c78b9e1 | 2019-11-29 11:14:51 +0000 | [diff] [blame] | 143 | request->cookie(), |
| 144 | request->group_id()); |
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()) { |
Girish Gowdra | 9646105 | 2019-11-22 20:13:59 +0530 | [diff] [blame] | 186 | std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD*1000, 1000); |
Shad Ansari | edef213 | 2018-08-10 22:14:50 +0000 | [diff] [blame] | 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 | |
Burak Gurdag | c78b9e1 | 2019-11-29 11:14:51 +0000 | [diff] [blame] | 304 | Status PerformGroupOperation( |
| 305 | ServerContext* context, |
| 306 | const openolt::Group* request, |
| 307 | openolt::Empty* response) override { |
| 308 | return PerformGroupOperation_(request); |
| 309 | }; |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | void RunServer() { |
| 313 | OpenoltService service; |
| 314 | std::string server_address(serverPort); |
| 315 | ServerBuilder builder; |
| 316 | |
| 317 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 318 | builder.RegisterService(&service); |
| 319 | |
| 320 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 321 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 322 | time_t now; |
| 323 | time(&now); |
| 324 | signature = (int)now; |
| 325 | |
| 326 | std::cout << "Server listening on " << server_address |
| 327 | << ", connection signature : " << signature << std::endl; |
| 328 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 329 | |
| 330 | server->Wait(); |
| 331 | } |