[weave] Initialize the required submodules in weavestack

Weavestack requires a bunch of submodules to be initialized
like systemlayer, inetlayer, messagelayer etc inorder to
receive weave messages.

Bug: 46253
Test: Added unittest to verify weave stack initialization.
Change-Id: I4e2bdf4b83b85e62a4351e1b90dc20c2e3ec263e
diff --git a/src/connectivity/weave/BUILD.gn b/src/connectivity/weave/BUILD.gn
index 85c2b2d..415b1e4 100644
--- a/src/connectivity/weave/BUILD.gn
+++ b/src/connectivity/weave/BUILD.gn
@@ -9,5 +9,8 @@
 
 group("tests") {
   testonly = true
-  public_deps = [ "weavestack:tests" ]
+  public_deps = [
+    "adaptation/tests:tests",
+    "weavestack/tests:tests",
+  ]
 }
diff --git a/src/connectivity/weave/adaptation/BUILD.gn b/src/connectivity/weave/adaptation/BUILD.gn
index d380259..be6230d 100644
--- a/src/connectivity/weave/adaptation/BUILD.gn
+++ b/src/connectivity/weave/adaptation/BUILD.gn
@@ -32,6 +32,9 @@
   ]
   public_configs = [ ":adaptation_config" ]
   public_deps = [ "//third_party/openweave-core" ]
+  deps = [
+    "//src/lib/syslog/cpp",
+  ]
 }
 
 config("adaptation_config") {
diff --git a/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.h b/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.h
index 4422e34..09be883 100644
--- a/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.h
+++ b/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.h
@@ -5,6 +5,7 @@
 #define SRC_CONNECTIVITY_WEAVE_ADAPTATION_GENERIC_PLATFORM_MANAGER_IMPL_FUCHSIA_H_
 
 #include <Weave/DeviceLayer/internal/GenericPlatformManagerImpl.h>
+#include <mutex>
 
 namespace nl {
 namespace Weave {
@@ -39,6 +40,14 @@
   inline ImplClass* Impl() { return static_cast<ImplClass*>(this); }
 
   static void EventLoopTaskMain(void* arg);
+  std::mutex mEventLoopLock;
+
+  // Weave submodule instances.
+  nl::Weave::System::Layer system_layer_;
+  nl::Inet::InetLayer inet_layer_;
+  nl::Weave::WeaveFabricState fabric_state_;
+  nl::Weave::WeaveMessageLayer message_layer_;
+  nl::Weave::WeaveSecurityManager security_manager_;
 };
 
 // Instruct the compiler to instantiate the template only when explicitly told to do so.
diff --git a/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.ipp b/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.ipp
index 1090b03..f46725b 100644
--- a/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.ipp
+++ b/src/connectivity/weave/adaptation/generic_platform_manager_impl_fuchsia.ipp
@@ -4,6 +4,7 @@
 #ifndef GENERIC_PLATFORM_MANAGER_IMPL_FUCHSIA_IPP
 #define GENERIC_PLATFORM_MANAGER_IMPL_FUCHSIA_IPP
 
+// clang-format off
 #include <Weave/DeviceLayer/internal/WeaveDeviceLayerInternal.h>
 #include <Weave/DeviceLayer/PlatformManager.h>
 #include "generic_platform_manager_impl_fuchsia.h"
@@ -11,7 +12,9 @@
 // Include the non-inline definitions for the GenericPlatformManagerImpl<> template,
 // from which the GenericPlatformManagerImpl_Fuchsia<> template inherits.
 #include <Weave/DeviceLayer/internal/GenericPlatformManagerImpl.ipp>
+// clang-format on
 
+#include <lib/syslog/cpp/logger.h>
 
 namespace nl {
 namespace Weave {
@@ -22,12 +25,68 @@
 WEAVE_ERROR GenericPlatformManagerImpl_Fuchsia<ImplClass>::_InitWeaveStack(void)
 {
     WEAVE_ERROR err = WEAVE_NO_ERROR;
+    nl::Weave::WeaveMessageLayer::InitContext initContext;
 
-    // Call up to the base class _InitWeaveStack() to perform the bulk of the initialization.
-    err = GenericPlatformManagerImpl<ImplClass>::_InitWeaveStack();
-    SuccessOrExit(err);
+    err = system_layer_.Init(nullptr);
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "System layer init failed: " << ErrorStr(err);
+      return err;
+    }
 
-exit:
+    err = inet_layer_.Init(system_layer_, nullptr);
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "Inet layer init failed: " << ErrorStr(err);
+      return err;
+    }
+
+    err = fabric_state_.Init();
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "FabricState init failed: " << ErrorStr(err);
+      return err;
+    }
+
+    initContext.inet = &inet_layer_;
+    initContext.systemLayer = &system_layer_;
+    initContext.fabricState = &fabric_state_;
+    initContext.listenTCP = true;
+    initContext.listenUDP = true;
+
+    err = message_layer_.Init(&initContext);
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "Message layer init failed: "<< ErrorStr(err);
+      return err;
+    }
+
+    err = ExchangeMgr.Init(&message_layer_);
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "Exchange manager init failed: "<< ErrorStr(err);
+      return err;
+    }
+
+    err = security_manager_.Init(ExchangeMgr, system_layer_);
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "Security manager init failed: " << ErrorStr(err);
+      return err;
+    }
+
+    err = DeviceDescriptionSvr().Init();
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "device DeviceDescription init failed: "<< ErrorStr(err);
+      return err;
+    }
+
+    err = DeviceControlSvr().Init();
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "device control svr init failed: " << ErrorStr(err);
+      return err;
+    }
+
+    err = FabricProvisioningSvr().Init();
+    if (err != WEAVE_NO_ERROR) {
+      FX_LOGS(ERROR) << "FabricProvisioningSvr init failed: " << ErrorStr(err);
+      return err;
+    }
+
     return err;
 }
 
