Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / commissioner / thread / internal / ThreadCommissionerServiceImpl.java
1 /*
2  *   Copyright (c) 2020 Project CHIP Authors
3  *   All rights reserved.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  */
18
19 package com.google.chip.chiptool.commissioner.thread.internal;
20
21 import android.content.Context;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import com.google.chip.chiptool.commissioner.thread.BorderAgentInfo;
25 import com.google.chip.chiptool.commissioner.thread.ThreadCommissionerException;
26 import com.google.chip.chiptool.commissioner.thread.ThreadCommissionerService;
27 import com.google.chip.chiptool.commissioner.thread.ThreadNetworkCredential;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.CompletionException;
30
31 public class ThreadCommissionerServiceImpl implements ThreadCommissionerService {
32
33   private static final String TAG = ThreadCommissionerException.class.getSimpleName();
34
35   private BorderAgentDatabase borderAgentDatabase;
36
37   public ThreadCommissionerServiceImpl(@NonNull Context context) {
38     this.borderAgentDatabase = BorderAgentDatabase.getDatabase(context);
39   }
40
41   @Override
42   public CompletableFuture<ThreadNetworkCredential> FetchThreadNetworkCredential(
43       @NonNull BorderAgentInfo borderAgentInfo, @Nullable byte[] pskc) {
44     return getThreadNetworkCredential(borderAgentInfo)
45         .thenApply(
46             credential -> {
47               if (credential != null) {
48                 return credential;
49               }
50
51               NetworkCredentialFetcher credentialFetcher = new NetworkCredentialFetcher();
52               try {
53                 credential = credentialFetcher.fetchNetworkCredential(borderAgentInfo, pskc);
54                 addThreadNetworkCredential(borderAgentInfo, pskc, credential).get();
55                 return credential;
56               } catch (Exception ex) {
57                 throw new CompletionException(ex);
58               }
59             });
60   }
61
62   @Override
63   public CompletableFuture<ThreadNetworkCredential> getThreadNetworkCredential(
64       @NonNull BorderAgentInfo borderAgentInfo) {
65     return getBorderAgentRecord(borderAgentInfo)
66         .thenApply(
67             borderAgentRecord -> {
68               if (borderAgentRecord == null) {
69                 return null;
70               }
71               return new ThreadNetworkCredential(borderAgentRecord.getActiveOperationalDataset());
72             });
73   }
74
75   CompletableFuture<BorderAgentRecord> getBorderAgentRecord(
76       @NonNull BorderAgentInfo borderAgentInfo) {
77     return borderAgentDatabase.getBorderAgent(borderAgentInfo.discriminator);
78   }
79
80   @Override
81   public CompletableFuture<Void> deleteThreadNetworkCredential(
82       @NonNull BorderAgentInfo borderAgentInfo) {
83     return borderAgentDatabase.deleteBorderAgent(borderAgentInfo.discriminator);
84   }
85
86   // This method adds given Thread Network Credential into database on the phone.
87   CompletableFuture<Void> addThreadNetworkCredential(
88       @NonNull BorderAgentInfo borderAgentInfo,
89       @NonNull byte[] pskc,
90       @NonNull ThreadNetworkCredential networkCredential) {
91     BorderAgentRecord borderAgentRecord =
92         new BorderAgentRecord(
93             borderAgentInfo.discriminator,
94             borderAgentInfo.networkName,
95             borderAgentInfo.extendedPanId,
96             pskc,
97             networkCredential.getEncoded());
98     return borderAgentDatabase.insertBorderAgent(borderAgentRecord);
99   }
100 }