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 package com.android.cobalt.domain; 17 18 import static com.google.common.base.Preconditions.checkArgument; 19 20 import static java.util.Objects.requireNonNull; 21 22 import com.android.internal.annotations.VisibleForTesting; 23 24 import com.google.auto.value.AutoValue; 25 import com.google.cobalt.CobaltRegistry; 26 import com.google.cobalt.CustomerConfig; 27 import com.google.cobalt.MetricDefinition; 28 import com.google.cobalt.ProjectConfig; 29 import com.google.common.collect.ImmutableList; 30 31 /** Domain object for Cobalt's registry proto that supports a single customer and project. */ 32 @AutoValue 33 public abstract class Project { 34 public static final int CUSTOMER_ID = 200004; 35 public static final int PROJECT_ID = 2; 36 37 /** 38 * Parses a Project from a {@link com.google.cobalt.CobaltRegistry}. 39 * 40 * @param registry the {@link com.google.cobalt.CobaltRegistry} protocol buffer 41 * @return the parsed {@link Project} 42 * @throws Exception if the provided registry has more than 1 customer or more than 1 project 43 */ create(CobaltRegistry registry)44 public static Project create(CobaltRegistry registry) { 45 requireNonNull(registry); 46 for (CustomerConfig customer : registry.getCustomersList()) { 47 if (customer.getCustomerId() != CUSTOMER_ID) { 48 continue; 49 } 50 for (ProjectConfig project : customer.getProjectsList()) { 51 if (project.getProjectId() == PROJECT_ID) { 52 return Project.create( 53 customer.getCustomerId(), 54 project.getProjectId(), 55 project.getMetricsList()); 56 } 57 } 58 } 59 60 checkArgument(false, "AdServices project not found..."); 61 return Project.create(-1, -1, ImmutableList.of()); 62 } 63 64 /** 65 * Creates a Project with the provided customer and project info. 66 * 67 * @param customerId the customer's id 68 * @param projectId the customer's project id 69 * @param metrics the metrics the customer is collecting in this project id 70 * @return a {@link Project} for the customer's project 71 */ 72 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) create( int customerId, int projectId, Iterable<MetricDefinition> metrics)73 public static Project create( 74 int customerId, int projectId, Iterable<MetricDefinition> metrics) { 75 return new AutoValue_Project( 76 customerId, projectId, ImmutableList.copyOf(requireNonNull(metrics))); 77 } 78 79 /** 80 * @return the customer id 81 */ getCustomerId()82 public abstract int getCustomerId(); 83 84 /** 85 * @return the customer's project id 86 */ getProjectId()87 public abstract int getProjectId(); 88 89 /** 90 * @return the metrics being collected by the customer for the project 91 */ getMetrics()92 public abstract ImmutableList<MetricDefinition> getMetrics(); 93 } 94