VOL-1156 Re-factor openolt driver

Allows re-use of common code among backends for multiple hardware
and simulators.

Change-Id: If5f0147d2eef7c1f9cbcda394d06a2070b4be275
diff --git a/common/Queue.h b/common/Queue.h
new file mode 100644
index 0000000..30fbbda
--- /dev/null
+++ b/common/Queue.h
@@ -0,0 +1,72 @@
+//
+// Copyright (c) 2013 Juan Palacios juan.palacios.puyana@gmail.com
+// Subject to the BSD 2-Clause License
+// - see < http://opensource.org/licenses/BSD-2-Clause>
+//
+
+#ifndef CONCURRENT_QUEUE_
+#define CONCURRENT_QUEUE_
+
+#include <queue>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+#include <chrono>
+
+template <typename T>
+class Queue
+{
+ public:
+
+  std::pair<T, bool> pop(int timeout)
+  {
+    std::cv_status status = std::cv_status::no_timeout;
+    std::unique_lock<std::mutex> mlock(mutex_);
+    static int duration = 0;
+    while (queue_.empty())
+    {
+      status = cond_.wait_for(mlock, std::chrono::seconds(1));
+      if (status == std::cv_status::timeout)
+      {
+        duration++;
+        if (duration > timeout)
+        {
+          duration = 0;
+          return std::pair<T, bool>({}, false);
+        }
+      }
+    }
+    auto val = queue_.front();
+    queue_.pop();
+    return std::pair<T, bool>(val, true);
+  }
+
+  void pop(T& item)
+  {
+    std::unique_lock<std::mutex> mlock(mutex_);
+    while (queue_.empty())
+    {
+      cond_.wait(mlock);
+    }
+    item = queue_.front();
+    queue_.pop();
+  }
+
+  void push(const T& item)
+  {
+    std::unique_lock<std::mutex> mlock(mutex_);
+    queue_.push(item);
+    mlock.unlock();
+    cond_.notify_one();
+  }
+  Queue()=default;
+  Queue(const Queue&) = delete;            // disable copying
+  Queue& operator=(const Queue&) = delete; // disable assignment
+  
+ private:
+  std::queue<T> queue_;
+  std::mutex mutex_;
+  std::condition_variable cond_;
+};
+
+#endif
diff --git a/common/core.h b/common/core.h
new file mode 100644
index 0000000..be32f00
--- /dev/null
+++ b/common/core.h
@@ -0,0 +1,56 @@
+/*
+    Copyright (C) 2018 Open Networking Foundation
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef OPENOLT_CORE_H_
+#define OPENOLT_CORE_H_
+
+#include <grpc++/grpc++.h>
+using grpc::Status;
+#include <openolt.grpc.pb.h>
+
+#include "state.h"
+
+#define COLLECTION_PERIOD 15
+
+extern State state;
+
+Status Enable_(int argc, char *argv[]);
+Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific, uint32_t pir);
+Status DeactivateOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific);
+Status DeleteOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific);
+Status EnablePonIf_(uint32_t intf_id);
+Status DisablePonIf_(uint32_t intf_id);
+Status EnableUplinkIf_(uint32_t intf_id);
+Status DisableUplinkIf_(uint32_t intf_id);
+Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt);
+Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt);
+Status UplinkPacketOut_(uint32_t intf_id, const std::string pkt);
+Status FlowAdd_(uint32_t onu_id,
+                uint32_t flow_id, const std::string flow_type,
+                uint32_t access_intf_id, uint32_t network_intf_id,
+                uint32_t gemport_id, uint32_t priority,
+                const ::openolt::Classifier& classifier,
+                const ::openolt::Action& action);
+Status FlowRemove_(uint32_t flow_id, const std::string flow_type);
+Status Disable_();
+Status Reenable_();
+
+void* stats_collection();
+#endif
diff --git a/common/main.cc b/common/main.cc
new file mode 100644
index 0000000..2898219
--- /dev/null
+++ b/common/main.cc
@@ -0,0 +1,35 @@
+/*
+    Copyright (C) 2018 Open Networking Foundation 
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <iostream>
+
+#include "server.h"
+#include "core.h"
+
+int main(int argc, char** argv) {
+
+    Status status = Enable_(argc, argv);
+    if (!status.ok()) {
+        std::cout << "ERROR: Enable_ failed - "
+                  << status.error_code() << ": " << status.error_message()
+                  << std::endl;
+        return 1;
+    }
+
+    RunServer();
+
+    return 0;
+}
diff --git a/common/server.cc b/common/server.cc
new file mode 100644
index 0000000..54d32ac
--- /dev/null
+++ b/common/server.cc
@@ -0,0 +1,239 @@
+/*
+    Copyright (C) 2018 Open Networking Foundation
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <iostream>
+#include <memory>
+#include <string>
+#include <time.h>
+#include <pthread.h>
+
+#include "Queue.h"
+#include <iostream>
+#include <sstream>
+
+#include "server.h"
+#include "core.h"
+#include "state.h"
+
+#include <grpc++/grpc++.h>
+#include <openolt.grpc.pb.h>
+
+using grpc::Server;
+using grpc::ServerBuilder;
+using grpc::ServerContext;
+using grpc::ServerWriter;
+using grpc::Status;
+
+const char *serverPort = "0.0.0.0:9191";
+int signature;
+
+Queue<openolt::Indication> oltIndQ;
+
+class OpenoltService final : public openolt::Openolt::Service {
+
+    Status DisableOlt(
+            ServerContext* context,
+            const openolt::Empty* request,
+            openolt::Empty* response) override {
+        return Disable_();
+    }
+
+    Status ReenableOlt(
+            ServerContext* context,
+            const openolt::Empty* request,
+            openolt::Empty* response) override {
+        return Reenable_();
+    }
+
+    Status ActivateOnu(
+            ServerContext* context,
+            const openolt::Onu* request,
+            openolt::Empty* response) override {
+        return ActivateOnu_(
+            request->intf_id(),
+            request->onu_id(),
+            ((request->serial_number()).vendor_id()).c_str(),
+            ((request->serial_number()).vendor_specific()).c_str(),
+            request->pir());
+    }
+
+    Status DeactivateOnu(
+            ServerContext* context,
+            const openolt::Onu* request,
+            openolt::Empty* response) override {
+        return DeactivateOnu_(
+            request->intf_id(),
+            request->onu_id(),
+            ((request->serial_number()).vendor_id()).c_str(),
+            ((request->serial_number()).vendor_specific()).c_str());
+    }
+
+    Status DeleteOnu(
+            ServerContext* context,
+            const openolt::Onu* request,
+            openolt::Empty* response) override {
+        return DeleteOnu_(
+            request->intf_id(),
+            request->onu_id(),
+            ((request->serial_number()).vendor_id()).c_str(),
+            ((request->serial_number()).vendor_specific()).c_str());
+    }
+
+    Status OmciMsgOut(
+            ServerContext* context,
+            const openolt::OmciMsg* request,
+            openolt::Empty* response) override {
+        return OmciMsgOut_(
+            request->intf_id(),
+            request->onu_id(),
+            request->pkt());
+    }
+
+    Status OnuPacketOut(
+            ServerContext* context,
+            const openolt::OnuPacket* request,
+            openolt::Empty* response) override {
+        return OnuPacketOut_(
+            request->intf_id(),
+            request->onu_id(),
+            request->pkt());
+    }
+
+    Status UplinkPacketOut(
+            ServerContext* context,
+            const openolt::UplinkPacket* request,
+            openolt::Empty* response) override {
+        return UplinkPacketOut_(
+            request->intf_id(),
+            request->pkt());
+    }
+
+    Status FlowAdd(
+            ServerContext* context,
+            const openolt::Flow* request,
+            openolt::Empty* response) override {
+        return FlowAdd_(
+            request->onu_id(),
+            request->flow_id(),
+            request->flow_type(),
+            request->access_intf_id(),
+            request->network_intf_id(),
+            request->gemport_id(),
+            request->priority(),
+            request->classifier(),
+            request->action());
+    }
+
+    Status FlowRemove(
+            ServerContext* context,
+            const openolt::Flow* request,
+            openolt::Empty* response) override {
+        return FlowRemove_(
+            request->flow_id(),
+            request->flow_type());
+    }
+
+    Status EnableIndication(
+            ServerContext* context,
+            const ::openolt::Empty* request,
+            ServerWriter<openolt::Indication>* writer) override {
+
+        std::cout << "Connection to Voltha established. Indications enabled"
+        << std::endl;
+
+        state.connect();
+
+        while (state.is_connected()) {
+            std::pair<openolt::Indication, bool> ind = oltIndQ.pop(COLLECTION_PERIOD);
+            if (ind.second == false) {
+                /* timeout - do lower priority periodic stuff like stats */
+                stats_collection();
+                continue;
+            }
+            openolt::Indication oltInd = ind.first;
+            bool isConnected = writer->Write(oltInd);
+            if (!isConnected) {
+                //Lost connectivity to this Voltha instance
+                //Put the indication back in the queue for next connecting instance
+                oltIndQ.push(oltInd);
+                state.disconnect();
+            }
+            //oltInd.release_olt_ind()
+        }
+
+        return Status::OK;
+    }
+
+    Status HeartbeatCheck(
+            ServerContext* context,
+            const openolt::Empty* request,
+            openolt::Heartbeat* response) override {
+        response->set_heartbeat_signature(signature);
+
+        return Status::OK;
+    }
+
+    Status EnablePonIf(
+            ServerContext* context,
+            const openolt::Interface* request,
+            openolt::Empty* response) override {
+
+        return EnablePonIf_(request->intf_id());
+    }
+
+    Status DisablePonIf(
+            ServerContext* context,
+            const openolt::Interface* request,
+            openolt::Empty* response) override {
+
+        return DisablePonIf_(request->intf_id());
+    }
+
+    Status Reboot(
+            ServerContext* context,
+            const openolt::Empty* request,
+            openolt::Empty* response) override {
+
+        system("shutdown -r now");
+
+        return Status::OK;
+
+    }
+
+
+};
+
+void RunServer() {
+  OpenoltService service;
+  std::string server_address(serverPort);
+  ServerBuilder builder;
+
+  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
+  builder.RegisterService(&service);
+
+  std::unique_ptr<Server> server(builder.BuildAndStart());
+
+  time_t now;
+  time(&now);
+  signature = (int)now;
+
+  std::cout << "Server listening on " << server_address
+  << ", connection signature : " << signature << std::endl;
+
+
+  server->Wait();
+}
diff --git a/common/server.h b/common/server.h
new file mode 100644
index 0000000..52ef083
--- /dev/null
+++ b/common/server.h
@@ -0,0 +1,23 @@
+/*
+    Copyright (C) 2018 Open Networking Foundation 
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef OPENOLT_SERVER_H_
+#define OPENOLT_SERVER_H_
+
+void RunServer();
+
+#endif
diff --git a/common/state.h b/common/state.h
new file mode 100644
index 0000000..331b09d
--- /dev/null
+++ b/common/state.h
@@ -0,0 +1,35 @@
+#ifndef OPENOLT_STATE_H_
+#define OPENOLT_STATE_H_
+
+class State {
+  public:
+
+    bool is_connected() {
+        return connected_to_voltha;
+    }
+
+    bool is_activated() {
+        return activated;
+    }
+
+    void connect() {
+        connected_to_voltha = true;
+    }
+
+    void disconnect() {
+        connected_to_voltha = false;
+    }
+
+    void activate() {
+        activated = true;
+    }
+
+    void deactivate() {
+        activated = false;
+    }
+
+  private:
+    bool connected_to_voltha = false;
+    bool activated = false;
+};
+#endif