1 /* 2 * Copyright (C) 2016 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 package com.android.tradefed.host; 18 19 import com.android.tradefed.config.ConfigurationException; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.OptionClass; 22 import com.android.tradefed.log.LogUtil.CLog; 23 24 import java.io.File; 25 import java.net.InetAddress; 26 import java.net.NetworkInterface; 27 import java.net.SocketException; 28 import java.util.ArrayList; 29 import java.util.Enumeration; 30 import java.util.HashMap; 31 import java.util.HashSet; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Set; 35 36 /** 37 * Host options holder class. 38 * This class is used to store host-wide options. 39 */ 40 @OptionClass(alias = "host_options", global_namespace = false) 41 public class HostOptions implements IHostOptions { 42 43 @Option(name = "concurrent-flasher-limit", description = 44 "The maximum number of concurrent flashers (may be useful to avoid memory constraints)") 45 private Integer mConcurrentFlasherLimit = 1; 46 47 @Option( 48 name = "concurrent-download-limit", 49 description = 50 "The maximum number of concurrent downloads (may be useful to avoid network " 51 + "constraints)" 52 ) 53 private Integer mConcurrentDownloadLimit = null; 54 55 @Option( 56 name = "fastboot-tmpdir", 57 description = "The location of temporary directory used by fastboot" 58 ) 59 private File mFastbootTmpDir = null; 60 61 @Option( 62 name = "enable-fastbootd-mode", 63 description = "Feature flag to enable the support for fastbootd.") 64 private boolean mEnableFastbootdMode = false; 65 66 @Option(name = "download-cache-dir", description = "the directory for caching downloaded " 67 + "flashing files. Should be on the same filesystem as java.io.tmpdir. Consider " 68 + "changing the java.io.tmpdir property if you want to move downloads to a different " 69 + "filesystem.") 70 private File mDownloadCacheDir = new File(System.getProperty("java.io.tmpdir"), "lc_cache"); 71 72 @Option(name = "use-sso-client", description = "Use a SingleSignOn client for HTTP requests.") 73 private Boolean mUseSsoClient = true; 74 75 @Option( 76 name = "service-account-json-key-file", 77 description = 78 "Specify a service account json key file, and a String key name to identify it." 79 ) 80 private Map<String, File> mJsonServiceAccountMap = new HashMap<>(); 81 82 @Option(name = "label", description = "Labels to describe the host.") 83 private List<String> mLabels = new ArrayList<>(); 84 85 @Option( 86 name = "known-tcp-device-ip-pool", 87 description = 88 "known remote device available via ip associated with the " 89 + "tcp-device placeholder.") 90 private Set<String> mKnownTcpDeviceIpPool = new HashSet<>(); 91 92 @Option( 93 name = "known-gce-device-ip-pool", 94 description = 95 "known remote device available via ip associated with the " 96 + "gce-device placeholder.") 97 private Set<String> mKnownGceDeviceIpPool = new HashSet<>(); 98 99 @Option( 100 name = "use-zip64-in-partial-download", 101 description = "Whether to use zip64 format in partial download.") 102 private boolean mUseZip64InPartialDownload = false; 103 104 @Option( 105 name = "use-network-interface", 106 description = "The network interface used to connect to test devices.") 107 private String mNetworkInterface = null; 108 109 /** {@inheritDoc} */ 110 @Override getConcurrentFlasherLimit()111 public Integer getConcurrentFlasherLimit() { 112 return mConcurrentFlasherLimit; 113 } 114 115 /** {@inheritDoc} */ 116 @Override getConcurrentDownloadLimit()117 public Integer getConcurrentDownloadLimit() { 118 return mConcurrentDownloadLimit; 119 } 120 121 /** {@inheritDoc} */ 122 @Override getFastbootTmpDir()123 public File getFastbootTmpDir() { 124 return mFastbootTmpDir; 125 } 126 127 /** {@inheritDoc} */ 128 @Override isFastbootdEnable()129 public boolean isFastbootdEnable() { 130 return mEnableFastbootdMode; 131 } 132 133 /** {@inheritDoc} */ 134 @Override getDownloadCacheDir()135 public File getDownloadCacheDir() { 136 return mDownloadCacheDir; 137 } 138 139 /** {@inheritDoc} */ 140 @Override shouldUseSsoClient()141 public Boolean shouldUseSsoClient() { 142 return mUseSsoClient; 143 } 144 145 /** {@inheritDoc} */ 146 @Override getServiceAccountJsonKeyFiles()147 public Map<String, File> getServiceAccountJsonKeyFiles() { 148 return new HashMap<>(mJsonServiceAccountMap); 149 } 150 151 /** {@inheritDoc} */ 152 @Override validateOptions()153 public void validateOptions() throws ConfigurationException { 154 // Validation of host options 155 } 156 157 /** {@inheritDoc} */ 158 @Override getLabels()159 public List<String> getLabels() { 160 return new ArrayList<>(mLabels); 161 } 162 163 /** {@inheritDoc} */ 164 @Override getKnownTcpDeviceIpPool()165 public Set<String> getKnownTcpDeviceIpPool() { 166 return new HashSet<>(mKnownTcpDeviceIpPool); 167 } 168 169 /** {@inheritDoc} */ 170 @Override getKnownGceDeviceIpPool()171 public Set<String> getKnownGceDeviceIpPool() { 172 return new HashSet<>(mKnownGceDeviceIpPool); 173 } 174 175 /** {@inheritDoc} */ 176 @Override getUseZip64InPartialDownload()177 public boolean getUseZip64InPartialDownload() { 178 return mUseZip64InPartialDownload; 179 } 180 181 /** {@inheritDoc} */ 182 @Override getNetworkInterface()183 public String getNetworkInterface() { 184 if (mNetworkInterface != null) { 185 return mNetworkInterface; 186 } 187 188 try { 189 for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); 190 enNetI.hasMoreElements(); ) { 191 NetworkInterface netI = enNetI.nextElement(); 192 if (!netI.isUp()) { 193 continue; 194 } 195 for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); 196 enumIpAddr.hasMoreElements(); ) { 197 InetAddress inetAddress = enumIpAddr.nextElement(); 198 if (!inetAddress.isAnyLocalAddress() 199 && !inetAddress.isLinkLocalAddress() 200 && !inetAddress.isLoopbackAddress() 201 && !inetAddress.isMulticastAddress()) { 202 mNetworkInterface = netI.getName(); 203 return mNetworkInterface; 204 } 205 } 206 } 207 } catch (SocketException e) { 208 CLog.w("Failed to get host's active interface"); 209 CLog.w(e); 210 } 211 return null; 212 } 213 } 214