1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <memory> 20 #include <string> 21 22 #include <gtest/gtest.h> 23 24 #include "common/libs/utils/json.h" 25 #include "common/libs/utils/result.h" 26 #include "host/commands/cvd/selector/constant_reference.h" 27 #include "host/commands/cvd/selector/instance_database_types.h" 28 #include "host/commands/cvd/selector/instance_record.h" 29 30 namespace cuttlefish { 31 namespace selector { 32 33 class InstanceDatabase; 34 35 /** 36 * TODO(kwstephenkim): add more methods, fields, and abstract out Instance 37 * 38 * Needs design changes to support both Remote Instances 39 */ 40 class LocalInstanceGroup { 41 friend InstanceDatabase; 42 43 public: 44 LocalInstanceGroup(const LocalInstanceGroup& src); 45 LocalInstanceGroup& operator=(const LocalInstanceGroup& src); 46 InternalGroupName()47 const std::string& InternalGroupName() const { return internal_group_name_; } GroupName()48 const std::string& GroupName() const { return group_name_; } HomeDir()49 const std::string& HomeDir() const { return home_dir_; } HostArtifactsPath()50 const std::string& HostArtifactsPath() const { return host_artifacts_path_; } ProductOutPath()51 const std::string& ProductOutPath() const { return product_out_path_; } BuildId()52 const std::optional<std::string>& BuildId() const { return build_id_; } 53 Result<std::string> GetCuttlefishConfigPath() const; Instances()54 const Set<std::unique_ptr<LocalInstance>>& Instances() const { 55 return instances_; 56 } 57 Json::Value Serialize() const; 58 59 /** 60 * return error if instance id of instance is taken AND that taken id 61 * belongs to this group 62 */ 63 Result<void> AddInstance(const unsigned instance_id, 64 const std::string& instance_name); 65 bool HasInstance(const unsigned instance_id) const; 66 void SetBuildId(const std::string& build_id); 67 Result<Set<ConstRef<LocalInstance>>> FindById(const unsigned id) const; 68 /** 69 * Find by per-instance name. 70 * 71 * If the device name is cvd-foo or cvd-4, "cvd" is the group name, 72 * "foo" or "4" is the per-instance names, and "cvd-foo" or "cvd-4" is 73 * the device name. 74 */ 75 Result<Set<ConstRef<LocalInstance>>> FindByInstanceName( 76 const std::string& instance_name) const; 77 78 // returns all instances in the dedicated data type 79 Result<Set<ConstRef<LocalInstance>>> FindAllInstances() const; 80 81 private: 82 struct InstanceGroupParam { 83 std::string group_name; 84 std::string home_dir; 85 std::string host_artifacts_path; 86 std::string product_out_path; 87 }; 88 LocalInstanceGroup(const InstanceGroupParam& param); 89 // Eventually copies the instances of a src to *this 90 Set<std::unique_ptr<LocalInstance>> CopyInstances( 91 const Set<std::unique_ptr<LocalInstance>>& src_instances); 92 Json::Value Serialize(const std::unique_ptr<LocalInstance>& instance) const; 93 94 std::string home_dir_; 95 std::string host_artifacts_path_; 96 std::string product_out_path_; 97 98 // for now, "cvd", which is "cvd-".remove_suffix(1) 99 std::string internal_group_name_; 100 std::string group_name_; 101 // This will be initialized after the LocalInstanceGroup is created, 102 // which is also after the device completes the boot. 103 std::optional<std::string> build_id_; 104 Set<std::unique_ptr<LocalInstance>> instances_; 105 106 static constexpr const char kJsonGroupName[] = "Group Name"; 107 static constexpr const char kJsonHomeDir[] = "Runtime/Home Dir"; 108 static constexpr const char kJsonHostArtifactPath[] = "Host Tools Dir"; 109 static constexpr const char kJsonProductOutPath[] = "Product Out Dir"; 110 static constexpr const char kJsonInstances[] = "Instances"; 111 static constexpr const char kJsonParent[] = "Parent Group"; 112 static constexpr const char kJsonBuildId[] = "Build Id"; 113 static constexpr const char kJsonUnknownBuildId[] = "Unknown Build"; 114 115 /* 116 * Expose constructor to the tests in InstanceRecord unit test suite. 117 * 118 * To create InstanceRecords, we should create InstanceGroup first. 119 */ 120 FRIEND_TEST(CvdInstanceRecordUnitTest, Fields); 121 FRIEND_TEST(CvdInstanceRecordUnitTest, Copy); 122 123 friend class CvdInstanceGroupUnitTest; 124 friend class CvdInstanceGroupSearchUnitTest; 125 }; 126 127 } // namespace selector 128 } // namespace cuttlefish 129