• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/selector/group_selector.h"
18 #include "host/commands/cvd/selector/device_selector_utils.h"
19 #include "host/commands/cvd/selector/selector_constants.h"
20 
21 namespace cuttlefish {
22 namespace selector {
23 
GetSelector(const cvd_common::Args & selector_args,const Queries & extra_queries,const cvd_common::Envs & envs,const uid_t uid)24 Result<GroupSelector> GroupSelector::GetSelector(
25     const cvd_common::Args& selector_args, const Queries& extra_queries,
26     const cvd_common::Envs& envs, const uid_t uid) {
27   cvd_common::Args selector_args_copied{selector_args};
28   SelectorCommonParser common_parser =
29       CF_EXPECT(SelectorCommonParser::Parse(uid, selector_args_copied, envs));
30   std::stringstream unused_args;
31   unused_args << "{";
32   for (const auto& arg : selector_args_copied) {
33     unused_args << arg << ", ";
34   }
35   std::string unused_arg_list = unused_args.str();
36   if (!selector_args_copied.empty()) {
37     unused_arg_list.pop_back();
38     unused_arg_list.pop_back();
39   }
40   unused_arg_list.append("}");
41   if (!selector_args_copied.empty()) {
42     LOG(ERROR) << "Warning: there are unused selector options. "
43                << unused_arg_list;
44   }
45 
46   // search by group and instances
47   // search by HOME if overridden
48   Queries queries;
49   if (IsHomeOverridden(common_parser)) {
50     CF_EXPECT(common_parser.Home());
51     queries.emplace_back(kHomeField, common_parser.Home().value());
52   }
53   if (common_parser.GroupName()) {
54     queries.emplace_back(kGroupNameField, common_parser.GroupName().value());
55   }
56   if (common_parser.PerInstanceNames()) {
57     const auto per_instance_names = common_parser.PerInstanceNames().value();
58     for (const auto& per_instance_name : per_instance_names) {
59       queries.emplace_back(kInstanceNameField, per_instance_name);
60     }
61   }
62 
63   for (const auto& extra_query : extra_queries) {
64     queries.push_back(extra_query);
65   }
66 
67   GroupSelector group_selector(uid, queries);
68   return group_selector;
69 }
70 
IsHomeOverridden(const SelectorCommonParser & common_parser)71 bool GroupSelector::IsHomeOverridden(
72     const SelectorCommonParser& common_parser) {
73   auto home_overridden_result = common_parser.HomeOverridden();
74   if (!home_overridden_result.ok()) {
75     return false;
76   }
77   return *home_overridden_result;
78 }
79 
FindGroup(const InstanceDatabase & instance_database)80 Result<LocalInstanceGroup> GroupSelector::FindGroup(
81     const InstanceDatabase& instance_database) {
82   if (queries_.empty()) {
83     auto default_group = CF_EXPECT(FindDefaultGroup(instance_database));
84     return default_group;
85   }
86   auto groups = CF_EXPECT(instance_database.FindGroups(queries_));
87   CF_EXPECT(groups.size() == 1, "groups.size() = " << groups.size());
88   return *(groups.cbegin());
89 }
90 
FindDefaultGroup(const InstanceDatabase & instance_database)91 Result<LocalInstanceGroup> GroupSelector::FindDefaultGroup(
92     const InstanceDatabase& instance_database) {
93   auto group = CF_EXPECT(GetDefaultGroup(instance_database, client_uid_));
94   return group;
95 }
96 
97 }  // namespace selector
98 }  // namespace cuttlefish
99