VOL-1243: Added logic for thread safety

- Thread safety was added at the proxy level
- Refactored the test init in a base_test structure
- Fixed issue with writing to kv
- Added profiling for locking period

Amendments:

- Comment out a cleanup statement causing KV corruption (as per VOL-1293)
- Added missing license

Change-Id: Id6658270dbb8b738abeef9e9e1d349dce36501bc
diff --git a/db/model/proxy_test.go b/db/model/proxy_test.go
index 6226caa..2d831c1 100644
--- a/db/model/proxy_test.go
+++ b/db/model/proxy_test.go
@@ -19,118 +19,34 @@
 	"encoding/hex"
 	"encoding/json"
 	"github.com/google/uuid"
-	"github.com/opencord/voltha-go/common/log"
-	"github.com/opencord/voltha-go/protos/common"
-	"github.com/opencord/voltha-go/protos/openflow_13"
 	"github.com/opencord/voltha-go/protos/voltha"
+	"github.com/opencord/voltha-go/protos/openflow_13"
 	"reflect"
 	"strconv"
 	"testing"
 )
 
-type proxyTest struct {
-	Root      *root
-	Backend   *Backend
-	Proxy     *Proxy
-	DbPrefix  string
-	DbType    string
-	DbHost    string
-	DbPort    int
-	DbTimeout int
-}
-
 var (
-	pt = &proxyTest{
-		DbPrefix: "service/voltha/data/core/0001",
-		DbType:   "etcd",
-		//DbHost:    "10.102.58.0",
-		DbHost:    "localhost",
-		DbPort:    2379,
-		DbTimeout: 5,
-	}
-	ports = []*voltha.Port{
-		{
-			PortNo:     123,
-			Label:      "test-port-0",
-			Type:       voltha.Port_PON_OLT,
-			AdminState: common.AdminState_ENABLED,
-			OperStatus: common.OperStatus_ACTIVE,
-			DeviceId:   "etcd_port-0-device-id",
-			Peers:      []*voltha.Port_PeerPort{},
-		},
-	}
-
-	stats = &openflow_13.OfpFlowStats{
-		Id: 1111,
-	}
-	flows = &openflow_13.Flows{
-		Items: []*openflow_13.OfpFlowStats{stats},
-	}
-	device = &voltha.Device{
-		Id:         devId,
-		Type:       "simulated_olt",
-		Address:    &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
-		AdminState: voltha.AdminState_PREPROVISIONED,
-		Flows:      flows,
-		Ports:      ports,
-	}
-	devId          string
-	targetDeviceId string
-
-	preAddExecuted      = false
-	postAddExecuted     = false
-	preUpdateExecuted   = false
-	postUpdateExecuted  = false
-	preRemoveExecuted   = false
-	postRemoveExecuted = false
 )
 
-func init() {
-	log.AddPackage(log.JSON, log.DebugLevel, nil)
-	log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
-
-	defer log.CleanUp()
-
-	pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
-
-	msgClass := &voltha.Voltha{}
-	root := NewRoot(msgClass, pt.Backend)
-	pt.Root = root.Load(msgClass)
-
-	GetProfiling().Report()
-
-	pt.Proxy = pt.Root.GetProxy("/", false)
-}
-
-func commonCallback(args ...interface{}) interface{} {
-	log.Infof("Running common callback - arg count: %s", len(args))
-
-	for i := 0; i < len(args); i++ {
-		log.Infof("ARG %d : %+v", i, args[i])
-	}
-	execStatus := args[1].(*bool)
-
-	// Inform the caller that the callback was executed
-	*execStatus = true
-
-	return nil
-}
-
 func Test_Proxy_1_1_Add_NewDevice(t *testing.T) {
 	devIdBin, _ := uuid.New().MarshalBinary()
 	devId = "0001" + hex.EncodeToString(devIdBin)[:12]
 
-	pt.Proxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
-	pt.Proxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
+	preAddExecuted := false
+	postAddExecuted := false
+
+	modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, commonCallback, "PRE_ADD instructions", &preAddExecuted)
+	modelTestConfig.RootProxy.RegisterCallback(POST_ADD, commonCallback, "POST_ADD instructions", &postAddExecuted)
 
 	device.Id = devId