@@ -37,7 +96,7 @@
 template<class ImplClass>
 bool GenericPlatformManagerImpl_Fuchsia<ImplClass>::_TryLockWeaveStack(void)
 {
-    return true;
+  return true;
 }
 
 template<class ImplClass>
diff --git a/src/connectivity/weave/adaptation/platform_manager_impl.cpp b/src/connectivity/weave/adaptation/platform_manager_impl.cpp
index 32cce04..8caf778 100644
--- a/src/connectivity/weave/adaptation/platform_manager_impl.cpp
+++ b/src/connectivity/weave/adaptation/platform_manager_impl.cpp
@@ -15,7 +15,9 @@
 
 PlatformManagerImpl PlatformManagerImpl::sInstance;
 
-WEAVE_ERROR PlatformManagerImpl::_InitWeaveStack(void) { return WEAVE_NO_ERROR; }
+WEAVE_ERROR PlatformManagerImpl::_InitWeaveStack(void) {
+  return Internal::GenericPlatformManagerImpl_Fuchsia<PlatformManagerImpl>::_InitWeaveStack();
+}
 
 }  // namespace DeviceLayer
 }  // namespace Weave
diff --git a/src/connectivity/weave/adaptation/tests/BUILD.gn b/src/connectivity/weave/adaptation/tests/BUILD.gn
new file mode 100644
index 0000000..e42427c
--- /dev/null
+++ b/src/connectivity/weave/adaptation/tests/BUILD.gn
@@ -0,0 +1,42 @@
+# Copyright 2020 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config.gni")
+import("//build/test/test_package.gni")
+import("//build/testing/environments.gni")
+
+group("tests") {
+  testonly = true
+
+  public_deps = [
+    ":adaptation_tests"
+  ]
+}
+
+test_package("adaptation_tests") {
+  tests = [
+    {
+      name = "platform_manager_unittests"
+    },
+  ]
+  deps = [
+    ":platform_manager_unittests",
+  ]
+}
+
+executable("platform_manager_unittests") {
+  testonly = true
+  output_name = "platform_manager_unittests"
+  sources = [
+    "platform_manager_unittests.cpp",
+  ]
+  deps = [
+    "//sdk/lib/sys/cpp/testing:unit",
+    "//src/connectivity/weave/adaptation",
+    "//src/lib/fxl/test:gtest_main",
+    "//src/lib/testing/loop_fixture",
+    "//third_party/googletest:gtest",
+    "//third_party/googletest:gmock",
+  ]
+}
diff --git a/src/connectivity/weave/adaptation/tests/meta/platform_manager_unittests.cmx b/src/connectivity/weave/adaptation/tests/meta/platform_manager_unittests.cmx
new file mode 100644
index 0000000..6980899
--- /dev/null
+++ b/src/connectivity/weave/adaptation/tests/meta/platform_manager_unittests.cmx
@@ -0,0 +1,15 @@
+{
+    "facets": {
+        "fuchsia.test": {
+            "injected-services": {
+                "fuchsia.posix.socket.Provider": "fuchsia-pkg://fuchsia.com/netstack#meta/netstack.cmx"
+            }
+        }
+    },
+   "program": {
+        "binary": "test/platform_manager_unittests"
+    },
+    "sandbox": {
+        "services": [ "fuchsia.posix.socket.Provider" ]
+    }
+}
\ No newline at end of file
diff --git a/src/connectivity/weave/adaptation/tests/platform_manager_unittests.cpp b/src/connectivity/weave/adaptation/tests/platform_manager_unittests.cpp
new file mode 100644
index 0000000..51a5940
--- /dev/null
+++ b/src/connectivity/weave/adaptation/tests/platform_manager_unittests.cpp
@@ -0,0 +1,28 @@
+// Copyright 2020 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <lib/gtest/test_loop_fixture.h>
+
+#include <Weave/DeviceLayer/PlatformManager.h>
+
+#include "gtest/gtest.h"
+
+namespace adaptation {
+namespace testing {
+namespace {
+using nl::Weave::DeviceLayer::PlatformMgr;
+}  // namespace
+
+class PlatformManagerTest : public ::gtest::TestLoopFixture {
+ public:
+  PlatformManagerTest() {}
+  void SetUp() override { TestLoopFixture::SetUp(); }
+};
+
+TEST_F(PlatformManagerTest, InitWeaveStackTest) {
+  EXPECT_EQ(PlatformMgr().InitWeaveStack(), WEAVE_NO_ERROR);
+}
+
+}  // namespace testing
+}  // namespace adaptation
diff --git a/src/connectivity/weave/meta/weavestack.cmx b/src/connectivity/weave/meta/weavestack.cmx
index c84dfda..753d0e8 100644
--- a/src/connectivity/weave/meta/weavestack.cmx
+++ b/src/connectivity/weave/meta/weavestack.cmx
@@ -3,6 +3,8 @@
         "binary": "bin/weavestack"
     },
     "sandbox": {
-        "services": []
+        "services": [
+            "fuchsia.posix.socket.Provider"
+        ]
     }
 }
