SEBA-758 send periodic port stats to voltha
SEBA-790 get flow, gemport, and tcont information through API
fix lint errors

Change-Id: I10909e1992eba71d8e54c976ccbcea8778e35539
diff --git a/core/mediator.go b/core/mediator.go
index 0756651..97bd128 100644
--- a/core/mediator.go
+++ b/core/mediator.go
@@ -24,6 +24,7 @@
 	"strconv"
 	"strings"
 	"sync"
+	"syscall"
 	"time"
 
 	"github.com/opencord/voltha-bbsim/common/logger"
@@ -31,19 +32,21 @@
 	log "github.com/sirupsen/logrus"
 )
 
+// Constants for tester mode
 const (
 	DEFAULT Mode = iota
 	AAA
 	BOTH
 )
 
-// Store emulation mode
+// Mode store emulation mode
 type Mode int
 
 // AutoONUActivate is flag for Auto ONU Add on/off.
 var AutoONUActivate int
 
-type option struct {
+//Option is the structure to store the user provided flag values
+type Option struct {
 	address                  string
 	port                     uint32
 	mgmtGrpcPort             uint32
@@ -61,9 +64,9 @@
 	Debuglvl                 string
 }
 
-// GetOptions receives command line options and stores them in option structure
-func GetOptions() *option {
-	o := new(option)
+// GetOptions receives command line options and stores them in Option structure
+func GetOptions() *Option {
+	o := new(Option)
 	addressport := flag.String("H", ":50060", "IP address:port")
 	oltid := flag.Int("id", 0, "OLT-ID")
 	npon := flag.Int("i", 1, "Number of PON-IF ports")
@@ -88,13 +91,25 @@
 	o.Debuglvl = *debg
 	o.oltid = uint32(*oltid)
 	o.npon = uint32(*npon)
+	// make sure to have at-lease 1 PON port available
+	if o.npon == 0 {
+		o.npon = 1
+	}
 	o.nonus = uint32(*nonus)
+	// make sure to have at-least 1 ONU is available
+	if o.nonus == 0 {
+		o.nonus = 1
+	}
 	o.aaawait = *aaawait
 	o.dhcpwait = *dhcpwait
 	o.dhcpservip = *dhcpservip
 	o.intvl = *intvl
 	o.interactiveOnuActivation = *interactiveOnuActivation
 	o.KafkaBroker = *kafkaBroker
+	// make sure that 'IP:Port' or ':Port' provided properly.
+	if !strings.Contains(*addressport, ":") {
+		log.Fatal("Invalid address given (missing colon)")
+	}
 	o.address = strings.Split(*addressport, ":")[0]
 	tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1])
 	o.port = uint32(tmp)
@@ -108,15 +123,16 @@
 	return o
 }
 
-type mediator struct {
-	opt         *option
+//Mediator stores option, server and testmanager to mediate
+type Mediator struct {
+	opt         *Option
 	server      *Server
 	testmanager *TestManager
 }
 
-// NewMediator returns a new mediator object
-func NewMediator(o *option) *mediator {
-	m := new(mediator)
+// NewMediator returns a new Mediator object
+func NewMediator(o *Option) *Mediator {
+	m := new(Mediator)
 	m.opt = o
 	logger.WithFields(log.Fields{
 		"ip":        o.address,
@@ -124,12 +140,12 @@
 		"pon_ports": o.npon,
 		"onus":      o.nonus,
 		"mode":      o.Mode,
-	}).Debug("New mediator")
+	}).Debug("New Mediator")
 	return m
 }
 
