Add new proto database and tool to manage it

This change adds the schema of the new extension database and the
skeleton of a tool to manage it.

Each extension version maps to set of module version requirements,
and a text representation of this proto database is checked in
(currently empty). The tool is currently only able to convert the
database to its binary representation (which will be used on-device to
derive what extension we have currently). Also add a test for the
tool/binary generation.

The tool will be extended in future CLs to be able to add/update/remove
SDKs, as well performing various consistency checks on the DB.

Bug: 173188089
Test: atest gen_sdk_test
Change-Id: I918170a851719bcc5bc70568f4705629b500735b
diff --git a/gen_sdk/Android.bp b/gen_sdk/Android.bp
new file mode 100644
index 0000000..b9ac715
--- /dev/null
+++ b/gen_sdk/Android.bp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+python_binary_host {
+    name: "gen_sdk",
+    srcs: ["gen_sdk.py"],
+    libs: ["sdk_proto_python"],
+    version: {
+        py3: {
+            embedded_launcher: true,
+        },
+    },
+}
+
+sh_test_host {
+    name: "gen_sdk_test",
+    src: "gen_sdk_test.sh",
+    data: ["testdata/**/*"],
+    required: ["gen_sdk"],
+    test_suites: ["general-tests"],
+}
+
+genrule {
+    name: "extensions_db.binarypb",
+    srcs: ["extensions_db.textpb"],
+    out: ["extensions_db.binarypb"],
+    tools: ["gen_sdk"],
+    cmd: "$(location gen_sdk) --action print_binary --database $(location extensions_db.textpb) > $(out)",
+}
diff --git a/gen_sdk/extensions_db.textpb b/gen_sdk/extensions_db.textpb
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gen_sdk/extensions_db.textpb
diff --git a/gen_sdk/gen_sdk.py b/gen_sdk/gen_sdk.py
new file mode 100644
index 0000000..588d30f
--- /dev/null
+++ b/gen_sdk/gen_sdk.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2021 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.
+"""gen_sdk is a command line tool for managing the sdk extension proto db.
+
+Example usages:
+# Print a binary representation of the proto database.
+$ gen_sdk --action print_binary
+"""
+
+import argparse
+import google.protobuf.text_format
+import pathlib
+import sys
+
+from sdk_pb2 import ExtensionVersion
+from sdk_pb2 import ExtensionDatabase
+from sdk_pb2 import SdkModule
+from sdk_pb2 import SdkVersion
+
+
+def ParseArgs(argv):
+  parser = argparse.ArgumentParser('Manage the extension SDK database')
+  parser.add_argument(
+    '--database',
+    type=pathlib.Path,
+    metavar='PATH',
+    default='extensions_db.textpb',
+    help='The existing text-proto database to use. (default: extensions_db.textpb)'
+  )
+  parser.add_argument(
+    '--action',
+    choices=['print_binary'],
+    metavar='ACTION',
+    required=True,
+    help='Which action to take (print_binary).'
+  )
+  return parser.parse_args(argv)
+
+
+"""Print the binary representation of the db proto to stdout."""
+def PrintBinary(database):
+  sys.stdout.buffer.write(database.SerializeToString())
+
+
+def main(argv):
+  args = ParseArgs(argv)
+  with args.database.open('r') as f:
+    database = google.protobuf.text_format.Parse(f.read(), ExtensionDatabase())
+
+  {
+    'print_binary': lambda : PrintBinary(database),
+  }[args.action]()
+
+
+if __name__ == '__main__':
+  main(sys.argv[1:])
diff --git a/gen_sdk/gen_sdk_test.sh b/gen_sdk/gen_sdk_test.sh
new file mode 100644
index 0000000..adb76e0
--- /dev/null
+++ b/gen_sdk/gen_sdk_test.sh
@@ -0,0 +1,32 @@
+#!/bin/bash -e
+#
+# Copyright (C) 2021 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.
+
+# Verifies the tool correctly prints the binary representation of a db.
+function test_print_binary() {
+  # Golden binary rep generated with:
+  # $ gqui from textproto:testdata/test_extensions_db.textpb \
+  #        proto extensions_db.proto:ExtensionDatabase \
+  #        --outfile rawproto:- | xxd -p
+  cat > golden_binary << EOF
+0a0a080112060803120208010a1a08021206080312020801120608021202
+080212060801120208020a1a080312060803120208031206080212020802
+12060801120208020a220804120608031202080312060802120208021206
+0801120208041206080512020804
+EOF
+
+  diff golden_binary <(gen_sdk --action print_binary --database testdata/test_extensions_db.textpb | xxd -p)
+}
+test_print_binary
diff --git a/gen_sdk/testdata/test_extensions_db.textpb b/gen_sdk/testdata/test_extensions_db.textpb
new file mode 100644
index 0000000..63163b0
--- /dev/null
+++ b/gen_sdk/testdata/test_extensions_db.textpb
@@ -0,0 +1,78 @@
+versions {
+  version: 1
+  requirements {
+    module: MEDIA_PROVIDER
+    version {
+      version: 1
+    }
+  }
+}
+versions {
+  version: 2
+  requirements {
+    module: MEDIA_PROVIDER
+    version {
+      version: 1
+    }
+  }
+  requirements {
+    module: MEDIA
+    version {
+      version: 2
+    }
+  }
+  requirements {
+    module: IPSEC
+    version {
+      version: 2
+    }
+  }
+}
+versions {
+  version: 3
+  requirements {
+    module: MEDIA_PROVIDER
+    version {
+      version: 3
+    }
+  }
+  requirements {
+    module: MEDIA
+    version {
+      version: 2
+    }
+  }
+  requirements {
+    module: IPSEC
+    version {
+      version: 2
+    }
+  }
+}
+versions {
+  version: 4
+  requirements {
+    module: MEDIA_PROVIDER
+    version {
+      version: 3
+    }
+  }
+  requirements {
+    module: MEDIA
+    version {
+      version: 2
+    }
+  }
+  requirements {
+    module: IPSEC
+    version {
+      version: 4
+    }
+  }
+  requirements {
+    module: SDK_EXTENSIONS
+    version {
+      version: 4
+    }
+  }
+}