1 /* 2 * Copyright (C) 2021 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 package com.android.tradefed.config.filter; 17 18 import com.android.tradefed.config.IConfiguration; 19 import com.android.tradefed.config.IConfigurationReceiver; 20 import com.android.tradefed.service.IRemoteFeature; 21 22 import com.google.common.base.Joiner; 23 import com.proto.tradefed.feature.ErrorInfo; 24 import com.proto.tradefed.feature.FeatureRequest; 25 import com.proto.tradefed.feature.FeatureResponse; 26 import com.proto.tradefed.feature.MultiPartResponse; 27 import com.proto.tradefed.feature.PartResponse; 28 29 /** Service implementation that returns the filters of a given invocation. */ 30 public class GlobalFilterGetter implements IRemoteFeature, IConfigurationReceiver { 31 32 public static final String GLOBAL_FILTER_GETTER = "getGlobalFilters"; 33 private static final String DELIMITER = "+,"; 34 private static final String ESCAPED_DELIMITER = "\\+,"; 35 36 private IConfiguration mConfig; 37 38 @Override getName()39 public String getName() { 40 return GLOBAL_FILTER_GETTER; 41 } 42 43 @Override setConfiguration(IConfiguration configuration)44 public void setConfiguration(IConfiguration configuration) { 45 mConfig = configuration; 46 } 47 48 @Override execute(FeatureRequest request)49 public FeatureResponse execute(FeatureRequest request) { 50 FeatureResponse.Builder responseBuilder = FeatureResponse.newBuilder(); 51 if (mConfig != null) { 52 MultiPartResponse.Builder multiPartBuilder = MultiPartResponse.newBuilder(); 53 multiPartBuilder.addResponsePart( 54 PartResponse.newBuilder() 55 .setKey(GlobalTestFilter.DELIMITER_NAME) 56 .setValue(ESCAPED_DELIMITER)); 57 multiPartBuilder.addResponsePart( 58 PartResponse.newBuilder() 59 .setKey(GlobalTestFilter.INCLUDE_FILTER_OPTION) 60 .setValue( 61 Joiner.on(DELIMITER) 62 .join(mConfig.getGlobalFilters().getIncludeFilters()))); 63 multiPartBuilder.addResponsePart( 64 PartResponse.newBuilder() 65 .setKey(GlobalTestFilter.EXCLUDE_FILTER_OPTION) 66 .setValue( 67 Joiner.on(DELIMITER) 68 .join(mConfig.getGlobalFilters().getExcludeFilters()))); 69 multiPartBuilder.addResponsePart( 70 PartResponse.newBuilder() 71 .setKey(GlobalTestFilter.STRICT_INCLUDE_FILTER_OPTION) 72 .setValue( 73 Joiner.on(DELIMITER) 74 .join( 75 mConfig.getGlobalFilters() 76 .getStrictIncludeFilters()))); 77 responseBuilder.setMultiPartResponse(multiPartBuilder); 78 } else { 79 responseBuilder.setErrorInfo( 80 ErrorInfo.newBuilder().setErrorTrace("Configuration not set.")); 81 } 82 return responseBuilder.build(); 83 } 84 } 85