1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/commands/cvd/parser/fetch_cvd_parser.h"
18
19 #include <android-base/file.h>
20 #include <gflags/gflags.h>
21
22 #include <stdio.h>
23 #include <fstream>
24 #include <string>
25
26 #include "common/libs/utils/files.h"
27 #include "common/libs/utils/json.h"
28 #include "host/commands/assemble_cvd/flags_defaults.h"
29 #include "host/commands/cvd/parser/cf_configs_common.h"
30
31 namespace cuttlefish {
32
33 #define EMPTY_CREDENTIAL ""
34 #define EMPTY_DEFAULT_BUILD ""
35 #define EMPTY_SYSTEM_BUILD ""
36 #define EMPTY_KERNEL_BUILD ""
37
InitFetchInstanceConfigs(Json::Value & instances)38 void InitFetchInstanceConfigs(Json::Value& instances) {
39 // Handle common flags
40 InitStringConfig(instances, "disk", "default_build", EMPTY_DEFAULT_BUILD);
41 InitStringConfig(instances, "disk", "system_build", EMPTY_SYSTEM_BUILD);
42 InitStringConfig(instances, "disk", "kernel_build", EMPTY_KERNEL_BUILD);
43 }
44
InitFetchCvdConfigs(Json::Value & root)45 void InitFetchCvdConfigs(Json::Value& root) {
46 if (!root.isMember("credential")) {
47 root["credential"] = EMPTY_CREDENTIAL;
48 }
49 InitFetchInstanceConfigs(root["instances"]);
50 }
51
ParseFetchInstanceConfigs(const Json::Value & instance)52 FetchCvdDeviceConfigs ParseFetchInstanceConfigs(const Json::Value& instance) {
53 FetchCvdDeviceConfigs result;
54 result.default_build = instance["disk"]["default_build"].asString();
55 result.system_build = instance["disk"]["system_build"].asString();
56 result.kernel_build = instance["disk"]["kernel_build"].asString();
57 if (result.default_build != EMPTY_DEFAULT_BUILD ||
58 result.system_build != EMPTY_SYSTEM_BUILD ||
59 result.kernel_build != EMPTY_KERNEL_BUILD) {
60 result.use_fetch_artifact = true;
61 } else {
62 result.use_fetch_artifact = false;
63 }
64
65 return result;
66 }
67
GenerateFetchCvdFlags(const Json::Value & root)68 FetchCvdConfigs GenerateFetchCvdFlags(const Json::Value& root) {
69 FetchCvdConfigs result;
70 result.credential = root["credential"].asString();
71 int num_instances = root["instances"].size();
72 for (unsigned int i = 0; i < num_instances; i++) {
73 auto instance_config = ParseFetchInstanceConfigs(root["instances"][i]);
74 result.instances.emplace_back(instance_config);
75 }
76
77 return result;
78 }
79
ParseFetchCvdConfigs(Json::Value & root)80 FetchCvdConfigs ParseFetchCvdConfigs(Json::Value& root) {
81 InitFetchCvdConfigs(root);
82 return GenerateFetchCvdFlags(root);
83 }
84
85 } // namespace cuttlefish
86