VOL-291 : PON simulator refactoring for cluster integration
- Added ponsim build target in Makefile
- Added new option to vcore to select comm type with ponsim
- Modified all proto files to include destination go package
Amendments:
- Clean up based on review comments
- Properly close GRPC connections in ponsim_olt adapter
- Added voltha namespace to some k8s templates
Change-Id: I2f349fa7b3550a8a8cc8fc676cc896f33fbb9372
diff --git a/ponsim/v2/common/interval_handler.go b/ponsim/v2/common/interval_handler.go
new file mode 100644
index 0000000..973865e
--- /dev/null
+++ b/ponsim/v2/common/interval_handler.go
@@ -0,0 +1,164 @@
+package common
+
+import (
+ "github.com/sirupsen/logrus"
+ "sync"
+ "time"
+)
+
+/*
+ IntervalHandler is used to run a routine at regular intervals and provide all the necessary
+ utilities to manage the execution.
+*/
+
+type IntervalHandler struct {
+ // Interval period in between each execution (in seconds?)
+ Interval int
+ // function to execute after each interval
+ function func()
+ // Channel listening to execution events
+ execute chan _ExecutionState
+ // Channel listening for a termination event
+ terminate chan struct{}
+ // Current execution state of the handler
+ state _ExecutionState
+ wg sync.WaitGroup
+}
+
+// Define execution state constants
+type _ExecutionState uint8
+
+const (
+ STARTED _ExecutionState = iota
+ STOPPED
+ PAUSED
+ RESUMED
+)
+
+// Execute state string equivalents
+var _ExecutionStateEnum = []string{
+ "STARTED",
+ "STOPPED",
+ "PAUSED",
+ "RESUMED",
+}
+
+func (s _ExecutionState) String() string {
+ return _ExecutionStateEnum[s]
+}
+
+/*
+NewIntervalHandler instantiates a new interval based function execution handler
+*/
+func NewIntervalHandler(interval int, function func()) *IntervalHandler {
+ handler := &IntervalHandler{
+ Interval: interval,
+ function: function,
+ }
+
+ handler.execute = make(chan _ExecutionState)
+ handler.terminate = make(chan struct{})
+ handler.state = STOPPED
+
+ return handler
+}
+
+/*
+_Execute is a routine running concurrently and listening to execution events
+*/
+func (h *IntervalHandler) _Execute() {
+ defer h.wg.Done()
+ for {
+ select {
+ case h.state = <-h.execute:
+ Logger().WithFields(logrus.Fields{
+ "handler": h,
+ }).Debug("Processing execution state")
+ switch h.state {
+ case STARTED:
+ case PAUSED:
+ case RESUMED:
+ h.state = STARTED
+ case STOPPED:
+ fallthrough
+ default:
+ h.terminate <- struct{}{}
+ }
+
+ case <-h.terminate:
+ return
+
+ default:
+ if h.state == STARTED {
+ h.function()
+ time.Sleep(time.Duration(h.Interval) * time.Second)
+ } else {
+ // TODO: replace hardcoded delay with a configurable parameter
+ time.Sleep(1 * time.Second)
+ }
+ }
+ }
+}
+
+/*
+Start initiates the interval based function execution
+*/
+func (h *IntervalHandler) Start() {
+ Logger().WithFields(logrus.Fields{
+ "handler": h,
+ }).Info("Starting interval handler")
+
+ if h.execute == nil {
+ return
+ }
+ if h.state == STOPPED {
+ go h._Execute()
+ h.execute <- STARTED
+ }
+}
+
+/*
+Pause interrupts the interval based function execution
+*/
+func (h *IntervalHandler) Pause() {
+ Logger().WithFields(logrus.Fields{
+ "handler": h,
+ }).Info("Pausing interval handler")
+
+ if h.execute == nil || h.state == STOPPED {
+ return
+ }
+ if h.state == STARTED {
+ h.execute <- PAUSED
+ }
+}
+
+/*
+Resume continues the interval based function execution
+*/
+func (h *IntervalHandler) Resume() {
+ Logger().WithFields(logrus.Fields{
+ "handler": h,
+ }).Info("Resuming interval handler")
+
+ if h.execute == nil || h.state == STOPPED {
+ return
+ }
+ if h.state == PAUSED {
+ h.execute <- RESUMED
+ }
+}
+
+/*
+Stop terminates the interval based function execution
+*/
+func (h *IntervalHandler) Stop() {
+ Logger().WithFields(logrus.Fields{
+ "handler": h,
+ }).Info("Stopping interval handler")
+
+ if h.execute == nil || h.state == STOPPED {
+ return
+ }
+ h.execute <- STOPPED
+}