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" |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 30 | #include "indications.h" |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 31 | #include "stats_collection.h" |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 32 | #include "state.h" |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 33 | |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 34 | #include <grpc++/grpc++.h> |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 35 | #include <openolt.grpc.pb.h> |
| 36 | |
| 37 | using grpc::Server; |
| 38 | using grpc::ServerBuilder; |
| 39 | using grpc::ServerContext; |
| 40 | using grpc::ServerWriter; |
Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 41 | using grpc::Status; |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 42 | |
| 43 | const char *serverPort = "0.0.0.0:9191"; |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 44 | int signature; |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 45 | |
| 46 | class 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(), |
Shad Ansari | 0610195 | 2018-07-25 00:22:09 +0000 | [diff] [blame] | 56 | ((request->serial_number()).vendor_specific()).c_str(), |
| 57 | request->pir()); |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jonathan Davis | 70c2181 | 2018-07-19 15:32:10 -0400 | [diff] [blame] | 60 | Status DeactivateOnu( |
| 61 | ServerContext* context, |
| 62 | const openolt::Onu* request, |
| 63 | openolt::Empty* response) override { |
| 64 | return DeactivateOnu_( |
| 65 | request->intf_id(), |
| 66 | request->onu_id(), |
| 67 | ((request->serial_number()).vendor_id()).c_str(), |
| 68 | ((request->serial_number()).vendor_specific()).c_str()); |
| 69 | } |
| 70 | |
| 71 | Status DeleteOnu( |
| 72 | ServerContext* context, |
| 73 | const openolt::Onu* request, |
| 74 | openolt::Empty* response) override { |
| 75 | return DeleteOnu_( |
| 76 | request->intf_id(), |
| 77 | request->onu_id(), |
| 78 | ((request->serial_number()).vendor_id()).c_str(), |
| 79 | ((request->serial_number()).vendor_specific()).c_str()); |
| 80 | } |
| 81 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 82 | Status OmciMsgOut( |
| 83 | ServerContext* context, |
| 84 | const openolt::OmciMsg* request, |
| 85 | openolt::Empty* response) override { |
| 86 | return OmciMsgOut_( |
| 87 | request->intf_id(), |
| 88 | request->onu_id(), |
| 89 | request->pkt()); |
| 90 | } |
| 91 | |
Shad Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 92 | Status OnuPacketOut( |
| 93 | ServerContext* context, |
| 94 | const openolt::OnuPacket* request, |
| 95 | openolt::Empty* response) override { |
| 96 | return OnuPacketOut_( |
| 97 | request->intf_id(), |
| 98 | request->onu_id(), |
| 99 | request->pkt()); |
| 100 | } |
| 101 | |
Nicolas Palpacuer | b78def4 | 2018-06-07 12:55:26 -0400 | [diff] [blame] | 102 | Status UplinkPacketOut( |
| 103 | ServerContext* context, |
| 104 | const openolt::UplinkPacket* request, |
| 105 | openolt::Empty* response) override { |
| 106 | return UplinkPacketOut_( |
| 107 | request->intf_id(), |
| 108 | request->pkt()); |
| 109 | } |
| 110 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 111 | Status FlowAdd( |
| 112 | ServerContext* context, |
| 113 | const openolt::Flow* request, |
| 114 | openolt::Empty* response) override { |
| 115 | return FlowAdd_( |
| 116 | request->onu_id(), |
| 117 | request->flow_id(), |
| 118 | request->flow_type(), |
| 119 | request->access_intf_id(), |
| 120 | request->network_intf_id(), |
| 121 | request->gemport_id(), |
Nicolas Palpacuer | d6cf5aa | 2018-07-16 15:14:39 -0400 | [diff] [blame] | 122 | request->priority(), |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 123 | request->classifier(), |
| 124 | request->action()); |
| 125 | } |
| 126 | |
| 127 | Status EnableIndication( |
| 128 | ServerContext* context, |
| 129 | const ::openolt::Empty* request, |
| 130 | ServerWriter<openolt::Indication>* writer) override { |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 131 | std::cout << "Connection to Voltha established. Indications enabled" |
| 132 | << std::endl; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 133 | state::connect(); |
| 134 | |
| 135 | while (state::is_connected) { |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 136 | auto oltInd = oltIndQ.pop(); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 137 | bool isConnected = writer->Write(oltInd); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 138 | if (!isConnected) { |
| 139 | //Lost connectivity to this Voltha instance |
| 140 | //Put the indication back in the queue for next connecting instance |
| 141 | oltIndQ.push(oltInd); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 142 | state::disconnect(); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 143 | } |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 144 | //oltInd.release_olt_ind() |
| 145 | } |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 146 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 147 | return Status::OK; |
| 148 | } |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 149 | |
| 150 | Status HeartbeatCheck( |
| 151 | ServerContext* context, |
| 152 | const openolt::Empty* request, |
| 153 | openolt::Heartbeat* response) override { |
| 154 | response->set_heartbeat_signature(signature); |
| 155 | |
| 156 | return Status::OK; |
| 157 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 158 | |
Nicolas Palpacuer | 05ea0ea | 2018-07-06 11:47:21 -0400 | [diff] [blame] | 159 | Status EnablePonIf( |
| 160 | ServerContext* context, |
| 161 | const openolt::Interface* request, |
| 162 | openolt::Empty* response) override { |
| 163 | |
| 164 | return EnablePonIf_(request->intf_id()); |
| 165 | } |
| 166 | |
| 167 | Status DisablePonIf( |
| 168 | ServerContext* context, |
| 169 | const openolt::Interface* request, |
| 170 | openolt::Empty* response) override { |
| 171 | |
| 172 | return DisablePonIf_(request->intf_id()); |
| 173 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 174 | |
Nicolas Palpacuer | 4518066 | 2018-08-02 14:01:51 -0400 | [diff] [blame^] | 175 | Status Reboot( |
| 176 | ServerContext* context, |
| 177 | const openolt::Empty* request, |
| 178 | openolt::Empty* response) override { |
| 179 | |
| 180 | system("shutdown -r now"); |
| 181 | |
| 182 | return Status::OK; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | void RunServer() { |
| 190 | OpenoltService service; |
| 191 | std::string server_address(serverPort); |
| 192 | ServerBuilder builder; |
| 193 | |
| 194 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 195 | builder.RegisterService(&service); |
| 196 | |
| 197 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 198 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 199 | time_t now; |
| 200 | time(&now); |
| 201 | signature = (int)now; |
| 202 | |
| 203 | std::cout << "Server listening on " << server_address |
| 204 | << ", connection signature : " << signature << std::endl; |
| 205 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 206 | |
| 207 | server->Wait(); |
| 208 | } |