Initial commit of a simple OpenOLT simulator

make openoltsim
./build/openoltsim

Openoltsim is meant to simulate an OLT running the OpenOLT driver.
Currently, it only sends the the "olt up" indication allowing
voltha pre-provision/enable commands to connect to openoltsim.

Change-Id: I95910657d269e37f4e4943d2b91f2780ecd8fd08
diff --git a/Makefile b/Makefile
index a9e4a93..2928a1b 100644
--- a/Makefile
+++ b/Makefile
@@ -184,6 +184,19 @@
 ########################################################################
 ##
 ##
+##        openoltsim
+##
+##
+SIM_SRCS = $(wildcard openoltsim/*.cc)
+SIM_OBJS = $(SIM_SRCS:.cc=.o)
+SIM_DEPS = $(SIM_SRCS:.cc=.d)
+openoltsim: $(BUILD_DIR)/openoltsim
+$(BUILD_DIR)/openoltsim: protos $(SIM_OBJS)
+	$(CXX) -pthread -L/usr/local/lib $(SIM_OBJS) $(OPENOLT_API_LIB) /usr/local/lib/libprotobuf.a -o $@ -lgrpc++ -lgrpc -lpthread -ldl
+
+########################################################################
+##
+##
 ##        Main
 ##
 ##
@@ -238,4 +251,4 @@
 distclean:
 	rm -rf $(BUILD_DIR)
 
-.PHONY: onl sdk bal protos prereq
+.PHONY: onl sdk bal protos prereq openoltsim
diff --git a/openoltsim/Queue.h b/openoltsim/Queue.h
new file mode 100644
index 0000000..854851e
--- /dev/null
+++ b/openoltsim/Queue.h
@@ -0,0 +1,60 @@
+//
+// 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>
+
+template <typename T>
+class Queue
+{
+ public:
+
+  T pop() 
+  {
+    std::unique_lock<std::mutex> mlock(mutex_);
+    while (queue_.empty())
+    {
+      cond_.wait(mlock);
+    }
+    auto val = queue_.front();
+    queue_.pop();
+    return val;
+  }
+
+  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/openoltsim/core.cc b/openoltsim/core.cc
new file mode 100644
index 0000000..edba795
--- /dev/null
+++ b/openoltsim/core.cc
@@ -0,0 +1,98 @@
+/*
+    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 "Queue.h"
+#include <iostream>
+#include <sstream>
+
+#include "core.h"
+
+Status Enable_() {
+    return Status::OK;
+}
+
+Status Disable_() {
+    return Status::OK;
+}
+
+Status Reenable_() {
+    return Status::OK;
+}
+
+Status EnablePonIf_(uint32_t intf_id) {
+    return Status::OK;
+}
+
+Status DisableUplinkIf_(uint32_t intf_id) {
+    return Status::OK;
+}
+
+Status EnableUplinkIf_(uint32_t intf_id) {
+    return Status::OK;
+}
+
+Status DisablePonIf_(uint32_t intf_id) {
+    return Status::OK;
+}
+
+Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific, uint32_t pir) {
+    return Status::OK;
+}
+
+Status DeactivateOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific) {
+    return Status::OK;
+}
+
+Status DeleteOnu_(uint32_t intf_id, uint32_t onu_id,
+    const char *vendor_id, const char *vendor_specific) {
+    return Status::OK;;
+}
+
+Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
+    return Status::OK;
+}
+
+Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
+    return Status::OK;
+}
+
+Status UplinkPacketOut_(uint32_t intf_id, const std::string pkt) {
+    return Status::OK;
+}
+
+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_value,
+                const ::openolt::Classifier& classifier,
+                const ::openolt::Action& action) {
+    return Status::OK;
+}
+
+Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) {
+    return Status::OK;
+}
+
+Status SchedRemove_(int intf_id, int onu_id, int agg_port_id) {
+    return Status::OK;
+}
diff --git a/openoltsim/core.h b/openoltsim/core.h
new file mode 100644
index 0000000..ab7fbf7
--- /dev/null
+++ b/openoltsim/core.h
@@ -0,0 +1,63 @@
+/*
+    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 "Queue.h"
+
+extern Queue<openolt::Indication> oltIndQ;
+
+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 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/openoltsim/main.cc b/openoltsim/main.cc
new file mode 100644
index 0000000..652378e
--- /dev/null
+++ b/openoltsim/main.cc
@@ -0,0 +1,50 @@
+/*
+    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 <unistd.h>
+#include <pthread.h>
+
+#include "server.h"
+#include "core.h"
+#include "state.h"
+
+void* RunSim(void *) {
+    while (!state::is_connected()) {
+        sleep(5);
+    }
+
+    // Send Olt up indication
+    {
+        openolt::Indication ind;
+        openolt::OltIndication* olt_ind = new openolt::OltIndication;
+        olt_ind->set_oper_state("up");
+        ind.set_allocated_olt_ind(olt_ind);
+        std::cout << "olt indication, oper_state:" << ind.olt_ind().oper_state() << std::endl;
+        oltIndQ.push(ind);
+    }
+
+    // TODO - Add interface and onu indication events
+}
+
+int main(int argc, char** argv) {
+    pthread_t simThread;
+
+    pthread_create(&simThread, NULL, RunSim, NULL);
+    RunServer();
+
+    return 0;
+}
diff --git a/openoltsim/server.cc b/openoltsim/server.cc
new file mode 100644
index 0000000..1277798
--- /dev/null
+++ b/openoltsim/server.cc
@@ -0,0 +1,222 @@
+/*
+    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 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) {
+            auto oltInd = oltIndQ.pop();
+            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/openoltsim/server.h b/openoltsim/server.h
new file mode 100644
index 0000000..52ef083
--- /dev/null
+++ b/openoltsim/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/openoltsim/state.cc b/openoltsim/state.cc
new file mode 100644
index 0000000..c3b2966
--- /dev/null
+++ b/openoltsim/state.cc
@@ -0,0 +1,41 @@
+#include <mutex>
+
+namespace state {
+
+    bool connected_to_voltha = false;
+    bool activated = false;
+    std::mutex state_lock;
+
+    bool is_connected() {
+        return connected_to_voltha;
+    }
+
+    bool is_activated() {
+        return activated;
+    }
+
+    void connect() {
+        state_lock.lock();
+        connected_to_voltha = true;
+        state_lock.unlock();
+    }
+
+    void disconnect() {
+        state_lock.lock();
+        connected_to_voltha = false;
+        state_lock.unlock();
+    }
+
+    void activate() {
+        state_lock.lock();
+        activated = true;
+        state_lock.unlock();
+    }
+
+    void deactivate() {
+        state_lock.lock();
+        activated = false;
+        state_lock.unlock();
+    }
+
+}
diff --git a/openoltsim/state.h b/openoltsim/state.h
new file mode 100644
index 0000000..c392a01
--- /dev/null
+++ b/openoltsim/state.h
@@ -0,0 +1,13 @@
+#ifndef OPENOLT_STATE_H_
+#define OPENOLT_STATE_H_
+
+namespace state {
+    bool is_activated();
+    bool is_connected();
+    void connect();
+    void disconnect();
+    void activate();
+    void deactivate();
+}
+
+#endif