1 /*
2  * Copyright (C) 2024 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.adservices.data.kanon;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 import androidx.room.ColumnInfo;
22 import androidx.room.Entity;
23 import androidx.room.PrimaryKey;
24 import androidx.room.TypeConverters;
25 
26 import com.android.adservices.data.common.FledgeRoomConverters;
27 
28 import com.google.auto.value.AutoValue;
29 import com.google.auto.value.AutoValue.CopyAnnotations;
30 
31 import java.time.Instant;
32 
33 @Entity(tableName = DBKAnonMessage.TABLE_NAME)
34 @TypeConverters({FledgeRoomConverters.class})
35 @AutoValue
36 @CopyAnnotations
37 public abstract class DBKAnonMessage {
38     public static final String TABLE_NAME = "kanon_messages";
39 
40     /**
41      * @return the message_id for this KAnonMessage.
42      */
43     @Nullable
44     @CopyAnnotations
45     @PrimaryKey(autoGenerate = true)
46     @ColumnInfo(name = "message_id")
getMessageId()47     public abstract Long getMessageId();
48 
49     /**
50      * @return the createdAt instant for this message.
51      */
52     @NonNull
53     @CopyAnnotations
54     @ColumnInfo(name = "created_at")
getCreatedAt()55     public abstract Instant getCreatedAt();
56 
57     /**
58      * @return the expiry_instant for this message.
59      */
60     @NonNull
61     @CopyAnnotations
62     @ColumnInfo(name = "expiry_instant")
getExpiryInstant()63     public abstract Instant getExpiryInstant();
64 
65     /**
66      * Corresponding client parameters id. This field is initially null and once the message is
67      * joined/signed, this field is updated to match the id of the client parameters used to make
68      * the sign/join calls.
69      *
70      * @return the Corresponding client parameters id for this message.
71      */
72     @Nullable
73     @CopyAnnotations
74     @ColumnInfo(name = "corresponding_client_parameters_id")
getCorrespondingClientParametersId()75     public abstract Long getCorrespondingClientParametersId();
76 
77     /**
78      * Corresponding client parameters expiry instant. This field is initially null and once the
79      * message is joined/signed, this field is updated to match the expiry instant of the client
80      * parameters used to make the sign/join calls. We need this field to keep track of expiry
81      * instant of the client parameters used for signing/joining this message.
82      *
83      * @return the Corresponding client parameters expiry instant for this message.
84      */
85     @Nullable
86     @CopyAnnotations
87     @ColumnInfo(name = "corresponding_client_parameters_expiry_instant")
getCorrespondingClientParametersExpiryInstant()88     public abstract Instant getCorrespondingClientParametersExpiryInstant();
89 
90     /**
91      * @return the ad selection id for this message.
92      */
93     @NonNull
94     @CopyAnnotations
95     @ColumnInfo(name = "ad_selection_id")
getAdSelectionId()96     public abstract long getAdSelectionId();
97 
98     /**
99      * Hash set corresponding to this message. This is the set that needs to be signed/joined.
100      *
101      * @return the kanon hash set for this message.
102      */
103     @NonNull
104     @CopyAnnotations
105     @ColumnInfo(name = "kanon_hash_set")
getKanonHashSet()106     public abstract String getKanonHashSet();
107 
108     /**
109      * @return the {@link KAnonMessageConstants.MessageStatus} for this message.
110      */
111     @NonNull
112     @CopyAnnotations
113     @ColumnInfo(name = "status")
114     @KAnonMessageConstants.MessageStatus
getStatus()115     public abstract int getStatus();
116 
117     /** Returns a {@link DBKAnonMessage.Builder} for {@link DBKAnonMessage} */
builder()118     public static DBKAnonMessage.Builder builder() {
119         return new AutoValue_DBKAnonMessage.Builder();
120     }
121 
122     /** Creates and returns a {@link DBKAnonMessage} object. */
create( @ullable Long messageId, @NonNull Instant createdAt, @NonNull Instant expiryInstant, @Nullable Long correspondingClientParametersId, @Nullable Instant correspondingClientParametersExpiryInstant, @KAnonMessageConstants.MessageStatus int status, @NonNull String kanonHashSet, long adSelectionId)123     public static DBKAnonMessage create(
124             @Nullable Long messageId,
125             @NonNull Instant createdAt,
126             @NonNull Instant expiryInstant,
127             @Nullable Long correspondingClientParametersId,
128             @Nullable Instant correspondingClientParametersExpiryInstant,
129             @KAnonMessageConstants.MessageStatus int status,
130             @NonNull String kanonHashSet,
131             long adSelectionId) {
132         return builder()
133                 .setAdSelectionId(adSelectionId)
134                 .setCreatedAt(createdAt)
135                 .setMessageId(messageId)
136                 .setExpiryInstant(expiryInstant)
137                 .setStatus(status)
138                 .setCorrespondingClientParametersExpiryInstant(
139                         correspondingClientParametersExpiryInstant)
140                 .setKanonHashSet(kanonHashSet)
141                 .setCorrespondingClientParametersId(correspondingClientParametersId)
142                 .build();
143     }
144 
145     /** Builder class for {@link DBKAnonMessage} */
146     @AutoValue.Builder
147     public abstract static class Builder {
148 
149         /** Sets the message id. */
150         @NonNull
setMessageId(@ullable Long messageId)151         public abstract Builder setMessageId(@Nullable Long messageId);
152 
153         /** Sets the createdAt field */
154         @NonNull
setCreatedAt(Instant createdAt)155         public abstract Builder setCreatedAt(Instant createdAt);
156 
157         /** Sets the expiry instant. */
158         @NonNull
setExpiryInstant(Instant expiryInstant)159         public abstract Builder setExpiryInstant(Instant expiryInstant);
160 
161         /** Sets the Corresponding client parameters id */
162         @NonNull
setCorrespondingClientParametersId( Long correspondingClientParametersId)163         public abstract Builder setCorrespondingClientParametersId(
164                 Long correspondingClientParametersId);
165 
166         /** Sets the Corresponding client parameters expiry instant */
167         @NonNull
setCorrespondingClientParametersExpiryInstant( Instant correspondingClientParametersExpiryInstant)168         public abstract Builder setCorrespondingClientParametersExpiryInstant(
169                 Instant correspondingClientParametersExpiryInstant);
170 
171         /** Sets the {@link KAnonMessageConstants.MessageStatus} for the message */
172         @NonNull
setStatus(@AnonMessageConstants.MessageStatus int status)173         public abstract Builder setStatus(@KAnonMessageConstants.MessageStatus int status);
174 
175         /** Sets the kanon hash set string */
176         @NonNull
setKanonHashSet(String kanonHashSet)177         public abstract Builder setKanonHashSet(String kanonHashSet);
178 
179         /** Sets the ad selection id corresponding to this kanon message */
180         @NonNull
setAdSelectionId(long adSelectionId)181         public abstract Builder setAdSelectionId(long adSelectionId);
182 
183         /** Builds and return a {@link DBKAnonMessage} message. */
build()184         public abstract DBKAnonMessage build();
185     }
186 }
187