xref: /aosp_15_r20/tools/carrier_settings/java/GenCarrierList.java (revision ff35212d322a3e892605b94fa777c67085d45efd)
1*ff35212dScey /*
2*ff35212dScey  * Copyright (C) 2020 Google LLC
3*ff35212dScey  *
4*ff35212dScey  * Licensed under the Apache License, Version 2.0 (the "License");
5*ff35212dScey  * you may not use this file except in compliance with the License.
6*ff35212dScey  * You may obtain a copy of the License at
7*ff35212dScey  *
8*ff35212dScey  *      http://www.apache.org/licenses/LICENSE-2.0
9*ff35212dScey  *
10*ff35212dScey  * Unless required by applicable law or agreed to in writing, software
11*ff35212dScey  * distributed under the License is distributed on an "AS IS" BASIS,
12*ff35212dScey  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ff35212dScey  * See the License for the specific language governing permissions and
14*ff35212dScey  * limitations under the License.
15*ff35212dScey  */
16*ff35212dScey package com.google.carrier;
17*ff35212dScey 
18*ff35212dScey import static java.nio.charset.StandardCharsets.UTF_8;
19*ff35212dScey 
20*ff35212dScey import com.beust.jcommander.JCommander;
21*ff35212dScey import com.beust.jcommander.Parameter;
22*ff35212dScey import com.beust.jcommander.Parameters;
23*ff35212dScey import com.google.carrier.CarrierId;
24*ff35212dScey import com.google.carrier.CarrierList;
25*ff35212dScey import com.google.carrier.CarrierMap;
26*ff35212dScey import com.google.common.base.Verify;
27*ff35212dScey import com.google.protobuf.TextFormat;
28*ff35212dScey import java.io.BufferedReader;
29*ff35212dScey import java.io.BufferedWriter;
30*ff35212dScey import java.io.IOException;
31*ff35212dScey import java.io.OutputStream;
32*ff35212dScey import java.nio.file.Files;
33*ff35212dScey import java.nio.file.Paths;
34*ff35212dScey import java.util.ArrayList;
35*ff35212dScey import java.util.Arrays;
36*ff35212dScey import java.util.List;
37*ff35212dScey 
38*ff35212dScey /**
39*ff35212dScey  * This command line tool generate a single carrier_list.pb. The output pb is used as a reverse-map:
40*ff35212dScey  * each entry is a CarrierMap with a canonical_name and only one CarrierId; entries are sorted by
41*ff35212dScey  * mcc_mnc, and MVNO comes before MNO. So that when matching a CarrierId against this list, the
42*ff35212dScey  * first match is always the best guess of carrier name.
43*ff35212dScey  */
44*ff35212dScey @Parameters(separators = "=")
45*ff35212dScey public final class GenCarrierList {
46*ff35212dScey   @Parameter(
47*ff35212dScey       names = "--version_offset",
48*ff35212dScey       description =
49*ff35212dScey           "The value to be added to file version, used to differentiate releases/branches.")
50*ff35212dScey   private long versionOffset = 0L;
51*ff35212dScey 
52*ff35212dScey   @Parameter(
53*ff35212dScey       names = "--in_textpbs",
54*ff35212dScey       description = "A comma-separated list of input CarrierList textpb files")
55*ff35212dScey   private List<String> textpbFileNames = Arrays.asList();
56*ff35212dScey 
57*ff35212dScey   @Parameter(names = "--out_pb", description = "The output CarrierList pb file")
58*ff35212dScey   private String outFileName = "/tmp/carrier_list.pb";
59*ff35212dScey 
60*ff35212dScey   @Parameter(
61*ff35212dScey       names = "--with_version_number",
62*ff35212dScey       description = "Encode version number into output pb filename.")
63*ff35212dScey   private boolean versionInFileName = false;
64*ff35212dScey 
65*ff35212dScey   public static final String BINARY_PB_SUFFIX = ".pb";
66*ff35212dScey   public static final String TEXT_PB_SUFFIX = ".textpb";
67*ff35212dScey 
main(String[] args)68*ff35212dScey   public static void main(String[] args) throws IOException {
69*ff35212dScey     GenCarrierList generator = new GenCarrierList();
70*ff35212dScey     new JCommander(generator, args);
71*ff35212dScey     generator.generate();
72*ff35212dScey   }
73*ff35212dScey 
generate()74*ff35212dScey   private void generate() throws IOException {
75*ff35212dScey     long version = 0;
76*ff35212dScey 
77*ff35212dScey     Verify.verify(
78*ff35212dScey         outFileName.endsWith(BINARY_PB_SUFFIX),
79*ff35212dScey         "Output filename must end with %s.",
80*ff35212dScey         BINARY_PB_SUFFIX);
81*ff35212dScey 
82*ff35212dScey     List<CarrierMap> list = new ArrayList<>();
83*ff35212dScey     for (String textpbFileName : textpbFileNames) {
84*ff35212dScey       // Load textpbFileName
85*ff35212dScey       CarrierList carrierList = null;
86*ff35212dScey       try (BufferedReader br = Files.newBufferedReader(Paths.get(textpbFileName), UTF_8)) {
87*ff35212dScey         CarrierList.Builder builder = CarrierList.newBuilder();
88*ff35212dScey         TextFormat.getParser().merge(br, builder);
89*ff35212dScey         carrierList = builder.build();
90*ff35212dScey       }
91*ff35212dScey 
92*ff35212dScey       // Add carrierList into list
93*ff35212dScey       for (CarrierMap cm : carrierList.getEntryList()) {
94*ff35212dScey         for (CarrierId cid : cm.getCarrierIdList()) {
95*ff35212dScey           list.add(cm.toBuilder().clearCarrierId().addCarrierId(cid).build());
96*ff35212dScey         }
97*ff35212dScey       }
98*ff35212dScey 
99*ff35212dScey       // Add up version number
100*ff35212dScey       version += carrierList.getVersion();
101*ff35212dScey     }
102*ff35212dScey 
103*ff35212dScey     // Bump version according to the release
104*ff35212dScey     version = CarrierProtoUtils.addVersionOffset(version, versionOffset);
105*ff35212dScey 
106*ff35212dScey     // Sort the list as per carrier identification algorithm requirement
107*ff35212dScey     CarrierProtoUtils.sortCarrierMapEntries(list);
108*ff35212dScey 
109*ff35212dScey     // Convert list to CarrierList
110*ff35212dScey     CarrierList clist = CarrierList.newBuilder().setVersion(version).addAllEntry(list).build();
111*ff35212dScey 
112*ff35212dScey     // Output
113*ff35212dScey     String outFileMainName = outFileName.replace(BINARY_PB_SUFFIX, "");
114*ff35212dScey     try (BufferedWriter bw =
115*ff35212dScey         Files.newBufferedWriter(Paths.get(outFileMainName + TEXT_PB_SUFFIX), UTF_8)) {
116*ff35212dScey       TextFormat.printUnicode(clist, bw);
117*ff35212dScey     }
118*ff35212dScey 
119*ff35212dScey     if (versionInFileName) {
120*ff35212dScey       outFileMainName += "-" + clist.getVersion();
121*ff35212dScey     }
122*ff35212dScey     try (OutputStream os = Files.newOutputStream(Paths.get(outFileMainName + BINARY_PB_SUFFIX))) {
123*ff35212dScey       clist.writeTo(os);
124*ff35212dScey     }
125*ff35212dScey   }
126*ff35212dScey 
GenCarrierList()127*ff35212dScey   private GenCarrierList() {}
128*ff35212dScey }
129