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 <optional> 20 #include <string> 21 22 #include "common/libs/utils/result.h" 23 24 namespace cuttlefish { 25 namespace selector { 26 27 class LocalInstanceGroup; 28 29 /** 30 * TODO(kwstephenkim): add more methods, fields, and abstract out Instance 31 * 32 * Needs design changes to support both Remote and Local Instances 33 */ 34 class LocalInstance { 35 friend class LocalInstanceGroup; 36 friend class InstanceDatabase; 37 38 public: 39 /* names: 40 * 41 * Many components in Cuttlefish traditionally expect the name to be "cvd-N," 42 * and rely on "N" to avoid conflicts in the global resource uses. 43 * 44 * Thus, we will eventually maintain the internal device name for those 45 * existing cuttlefish implementation, and the user-given name. 46 * 47 */ 48 const std::string& InternalName() const; 49 std::string InternalDeviceName() const; 50 51 unsigned InstanceId() const; 52 const std::string& PerInstanceName() const; 53 std::string DeviceName() const; 54 55 const LocalInstanceGroup& ParentGroup() const; 56 57 class Copy { 58 friend class LocalInstance; 59 struct MockParentParam { 60 std::string home_dir; 61 std::string host_artifacts_path; 62 std::string internal_group_name; 63 std::string group_name; 64 std::optional<std::string> build_id; 65 }; 66 67 public: 68 /* when Copy is used, it is already disconnected from the original parent 69 * group. Thus, it should carry the snapshot of needed information about 70 * the parent group 71 */ 72 class MockParent { 73 public: 74 MockParent(const MockParentParam&); InternalGroupName()75 const std::string& InternalGroupName() const { 76 return internal_group_name_; 77 } GroupName()78 const std::string& GroupName() const { return group_name_; } HomeDir()79 const std::string& HomeDir() const { return home_dir_; } HostArtifactsPath()80 const std::string& HostArtifactsPath() const { 81 return host_artifacts_path_; 82 } BuildId()83 const std::optional<std::string>& BuildId() const { return build_id_; } 84 85 private: 86 std::string home_dir_; 87 std::string host_artifacts_path_; 88 std::string internal_group_name_; 89 std::string group_name_; 90 std::optional<std::string> build_id_; 91 }; 92 Copy(const LocalInstance& src); InternalName()93 const std::string& InternalName() const { return internal_name_; } InternalDeviceName()94 const std::string& InternalDeviceName() const { 95 return internal_device_name_; 96 } InstanceId()97 unsigned InstanceId() const { return instance_id_; } PerInstanceName()98 const std::string& PerInstanceName() const { return per_instance_name_; } DeviceName()99 const std::string& DeviceName() const { return device_name_; } ParentGroup()100 const MockParent& ParentGroup() const { return mock_group_; } 101 102 private: 103 std::string internal_name_; 104 std::string internal_device_name_; 105 unsigned instance_id_; 106 std::string per_instance_name_; 107 std::string device_name_; 108 MockParent mock_group_; 109 }; 110 Copy GetCopy() const; 111 112 private: 113 LocalInstance(const LocalInstanceGroup& parent_group, 114 const unsigned instance_id, const std::string& instance_name); 115 116 static constexpr const char kJsonInstanceId[] = "Instance Id"; 117 static constexpr const char kJsonInstanceName[] = "Per-Instance Name"; 118 119 const LocalInstanceGroup& parent_group_; 120 unsigned instance_id_; 121 std::string internal_name_; ///< for now, it is to_string(instance_id_) 122 /** the instance specific name to be appended to the group name 123 * 124 * by default, to_string(instance_id_). The default value is decided by 125 * InstanceGroupRecord, as that's the only class that will create this 126 * instance 127 */ 128 std::string per_instance_name_; 129 }; 130 131 } // namespace selector 132 } // namespace cuttlefish 133