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