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/src/Queue.h b/src/Queue.h
deleted file mode 100644
index 30fbbda..0000000
--- a/src/Queue.h
+++ /dev/null
@@ -1,72 +0,0 @@
-//
-// 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/src/core.cc b/src/core.cc
index ae54c56..9bdb72d 100644
--- a/src/core.cc
+++ b/src/core.cc
@@ -38,12 +38,34 @@
 
 State state;
 
-Status Enable_() {
+static Status SchedAdd_(int intf_id, int onu_id, int agg_port_id);
+static Status SchedRemove_(int intf_id, int onu_id, int agg_port_id);
+
+static inline int mk_sched_id(int onu_id) {
+    return 1023 + onu_id;
+}
+
+static inline int mk_agg_port_id(int onu_id) {
+    return 1023 + onu_id;
+}
+
+Status Enable_(int argc, char *argv[]) {
     bcmbal_access_terminal_cfg acc_term_obj;
     bcmbal_access_terminal_key key = { };
 
     if (!state.is_activated()) {
         std::cout << "Enable OLT" << std::endl;
+
+        bcmbal_init(argc, argv, NULL);
+
+        Status status = SubscribeIndication();
+        if (!status.ok()) {
+            std::cout << "ERROR: SubscribeIndication failed - "
+                      << status.error_code() << ": " << status.error_message()
+                      << std::endl;
+            return status;
+        }
+
         key.access_term_id = DEFAULT_ATERM_ID;
         BCMBAL_CFG_INIT(&acc_term_obj, access_terminal, key);
         BCMBAL_CFG_PROP_SET(&acc_term_obj, access_terminal, admin_state, BCMBAL_STATE_UP);
diff --git a/src/core.h b/src/core.h
deleted file mode 100644
index b0c3b5f..0000000
--- a/src/core.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
-    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"
-
-extern State state;
-
-Status Enable_();
-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_();
-
-
-static Status SchedAdd_(int intf_id, int onu_id, int agg_port_id);
-static Status SchedRemove_(int intf_id, int onu_id, int agg_port_id);
-
-static inline int mk_sched_id(int onu_id) {
-    return 1023 + onu_id;
-}
-
-static inline int mk_agg_port_id(int onu_id) {
-    return 1023 + onu_id;
-}
-
-#endif
diff --git a/src/indications.cc b/src/indications.cc
index 4d7a393..203b675 100644
--- a/src/indications.cc
+++ b/src/indications.cc
@@ -32,7 +32,7 @@
 
 using grpc::Status;
 
-Queue<openolt::Indication> oltIndQ;
+extern Queue<openolt::Indication> oltIndQ;
 //Queue<openolt::Indication*> oltIndQ;
 
 
diff --git a/src/main.cc b/src/main.cc
deleted file mode 100644
index 3c05489..0000000
--- a/src/main.cc
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
-    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 "indications.h"
-#include "server.h"
-#include "core.h"
-
-extern "C"
-{
-#include <bal_api.h>
-#include <bal_api_end.h>
-}
-
-int main(int argc, char** argv) {
-
-/*
-    if (argc < 5) {
-        std::cout << "Missing arguments" << std::endl;
-        exit(1);
-    }
-*/
-
-    bcmbal_init(argc, argv, NULL);
-
-    Status status = SubscribeIndication();
-    if (!status.ok()) {
-        std::cout << "ERROR: SubscribeIndication failed - "
-                  << status.error_code() << ": " << status.error_message()
-                  << std::endl;
-        return 1;
-    }
-
-    status = Enable_();
-    if (!status.ok()) {
-        std::cout << "ERROR: Enable_ failed - "
-                  << status.error_code() << ": " << status.error_message()
-                  << std::endl;
-        return 1;
-    }
-
-    RunServer();
-
-  return 0;
-}
diff --git a/src/server.cc b/src/server.cc
deleted file mode 100644
index 8cd83ff..0000000
--- a/src/server.cc
+++ /dev/null
@@ -1,239 +0,0 @@
-/*
-    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 "indications.h"
-#include "stats_collection.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;
-
-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/src/server.h b/src/server.h
deleted file mode 100644
index 52ef083..0000000
--- a/src/server.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
-    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/src/state.h b/src/state.h
deleted file mode 100644
index 331b09d..0000000
--- a/src/state.h
+++ /dev/null
@@ -1,35 +0,0 @@
-#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
diff --git a/src/stats_collection.h b/src/stats_collection.h
index 0437634..fe3c27a 100644
--- a/src/stats_collection.h
+++ b/src/stats_collection.h
@@ -8,8 +8,6 @@
 #include <bal_model_types.h>
 }
 
-#define COLLECTION_PERIOD 15
-
 void init_stats();
 void stop_collecting_statistics();
 openolt::PortStatistics* get_default_port_statistics();
@@ -18,7 +16,6 @@
 openolt::FlowStatistics* get_default_flow_statistics();
 openolt::FlowStatistics* collectFlowStatistics(bcmbal_flow_id flow_id, bcmbal_flow_type flow_type);
 #endif
-void* stats_collection();
 void register_new_flow(bcmbal_flow_key key);