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(), |
| 56 | ((request->serial_number()).vendor_specific()).c_str()); |
| 57 | } |
| 58 | |
| 59 | Status OmciMsgOut( |
| 60 | ServerContext* context, |
| 61 | const openolt::OmciMsg* request, |
| 62 | openolt::Empty* response) override { |
| 63 | return OmciMsgOut_( |
| 64 | request->intf_id(), |
| 65 | request->onu_id(), |
| 66 | request->pkt()); |
| 67 | } |
| 68 | |
Shad Ansari | f2e27a4 | 2018-04-26 22:37:38 +0000 | [diff] [blame] | 69 | Status OnuPacketOut( |
| 70 | ServerContext* context, |
| 71 | const openolt::OnuPacket* request, |
| 72 | openolt::Empty* response) override { |
| 73 | return OnuPacketOut_( |
| 74 | request->intf_id(), |
| 75 | request->onu_id(), |
| 76 | request->pkt()); |
| 77 | } |
| 78 | |
Nicolas Palpacuer | b78def4 | 2018-06-07 12:55:26 -0400 | [diff] [blame] | 79 | Status UplinkPacketOut( |
| 80 | ServerContext* context, |
| 81 | const openolt::UplinkPacket* request, |
| 82 | openolt::Empty* response) override { |
| 83 | return UplinkPacketOut_( |
| 84 | request->intf_id(), |
| 85 | request->pkt()); |
| 86 | } |
| 87 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 88 | Status FlowAdd( |
| 89 | ServerContext* context, |
| 90 | const openolt::Flow* request, |
| 91 | openolt::Empty* response) override { |
| 92 | return FlowAdd_( |
| 93 | request->onu_id(), |
| 94 | request->flow_id(), |
| 95 | request->flow_type(), |
| 96 | request->access_intf_id(), |
| 97 | request->network_intf_id(), |
| 98 | request->gemport_id(), |
| 99 | request->classifier(), |
| 100 | request->action()); |
| 101 | } |
| 102 | |
| 103 | Status EnableIndication( |
| 104 | ServerContext* context, |
| 105 | const ::openolt::Empty* request, |
| 106 | ServerWriter<openolt::Indication>* writer) override { |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 107 | std::cout << "Connection to Voltha established. Indications enabled" |
| 108 | << std::endl; |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 109 | state::connect(); |
| 110 | |
| 111 | while (state::is_connected) { |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 112 | auto oltInd = oltIndQ.pop(); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 113 | bool isConnected = writer->Write(oltInd); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 114 | if (!isConnected) { |
| 115 | //Lost connectivity to this Voltha instance |
| 116 | //Put the indication back in the queue for next connecting instance |
| 117 | oltIndQ.push(oltInd); |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 118 | state::disconnect(); |
Nicolas Palpacuer | 58d252c | 2018-06-06 11:19:04 -0400 | [diff] [blame] | 119 | } |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 120 | //oltInd.release_olt_ind() |
| 121 | } |
Nicolas Palpacuer | 3cad49d | 2018-07-02 14:03:24 -0400 | [diff] [blame] | 122 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 123 | return Status::OK; |
| 124 | } |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 125 | |
| 126 | Status HeartbeatCheck( |
| 127 | ServerContext* context, |
| 128 | const openolt::Empty* request, |
| 129 | openolt::Heartbeat* response) override { |
| 130 | response->set_heartbeat_signature(signature); |
| 131 | |
| 132 | return Status::OK; |
| 133 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 134 | |
Nicolas Palpacuer | 05ea0ea | 2018-07-06 11:47:21 -0400 | [diff] [blame] | 135 | Status EnablePonIf( |
| 136 | ServerContext* context, |
| 137 | const openolt::Interface* request, |
| 138 | openolt::Empty* response) override { |
| 139 | |
| 140 | return EnablePonIf_(request->intf_id()); |
| 141 | } |
| 142 | |
| 143 | Status DisablePonIf( |
| 144 | ServerContext* context, |
| 145 | const openolt::Interface* request, |
| 146 | openolt::Empty* response) override { |
| 147 | |
| 148 | return DisablePonIf_(request->intf_id()); |
| 149 | } |
Nicolas Palpacuer | 0f19b1a | 2018-06-07 17:29:31 -0400 | [diff] [blame] | 150 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | void RunServer() { |
| 154 | OpenoltService service; |
| 155 | std::string server_address(serverPort); |
| 156 | ServerBuilder builder; |
| 157 | |
| 158 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 159 | builder.RegisterService(&service); |
| 160 | |
| 161 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 162 | |
nick | 7be062f | 2018-05-25 17:52:56 -0400 | [diff] [blame] | 163 | time_t now; |
| 164 | time(&now); |
| 165 | signature = (int)now; |
| 166 | |
| 167 | std::cout << "Server listening on " << server_address |
| 168 | << ", connection signature : " << signature << std::endl; |
| 169 | |
Shad Ansari | 01b0e65 | 2018-04-05 21:02:53 +0000 | [diff] [blame] | 170 | |
| 171 | server->Wait(); |
| 172 | } |