-	if added := pt.Proxy.Add("/devices", device, ""); added == nil {
+	if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
 		t.Error("Failed to add device")
 	} else {
 		t.Logf("Added device : %+v", added)
 	}
 
-	if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
+	if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
 		t.Error("Failed to find added device")
 	} else {
 		djson, _ := json.Marshal(d)
@@ -147,7 +63,7 @@
 
 func Test_Proxy_1_2_Add_ExistingDevice(t *testing.T) {
 	device.Id = devId
-	if added := pt.Proxy.Add("/devices", device, ""); added == nil {
+	if added := modelTestConfig.RootProxy.Add("/devices", device, ""); added == nil {
 		t.Logf("Successfully detected that the device already exists: %s", devId)
 	} else {
 		t.Errorf("A new device should not have been created : %+v", added)
@@ -156,7 +72,7 @@
 }
 
 func Test_Proxy_2_1_Get_AllDevices(t *testing.T) {
-	devices := pt.Proxy.Get("/devices", 1, false, "")
+	devices := modelTestConfig.RootProxy.Get("/devices", 1, false, "")
 
 	if len(devices.([]interface{})) == 0 {
 		t.Error("there are no available devices to retrieve")
@@ -168,7 +84,7 @@
 }
 
 func Test_Proxy_2_2_Get_SingleDevice(t *testing.T) {
-	if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
+	if d := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
 		t.Errorf("Failed to find device : %s", targetDeviceId)
 	} else {
 		djson, _ := json.Marshal(d)
@@ -178,9 +94,11 @@
 }
 
 func Test_Proxy_3_1_Update_Device_WithRootProxy(t *testing.T) {
-	if retrieved := pt.Proxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
+	if retrieved := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 1, false, ""); retrieved == nil {
 		t.Error("Failed to get device")
 	} else {
+		t.Logf("Found raw device (root proxy): %+v", retrieved)
+
 		var fwVersion int
 		if retrieved.(*voltha.Device).FirmwareVersion == "n/a" {
 			fwVersion = 0
@@ -189,15 +107,15 @@
 			fwVersion += 1
 		}
 
-		preUpdateExecuted = false
-		postUpdateExecuted = false
+		preUpdateExecuted := false
+		postUpdateExecuted := false
 
-		pt.Proxy.RegisterCallback(
+		modelTestConfig.RootProxy.RegisterCallback(
 			PRE_UPDATE,
 			commonCallback,
 			"PRE_UPDATE instructions (root proxy)", &preUpdateExecuted,
 		)
-		pt.Proxy.RegisterCallback(
+		modelTestConfig.RootProxy.RegisterCallback(
 			POST_UPDATE,
 			commonCallback,
 			"POST_UPDATE instructions (root proxy)", &postUpdateExecuted,
@@ -208,16 +126,17 @@
 		retrieved.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
 		//t.Logf("Before update : %+v", cloned)
 
-		if afterUpdate := pt.Proxy.Update("/devices/"+targetDeviceId, retrieved, false, ""); afterUpdate == nil {
+		if afterUpdate := modelTestConfig.RootProxy.Update("/devices/"+targetDeviceId, retrieved, false, ""); afterUpdate == nil {
 			t.Error("Failed to update device")
 		} else {
 			t.Logf("Updated device : %+v", afterUpdate.(Revision).GetData())
 		}
-		if d := pt.Proxy.Get("/devices/"+targetDeviceId, 0, false, ""); !reflect.ValueOf(d).IsValid() {
+		if d := modelTestConfig.RootProxy.Get("/devices/"+targetDeviceId, 1, false, ""); !reflect.ValueOf(d).IsValid() {
 			t.Error("Failed to find updated device (root proxy)")
 		} else {
 			djson, _ := json.Marshal(d)
-			t.Logf("Found device (root proxy): %s", string(djson))
+
+			t.Logf("Found device (root proxy): %s raw: %+v", string(djson), d)
 		}
 
 		if !preUpdateExecuted {
@@ -231,20 +150,20 @@
 
 func Test_Proxy_3_2_Update_Flow_WithSubProxy(t *testing.T) {
 	// Get a device proxy and update a specific port
-	devflowsProxy := pt.Root.GetProxy("/devices/"+devId+"/flows", false)
+	devflowsProxy := modelTestConfig.Root.GetProxy("/devices/"+devId+"/flows", false)
 	flows := devflowsProxy.Get("/", 0, false, "")
 	//flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2222
-	flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244
-	//flows.(*openflow_13.Flows).Items[0].TableId = 2244
+	//flows.([]interface{})[0].(*openflow_13.Flows).Items[0].TableId = 2244
+	flows.(*openflow_13.Flows).Items[0].TableId = 2244
 	t.Logf("before updated flows: %+v", flows)
 
-	//devPortsProxy := pt.Root.node.GetProxy("/devices/"+devId+"/ports", false)
+	//devPortsProxy := modelTestConfig.RootProxy.node.GetProxy("/devices/"+devId+"/ports", false)
 	//port123 := devPortsProxy.Get("/123", 0, false, "")
 	//t.Logf("got ports: %+v", port123)
 	//port123.(*voltha.Port).OperStatus = common.OperStatus_DISCOVERED
 
-	preUpdateExecuted = false
-	postUpdateExecuted = false
+	preUpdateExecuted := false
+	postUpdateExecuted := false
 
 	devflowsProxy.RegisterCallback(
 		PRE_UPDATE,
@@ -263,7 +182,7 @@
 		t.Errorf("Local changes have changed the KV store contents -  local:%+v, kv: %+v", flows, kvFlows)
 	}
 
-	if updated := devflowsProxy.Update("/", flows.([]interface{})[0], false, ""); updated == nil {
+	if updated := devflowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
 		t.Error("Failed to update flow")
 	} else {
 		t.Logf("Updated flows : %+v", updated)
@@ -276,7 +195,7 @@
 		t.Logf("Found flows (flows proxy): %s", string(djson))
 	}
 
-	if d := pt.Proxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
+	if d := modelTestConfig.RootProxy.Get("/devices/"+devId+"/flows", 0, false, ""); !reflect.ValueOf(d).IsValid() {
 		t.Error("Failed to find updated flows (root proxy)")
 	} else {
 		djson, _ := json.Marshal(d)
@@ -289,11 +208,11 @@
 	if !postUpdateExecuted {
 		t.Error("POST_UPDATE callback was not executed")
 	}
-	//
-	// Get a device proxy and update all its ports
-	//
 
-	//devProxy := pt.Root.GetProxy("/devices/"+devId, false)
+	//Get a device proxy and update all its ports
+
+
+	//devProxy := modelTestConfig.RootProxy.GetProxy("/devices/"+devId, false)
 	//ports := devProxy.Get("/ports", 0, false, "")
 	//t.Logf("got ports: %+v", ports)
 	//devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
@@ -308,7 +227,7 @@
 	// Get a device proxy, retrieve all the ports and update a specific one
 	//
 
-	//devProxy := pt.Root.GetProxy("/devices/"+devId, false)
+	//devProxy := modelTestConfig.RootProxy.GetProxy("/devices/"+devId, false)
 	//ports := devProxy.Get("/ports", 0, false, "")
 	//t.Logf("got ports: %+v", ports)
 	//devProxy.RegisterCallback(POST_UPDATE, commonCallback, nil)
@@ -321,26 +240,26 @@
 }
 
 func Test_Proxy_4_1_Remove_Device(t *testing.T) {
-	preRemoveExecuted = false
-	postRemoveExecuted = false
+	preRemoveExecuted := false
+	postRemoveExecuted := false
 
-	pt.Proxy.RegisterCallback(
+	modelTestConfig.RootProxy.RegisterCallback(
 		PRE_REMOVE,
 		commonCallback,
 		"PRE_REMOVE instructions (root proxy)", &preRemoveExecuted,
 	)
-	pt.Proxy.RegisterCallback(
+	modelTestConfig.RootProxy.RegisterCallback(
 		POST_REMOVE,
 		commonCallback,
 		"POST_REMOVE instructions (root proxy)", &postRemoveExecuted,
 	)
 
-	if removed := pt.Proxy.Remove("/devices/"+devId, ""); removed == nil {
+	if removed := modelTestConfig.RootProxy.Remove("/devices/"+devId, ""); removed == nil {
 		t.Error("Failed to remove device")
 	} else {
 		t.Logf("Removed device : %+v", removed)
 	}
-	if d := pt.Proxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
+	if d := modelTestConfig.RootProxy.Get("/devices/"+devId, 0, false, ""); reflect.ValueOf(d).IsValid() {
 		djson, _ := json.Marshal(d)
 		t.Errorf("Device was not removed - %s", djson)
 	} else {
@@ -359,56 +278,35 @@
 // Callback tests
 // -----------------------------
 
-func firstCallback(args ...interface{}) interface{} {
-	name := args[0]
-	id := args[1]
-	log.Infof("Running first callback - name: %s, id: %s\n", name, id)
-	return nil
-}
-func secondCallback(args ...interface{}) interface{} {
-	name := args[0].(map[string]string)
-	id := args[1]
-	log.Infof("Running second callback - name: %s, id: %f\n", name["name"], id)
-	// FIXME: the panic call seem to interfere with the logging mechanism
-	//panic("Generating a panic in second callback")
-	return nil
-}
-func thirdCallback(args ...interface{}) interface{} {
-	name := args[0]
-	id := args[1].(*voltha.Device)
-	log.Infof("Running third callback - name: %+v, id: %s\n", name, id.Id)
-	return nil
-}
-
 func Test_Proxy_Callbacks_1_Register(t *testing.T) {
-	pt.Proxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
+	modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, firstCallback, "abcde", "12345")
 
 	m := make(map[string]string)
 	m["name"] = "fghij"
-	pt.Proxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
+	modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, secondCallback, m, 1.2345)
 
 	d := &voltha.Device{Id: "12345"}
-	pt.Proxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
+	modelTestConfig.RootProxy.RegisterCallback(PRE_ADD, thirdCallback, "klmno", d)
 }
 
 func Test_Proxy_Callbacks_2_Invoke_WithNoInterruption(t *testing.T) {
-	pt.Proxy.InvokeCallbacks(PRE_ADD, false, nil)
+	modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, false, nil)
 }
 
 func Test_Proxy_Callbacks_3_Invoke_WithInterruption(t *testing.T) {
-	pt.Proxy.InvokeCallbacks(PRE_ADD, true, nil)
+	modelTestConfig.RootProxy.InvokeCallbacks(PRE_ADD, true, nil)
 }
 
 func Test_Proxy_Callbacks_4_Unregister(t *testing.T) {
-	pt.Proxy.UnregisterCallback(PRE_ADD, firstCallback)
-	pt.Proxy.UnregisterCallback(PRE_ADD, secondCallback)
-	pt.Proxy.UnregisterCallback(PRE_ADD, thirdCallback)
+	modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, firstCallback)
+	modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, secondCallback)
+	modelTestConfig.RootProxy.UnregisterCallback(PRE_ADD, thirdCallback)
 }
 
 //func Test_Proxy_Callbacks_5_Add(t *testing.T) {
-//	pt.Proxy.Root.AddCallback(pt.Proxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
+//	modelTestConfig.RootProxy.Root.AddCallback(modelTestConfig.RootProxy.InvokeCallbacks, POST_UPDATE, false, "some data", "some new data")
 //}
 //
 //func Test_Proxy_Callbacks_6_Execute(t *testing.T) {
-//	pt.Proxy.Root.ExecuteCallbacks()
+//	modelTestConfig.RootProxy.Root.ExecuteCallbacks()
 //}