refactor statspuller

Test: manual test on device
Change-Id: Ibdec6a821e47cd2b2e7435002219c0b2e3f4c5d2
diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk
index 56f9512..1005b56 100644
--- a/cmds/statsd/Android.mk
+++ b/cmds/statsd/Android.mk
@@ -44,7 +44,6 @@
     ../../core/java/android/os/IStatsManager.aidl \
     src/StatsService.cpp \
     src/AnomalyMonitor.cpp \
-    src/StatsPuller.cpp \
     src/LogEntryPrinter.cpp \
     src/LogReader.cpp \
     src/main.cpp \
@@ -53,6 +52,8 @@
     src/StatsLogProcessor.cpp \
     src/stats_log.proto \
     src/statsd_config.proto \
+    src/StatsPullerManager.cpp \
+    src/KernelWakelockPuller.cpp \
     src/DropboxReader.cpp \
     src/matchers/LogEntryMatcherManager.cpp \
     src/metrics/CountMetricProducer.cpp \
diff --git a/cmds/statsd/src/KernelWakelockPuller.cpp b/cmds/statsd/src/KernelWakelockPuller.cpp
new file mode 100644
index 0000000..1798f9d
--- /dev/null
+++ b/cmds/statsd/src/KernelWakelockPuller.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#include "KernelWakelockPuller.h"
+#include <android/os/IStatsCompanionService.h>
+#include <binder/IPCThreadState.h>
+#include <cutils/log.h>
+#include <private/android_filesystem_config.h>
+#include "StatsPuller.h"
+#include "StatsService.h"
+
+using namespace android;
+using namespace android::base;
+using namespace android::binder;
+using namespace android::os;
+using namespace std;
+
+namespace android {
+namespace os {
+namespace statsd {
+
+const int KernelWakelockPuller::PULL_CODE_KERNEL_WAKELOCKS = 20;
+
+// The reading and parsing are implemented in Java. It is not difficult to port over. But for now
+// let StatsCompanionService handle that and send the data back.
+String16 KernelWakelockPuller::pull() {
+    sp<IStatsCompanionService> statsCompanion = StatsService::getStatsCompanionService();
+    String16 returned_value("");
+    if (statsCompanion != NULL) {
+      Status status = statsCompanion->pullData(KernelWakelockPuller::PULL_CODE_KERNEL_WAKELOCKS,
+                                             &returned_value);
+      if (!status.isOk()) {
+          ALOGW("error pulling kernel wakelock");
+      }
+      ALOGD("KernelWakelockPuller::pull succeeded!");
+      // TODO: remove this when we integrate into aggregation chain.
+      ALOGD("%s", String8(returned_value).string());
+      return returned_value;
+    } else {
+        ALOGW("statsCompanion not found!");
+        return String16();
+    }
+}
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
diff --git a/cmds/statsd/src/KernelWakelockPuller.h b/cmds/statsd/src/KernelWakelockPuller.h
new file mode 100644
index 0000000..1c16f87
--- /dev/null
+++ b/cmds/statsd/src/KernelWakelockPuller.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef STATSD_KERNELWAKELOCKPULLER_H
+#define STATSD_KERNELWAKELOCKPULLER_H
+
+#include <utils/String16.h>
+#include "StatsPuller.h"
+
+namespace android {
+namespace os {
+namespace statsd {
+
+class KernelWakelockPuller : public StatsPuller {
+public:
+    // a number of stats need to be pulled from StatsCompanionService
+    //
+    const static int PULL_CODE_KERNEL_WAKELOCKS;
+    String16 pull() override;
+};
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
+
+#endif  // STATSD_KERNELWAKELOCKPULLER_H
diff --git a/cmds/statsd/src/StatsPuller.cpp b/cmds/statsd/src/StatsPuller.cpp
deleted file mode 100644
index 94e8361..0000000
--- a/cmds/statsd/src/StatsPuller.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * 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.
- */
-
-#define LOG_TAG "StatsPuller"
-#define DEBUG true
-
-#include "StatsPuller.h"
-#include "StatsService.h"
-#include <android/os/IStatsCompanionService.h>
-#include <cutils/log.h>
-
-using namespace android;
-
-namespace android {
-namespace os {
-namespace statsd {
-
-String16 StatsPuller::pull(int pullCode) {
-    if (DEBUG) ALOGD("Initiating pulling %d", pullCode);
-
-    switch (pullCode) {
-        // All stats_companion_service cases go here with fallthroughs
-        case PULL_CODE_KERNEL_WAKELOCKS: {
-            // TODO: Consider caching the statsCompanion service
-            sp <IStatsCompanionService>
-                    statsCompanion = StatsService::getStatsCompanionService();
-            String16 returned_value("");
-            Status status = statsCompanion->pullData(pullCode, &returned_value);
-            if (DEBUG) ALOGD("Finished pulling the data");
-            if (!status.isOk()) {
-                ALOGW("error pulling data of type %d", pullCode);
-            }
-            return returned_value;
-        }
-
-        // case OTHER_TYPES: etc.
-
-        default: {
-            ALOGE("invalid pull code %d", pullCode);
-            return String16("");
-        }
-    }
-}
-
-}  // namespace statsd
-}  // namespace os
-}  // namespace android
diff --git a/cmds/statsd/src/StatsPuller.h b/cmds/statsd/src/StatsPuller.h
index 05343b5..5e556b8 100644
--- a/cmds/statsd/src/StatsPuller.h
+++ b/cmds/statsd/src/StatsPuller.h
@@ -25,19 +25,13 @@
 
 class StatsPuller {
 public:
-    // Enums of pulled data types (pullCodes)
-    // These values must be kept in sync with com/android/server/stats/StatsCompanionService.java.
-    // TODO: pull the constant from stats_events.proto instead
-    const static int PULL_CODE_KERNEL_WAKELOCKS = 20;
-
-    StatsPuller();
-    ~StatsPuller();
-
-    static String16 pull(int pullCode);
+    virtual ~StatsPuller(){};
+    // use string for now, until we figure out how to integrate into the aggregation path
+    virtual String16 pull() = 0;
 };
 
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
 
-#endif //STATSD_STATSPULLER_H
+#endif  // STATSD_STATSPULLER_H
diff --git a/cmds/statsd/src/StatsPullerManager.cpp b/cmds/statsd/src/StatsPullerManager.cpp
new file mode 100644
index 0000000..f4cf1ce
--- /dev/null
+++ b/cmds/statsd/src/StatsPullerManager.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#define LOG_TAG "StatsPullerManager"
+#define DEBUG true
+
+#include "StatsPullerManager.h"
+#include <android/os/IStatsCompanionService.h>
+#include <cutils/log.h>
+#include "StatsService.h"
+#include "KernelWakelockPuller.h"
+
+
+using namespace android;
+
+namespace android {
+namespace os {
+namespace statsd {
+
+const int StatsPullerManager::KERNEL_WAKELOCKS = 1;
+
+StatsPullerManager::StatsPullerManager() {
+    mStatsPullers.insert(
+            {static_cast<int>(KERNEL_WAKELOCKS), std::make_unique<KernelWakelockPuller>()});
+}
+
+String16 StatsPullerManager::pull(int pullCode) {
+    if (DEBUG) ALOGD("Initiating pulling %d", pullCode);
+    if (mStatsPullers.find(pullCode) != mStatsPullers.end()) {
+        return (mStatsPullers.find(pullCode)->second)->pull();
+    } else {
+        ALOGD("Unknown pull code %d", pullCode);
+        return String16();
+    }
+}
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
diff --git a/cmds/statsd/src/StatsPullerManager.h b/cmds/statsd/src/StatsPullerManager.h
new file mode 100644
index 0000000..ab36df5
--- /dev/null
+++ b/cmds/statsd/src/StatsPullerManager.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef STATSD_STATSPULLERMANAGER_H
+#define STATSD_STATSPULLERMANAGER_H
+
+#include <utils/String16.h>
+#include <unordered_map>
+#include "StatsPuller.h"
+
+namespace android {
+namespace os {
+namespace statsd {
+
+const static int KERNEL_WAKELOCKS = 1;
+
+class StatsPullerManager {
+public:
+    // Enums of pulled data types (pullCodes)
+    // These values must be kept in sync with com/android/server/stats/StatsCompanionService.java.
+    // TODO: pull the constant from stats_events.proto instead
+    const static int KERNEL_WAKELOCKS;
+    StatsPullerManager();
+
+    String16 pull(const int pullCode);
+
+private:
+    std::unordered_map<int, std::unique_ptr<StatsPuller>> mStatsPullers;
+};
+
+
+}  // namespace statsd
+}  // namespace os
+}  // namespace android
+
+#endif  // STATSD_STATSPULLERMANAGER_H
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index ae7d66b..9baeebb 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -40,7 +40,7 @@
 namespace statsd {
 
 StatsService::StatsService(const sp<Looper>& handlerLooper)
-    : mAnomalyMonitor(new AnomalyMonitor(2))  // TODO: Change this based on the config
+    : mAnomalyMonitor(new AnomalyMonitor(2)), mStatsPullerManager()  // TODO: Change this based on the config
 {
     ALOGD("stats service constructed");
 }
@@ -177,9 +177,10 @@
 
     if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded");
     // TODO: determine what services to poll and poll (or ask StatsCompanionService to poll) them.
-    String16 output = StatsPuller::pull(StatsPuller::PULL_CODE_KERNEL_WAKELOCKS);
+    String16 output = mStatsPullerManager.pull(StatsPullerManager::KERNEL_WAKELOCKS);
     // TODO: do something useful with the output instead of writing a string to screen.
     ALOGD("%s", String8(output).string());
+    ALOGD("%d", int(output.size()));
 
     return Status::ok();
 }
diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h
index a16b115..9642279 100644
--- a/cmds/statsd/src/StatsService.h
+++ b/cmds/statsd/src/StatsService.h
@@ -19,7 +19,7 @@
 
 #include "AnomalyMonitor.h"
 #include "StatsLogProcessor.h"
-#include "StatsPuller.h"
+#include "StatsPullerManager.h"
 
 #include <android/os/BnStatsManager.h>
 #include <android/os/IStatsCompanionService.h>
@@ -74,7 +74,7 @@
     /** Fetches and returns the StatsCompanionService. */
     static sp<IStatsCompanionService> getStatsCompanionService();
 
- private:
+private:
     sp<StatsLogProcessor> m_processor;  // Reference to the processor for updating configs.
 
     status_t doPrintStatsLog(FILE* out, const Vector<String8>& args);
@@ -82,6 +82,8 @@
     void printCmdHelp(FILE* out);
 
     status_t doLoadConfig(FILE* in);
+
+    StatsPullerManager mStatsPullerManager;
 };
 
 // --- StatsdDeathRecipient ---