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 android.annotation.NonNull; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 26 import com.google.auto.value.AutoValue; 27 import com.google.cobalt.CobaltRegistry; 28 import com.google.cobalt.MetricDefinition; 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 /** 35 * Parses a Project from a {@link com.google.cobalt.CobaltRegistry}. 36 * 37 * @param registry the {@link com.google.cobalt.CobaltRegistry} protocol buffer 38 * @return the parsed {@link Project} 39 * @throws Exception if the provided registry has more than 1 customer or more than 1 project 40 */ 41 @NonNull create(@onNull CobaltRegistry registry)42 public static Project create(@NonNull CobaltRegistry registry) { 43 requireNonNull(registry); 44 checkArgument(registry.getCustomersCount() == 1, "must be one customer"); 45 checkArgument(registry.getCustomers(0).getProjectsCount() == 1, "must be one project"); 46 47 return Project.create( 48 registry.getCustomers(0).getCustomerId(), 49 registry.getCustomers(0).getProjects(0).getProjectId(), 50 registry.getCustomers(0).getProjects(0).getMetricsList()); 51 } 52 53 /** 54 * Creates a Project with the provided customer and project info. 55 * 56 * @param customerId the customer's id 57 * @param projectId the customer's project id 58 * @param metrics the metrics the customer is collecting in this project id 59 * @return a {@link Project} for the customer's project 60 */ 61 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) 62 @NonNull create( int customerId, int projectId, @NonNull Iterable<MetricDefinition> metrics)63 public static Project create( 64 int customerId, int projectId, @NonNull Iterable<MetricDefinition> metrics) { 65 return new AutoValue_Project( 66 customerId, projectId, ImmutableList.copyOf(requireNonNull(metrics))); 67 } 68 69 /** 70 * @return the customer id 71 */ getCustomerId()72 public abstract int getCustomerId(); 73 74 /** 75 * @return the customer's project id 76 */ getProjectId()77 public abstract int getProjectId(); 78 79 /** 80 * @return the metrics being collected by the customer for the project 81 */ 82 @NonNull getMetrics()83 public abstract ImmutableList<MetricDefinition> getMetrics(); 84 } 85