diff --git a/src/connectivity/weave/weavestack/BUILD.gn b/src/connectivity/weave/weavestack/BUILD.gn
index 11c5fbe..49a4789 100644
--- a/src/connectivity/weave/weavestack/BUILD.gn
+++ b/src/connectivity/weave/weavestack/BUILD.gn
@@ -14,9 +14,11 @@
 
   public_deps = [
     "//sdk/lib/sys/cpp",
+    "//src/lib/syslog/cpp",
     "//src/connectivity/weave/adaptation",
     "//zircon/public/lib/async-loop-cpp",
     "//zircon/public/lib/async-loop-default",
+    "//zircon/public/lib/zx",
   ]
 }
 
@@ -32,7 +34,6 @@
   testonly = true
   public_deps = [
     ":pkg",
-    ":tests",
   ]
 }
 
@@ -54,31 +55,3 @@
   ]
 }
 
-executable("unittests") {
-  output_name = "weavestack_unittests"
-
-  testonly = true
-
-  sources = [ "app_test.cc" ]
-
-  public_deps = [
-    ":lib",
-    "//src/lib/fxl/test:gtest_main",
-  ]
-}
-
-unittest_package("weavestack_unittests") {
-  deps = [ ":unittests" ]
-
-  tests = [
-    {
-      name = "weavestack_unittests"
-      environments = basic_envs
-    },
-  ]
-}
-
-group("tests") {
-  testonly = true
-  public_deps = [ ":weavestack_unittests" ]
-}
diff --git a/src/connectivity/weave/weavestack/app.cc b/src/connectivity/weave/weavestack/app.cc
index 6f53f95..74dabc0 100644
--- a/src/connectivity/weave/weavestack/app.cc
+++ b/src/connectivity/weave/weavestack/app.cc
@@ -4,6 +4,8 @@
 
 #include "src/connectivity/weave/weavestack/app.h"
 
