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/base_test.go b/db/model/base_test.go
new file mode 100644
index 0000000..1be34f6
--- /dev/null
+++ b/db/model/base_test.go
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package model
+
+import (
+	"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"
+)
+
+type ModelTestConfig struct {
+	Root      *root
+	Backend   *Backend
+	RootProxy *Proxy
+	DbPrefix  string
+	DbType    string
+	DbHost    string
+	DbPort    int
+	DbTimeout int
+}
+
+var (
+	modelTestConfig = &ModelTestConfig{
+		DbPrefix:  "service/voltha/data/core/0001",
+		DbType:    "etcd",
+		DbHost:    "localhost",
+		//DbHost:    "10.106.153.44",
+		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
+)
+
+func init() {
+	log.AddPackage(log.JSON, log.WarnLevel, nil)
+	log.UpdateAllLoggers(log.Fields{"instanceId": "MODEL_TEST"})
+
+	defer log.CleanUp()
+
+	modelTestConfig.Backend = NewBackend(
+		modelTestConfig.DbType,
+		modelTestConfig.DbHost,
+		modelTestConfig.DbPort,
+		modelTestConfig.DbTimeout,
+		modelTestConfig.DbPrefix,
+	)
+
+	msgClass := &voltha.Voltha{}
+	root := NewRoot(msgClass, modelTestConfig.Backend)
+
+	if modelTestConfig.Backend != nil {
+		modelTestConfig.Root = root.Load(msgClass)
+	} else {
+		modelTestConfig.Root = root
+	}
+
+	GetProfiling().Report()
+
+	modelTestConfig.RootProxy = modelTestConfig.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 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
+}