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