+#include <lib/syslog/cpp/logger.h>
+
 #include <Weave/DeviceLayer/PlatformManager.h>
 
 namespace weavestack {
@@ -26,8 +28,13 @@
 void App::WeaveMain() {
   auto state = std::make_unique<WeaveState>();
 
-  PlatformMgr().InitWeaveStack();
-  // TODO: while (state.KeepRunning()) { do stuff }
+  WEAVE_ERROR err;
+
+  err = PlatformMgr().InitWeaveStack();
+  if (err != WEAVE_NO_ERROR) {
+    FX_LOGS(ERROR) << "InitWeaveStack() failed " << nl::ErrorStr(err);
+    return;
+  }
 }
 
 }  // namespace weavestack
diff --git a/src/connectivity/weave/weavestack/tests/BUILD.gn b/src/connectivity/weave/weavestack/tests/BUILD.gn
new file mode 100644
index 0000000..7c449ec
--- /dev/null
+++ b/src/connectivity/weave/weavestack/tests/BUILD.gn
@@ -0,0 +1,40 @@
+# Copyright 2020 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config.gni")
+import("//build/test/test_package.gni")
+import("//build/testing/environments.gni")
+
+group("tests") {
+  testonly = true
+  public_deps = [ ":weavestack_unittests" ]
+}
+
+test_package("weavestack_unittests") {
+  deps = [ ":unittests" ]
+
+  tests = [
+    {
+      name = "weavestack_unittests"
+      environments = basic_envs
+    },
+  ]
+}
+
+executable("unittests") {
+  output_name = "weavestack_unittests"
+
+  testonly = true
+
+  sources = [ "app_test.cc" ]
+
+  public_deps = [
+    "//src/connectivity/weave/weavestack:lib",
+    "//src/lib/fxl/test:gtest_main",
+    "//src/lib/syslog/cpp",
+    "//zircon/public/lib/async-loop-cpp",
+    "//zircon/public/lib/async-loop-default",
+    "//zircon/public/lib/zx",
+  ]
+}
diff --git a/src/connectivity/weave/weavestack/app_test.cc b/src/connectivity/weave/weavestack/tests/app_test.cc
similarity index 100%
rename from src/connectivity/weave/weavestack/app_test.cc
rename to src/connectivity/weave/weavestack/tests/app_test.cc
diff --git a/src/connectivity/weave/weavestack/tests/meta/weavestack_unittests.cmx b/src/connectivity/weave/weavestack/tests/meta/weavestack_unittests.cmx
new file mode 100644
index 0000000..73b4d58d
--- /dev/null
+++ b/src/connectivity/weave/weavestack/tests/meta/weavestack_unittests.cmx
@@ -0,0 +1,15 @@
+{
+    "facets": {
+        "fuchsia.test": {
+            "injected-services": {
+                "fuchsia.posix.socket.Provider": "fuchsia-pkg://fuchsia.com/netstack#meta/netstack.cmx"
+            }
+        }
+    },
+   "program": {
+        "binary": "test/weavestack_unittests"
+    },
+    "sandbox": {
+        "services": [ "fuchsia.posix.socket.Provider" ]
+    }
+}
\ No newline at end of file