VOL-1050 Delay stats collection after connection

Move the state of connectivity and activation to its own file (state)
It handles starting/stopping statistics collection and provides a hook for future actions

Change-Id: If6a7b4015824716ff45781e39f29ca7f06933702
diff --git a/src/indications.cc b/src/indications.cc
index cbde803..df60fcb 100644
--- a/src/indications.cc
+++ b/src/indications.cc
@@ -19,7 +19,10 @@
 #include "utils.h"
 #include "stats_collection.h"
 #include "translation.h"
+#include "state.h"
+
 #include <string>
+
 extern "C"
 {
 #include <bcmos_system.h>
@@ -32,6 +35,7 @@
 Queue<openolt::Indication> oltIndQ;
 //Queue<openolt::Indication*> oltIndQ;
 
+
 bool subscribed = false;
 
 bcmos_errno OmciIndication(bcmbal_obj *obj);
@@ -44,10 +48,10 @@
     bcmbal_access_terminal_oper_status_change *acc_term_ind = (bcmbal_access_terminal_oper_status_change *)obj;
     if (acc_term_ind->data.new_oper_status == BCMBAL_STATUS_UP) {
         olt_ind->set_oper_state("up");
-        start_collecting_statistics();
+        state::activate();
     } else {
         olt_ind->set_oper_state("down");
-        stop_collecting_statistics();
+        state::deactivate();
     }
     ind.set_allocated_olt_ind(olt_ind);
     std::cout << "olt indication, oper_state:" << ind.olt_ind().oper_state() << std::endl;
diff --git a/src/indications.h b/src/indications.h
index 374b890..774e689 100644
--- a/src/indications.h
+++ b/src/indications.h
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2018 Open Networking Foundation 
+    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
diff --git a/src/server.cc b/src/server.cc
index cfd6f3f..ae2cd56 100644
--- a/src/server.cc
+++ b/src/server.cc
@@ -29,6 +29,7 @@
 #include "core.h"
 #include "indications.h"
 #include "stats_collection.h"
+#include "state.h"
 
 #include <grpc++/grpc++.h>
 #include <openolt.grpc.pb.h>
@@ -105,17 +106,20 @@
             ServerWriter<openolt::Indication>* writer) override {
         std::cout << "Connection to Voltha established. Indications enabled"
         << std::endl;
-        bool isConnected = true;
-        while (isConnected) {
+        state::connect();
+
+        while (state::is_connected) {
             auto oltInd = oltIndQ.pop();
-            isConnected = writer->Write(oltInd);
+            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;
     }
 
diff --git a/src/state.cc b/src/state.cc
new file mode 100644
index 0000000..54339e1
--- /dev/null
+++ b/src/state.cc
@@ -0,0 +1,50 @@
+#include "stats_collection.h"
+#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;
+        if (activated) {
+            start_collecting_statistics();
+        }
+        state_lock.unlock();
+    }
+
+    void disconnect() {
+        state_lock.lock();
+        connected_to_voltha = false;
+        stop_collecting_statistics();
+        state_lock.unlock();
+    }
+
+    void activate() {
+        state_lock.lock();
+        activated = true;
+        if (connected_to_voltha) {
+            start_collecting_statistics();
+        }
+        state_lock.unlock();
+    }
+
+    void deactivate() {
+        state_lock.lock();
+        activated = false;
+        stop_collecting_statistics();
+        state_lock.unlock();
+    }
+
+}
diff --git a/src/state.h b/src/state.h
new file mode 100644
index 0000000..c392a01
--- /dev/null
+++ b/src/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
diff --git a/src/stats_collection.cc b/src/stats_collection.cc
index ef922c3..d364384 100644
--- a/src/stats_collection.cc
+++ b/src/stats_collection.cc
@@ -167,6 +167,9 @@
     time_t now;
 
     while(isCollectingStatistics) {
+
+        std::cout << "Collecting statistics" << std::endl;
+
         //Ports statistics
 
         //Uplink ports