-// Start mediator
-func (m *mediator) Start() {
+// Start Mediator
+func (m *Mediator) Start() {
 	var wg sync.WaitGroup
 	opt := m.opt
 	server := NewCore(opt)
@@ -147,7 +163,7 @@
 	}()
 
 	c := make(chan os.Signal, 1)
-	signal.Notify(c, os.Interrupt)
+	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
 	go func() {
 		defer func() {
 			logger.Debug("SIGINT catcher Done")
@@ -157,9 +173,15 @@
 			wg.Add(1)
 			logger.Debug("SIGINT %v", sig)
 			close(c)
-			server.Stop() // Non-blocking
-			tm.Stop()     // Non-blocking
-			server.stopMgmtServer()
+			server.Stop()    // Non-blocking
+			err := tm.Stop() // Non-blocking
+			if err != nil {
+				logger.Error("Error stopping the TestManager %v", err)
+			}
+			err = server.stopMgmtServer()
+			if err != nil {
+				logger.Error("Error stopping the Management Server %v", err)
+			}
 			server.wg.Done()
 			return
 		}
@@ -169,7 +191,7 @@
 }
 
 // Mediate method is invoked on OLT and ONU state change
-func (m *mediator) Mediate() {
+func (m *Mediator) Mediate() {
 	defer logger.Debug("Mediate Done")
 	for sr := range m.server.stateRepCh {
 		next := sr.next
@@ -194,31 +216,37 @@
 	}
 }
 
-func transitOlt(current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error {
+func transitOlt(current device.State, next device.State, tm *TestManager, o *Option) error {
 	logger.Debug("trnsitOlt called current:%d , next:%d", current, next)
-	if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE {
-		tm.Start()
+	if current == device.OltPreactive && next == device.OltActive {
+		err := tm.Start()
+		if err != nil {
+			logger.Error("Error starting the TestManager %v", err)
+		}
 		nniup, _ := makeNniName(o.oltid)
-		activateDHCPServer(nniup, o.dhcpservip)
-	} else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE {
-		tm.Stop()
-	} else if current == device.OLT_ACTIVE && next == device.OLT_INACTIVE {
-		// Reboot case
-		// TODO Point of discussion
+		err = activateDHCPServer(nniup, o.dhcpservip)
+		if err != nil {
+			logger.Error("Error activating DHCP Server %v", err)
+		}
+	} else if current == device.OltActive && next == device.OltPreactive {
+		err := tm.Stop()
+		if err != nil {
+			logger.Error("Error stoping the TestManager %v", err)
+		}
 	}
 	return nil
 }
 
-func transitOnu(key device.Devkey, previous device.DeviceState, current device.DeviceState, tm *TestManager, o *option) error {
+func transitOnu(key device.Devkey, previous device.State, current device.State, tm *TestManager, o *Option) error {
 	logger.Debug("transitOnu called with key: %v, previous: %s, current: %s", key, device.ONUState[previous], device.ONUState[current])
 	if o.Mode == AAA || o.Mode == BOTH {
-		if previous == device.ONU_ACTIVE && current == device.ONU_OMCIACTIVE {
+		if previous == device.OnuActive && current == device.OnuOmciActive {
 			logger.Debug("Starting WPASupplicant for device %v", key)
 			t := tm.CreateTester("AAA", o, key, activateWPASupplicant, o.aaawait)
 			if err := tm.StartTester(t); err != nil {
 				logger.Error("Cannot Start AAA Executer error:%v", err)
 			}
-		} else if previous == device.ONU_OMCIACTIVE && current == device.ONU_INACTIVE {
+		} else if previous == device.OnuOmciActive && current == device.OnuInactive {
 			if err := tm.StopTester("AAA", key); err != nil {
 				logger.Error("Cannot Stop AAA Executer error:%v", err)
 			}
@@ -226,13 +254,13 @@
 	}
 
 	if o.Mode == BOTH {
-		if previous == device.ONU_OMCIACTIVE && current == device.ONU_AUTHENTICATED {
+		if previous == device.OnuOmciActive && current == device.OnuAuthenticated {
 			logger.Debug("Starting DHCP client for device %v", key)
 			t := tm.CreateTester("DHCP", o, key, activateDHCPClient, o.dhcpwait)
 			if err := tm.StartTester(t); err != nil {
 				logger.Error("Cannot Start DHCP Executer error:%v", err)
 			}
-		} else if previous == device.ONU_AUTHENTICATED && current == device.ONU_INACTIVE {
+		} else if previous == device.OnuAuthenticated && current == device.OnuInactive {
 			if err := tm.StopTester("DHCP", key); err != nil {
 				logger.Error("Cannot Stop DHCP Executer error:%v", err)
 			}