1 /*
2  * Copyright (C) 2021 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 android.car.cts.powerpolicy;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Objects;
22 
23 public final class PowerPolicyGroups {
24     private final HashMap<String, PowerPolicyGroupDef> mPolicyGroups = new HashMap<>();
25 
PowerPolicyGroups()26     public PowerPolicyGroups() { }
27 
PowerPolicyGroups(PowerPolicyGroupDef[] defs)28     public PowerPolicyGroups(PowerPolicyGroupDef[] defs) {
29         for (int i = 0; i < defs.length; i++) {
30             mPolicyGroups.put(defs[i].mGroupId, defs[i]);
31         }
32     }
33 
add(String id, String waitForVHALPolicy, String onPolicy)34     public void add(String id, String waitForVHALPolicy, String onPolicy) throws Exception {
35         if (mPolicyGroups.containsKey(id)) {
36             throw new IllegalArgumentException(id + " policy group already exists");
37         }
38         PowerPolicyGroupDef groupDef = new PowerPolicyGroupDef(id, waitForVHALPolicy, onPolicy);
39         mPolicyGroups.put(id, groupDef);
40     }
41 
42     @Override
toString()43     public String toString() {
44         StringBuilder str = new StringBuilder();
45         str.append("Power policy groups:\n");
46         mPolicyGroups.forEach((k, v) -> str.append(v.toString()));
47         return str.toString();
48     }
49 
50     @Override
hashCode()51     public int hashCode() {
52         return mPolicyGroups.hashCode();
53     }
54 
55     @Override
equals(Object o)56     public boolean equals(Object o) {
57         if (this == o) return true;
58         if (o == null || getClass() != o.getClass()) return false;
59         PowerPolicyGroups peer = (PowerPolicyGroups) o;
60         return mPolicyGroups.equals(peer.mPolicyGroups);
61     }
62 
parse(ArrayList<String> defStrs)63     public static PowerPolicyGroups parse(ArrayList<String> defStrs) throws Exception {
64         if ((defStrs.size() % 3) != 0) {
65             throw new IllegalArgumentException("each policy group def needs 3 lines of data");
66         }
67 
68         PowerPolicyGroups policyGroups = new PowerPolicyGroups();
69         for (int i = 0; i < defStrs.size(); i += 3) {
70             String groupId = defStrs.get(i).trim();
71             String waitForVHALPolicy = parsePolicyGroupDef("WaitForVHAL", defStrs.get(i + 1));
72             String onPolicy = parsePolicyGroupDef("On", defStrs.get(i + 2));
73             policyGroups.add(groupId, waitForVHALPolicy, onPolicy);
74         }
75         return policyGroups;
76     }
77 
parsePolicyGroupDef(String stateName, String defStr)78     private static String parsePolicyGroupDef(String stateName, String defStr) throws Exception {
79         String[] tokens = defStr.trim().split("(\\s*)(-{1,2})(>?)(\\s*)");
80         if (tokens.length != 3) {
81             throw new IllegalArgumentException("malformatted policy group def str: " + defStr);
82         }
83 
84         if (!stateName.equals(tokens[1].trim())) {
85             String errMsg = String.format("expected power state: %s but got: %s",
86                     stateName, tokens[1]);
87             throw new IllegalArgumentException(errMsg);
88         }
89 
90         return tokens[2].trim();
91     }
92 
93     public static final class PowerPolicyGroupDef {
94         private final String mGroupId;
95         private final String mWaitForVHALStatePolicy;
96         private final String mOnStatePolicy;
97 
PowerPolicyGroupDef(String groupId, String waitForVHALPolicy, String onPolicy)98         private PowerPolicyGroupDef(String groupId, String waitForVHALPolicy, String onPolicy) {
99             mGroupId = groupId;
100             mWaitForVHALStatePolicy = waitForVHALPolicy;
101             mOnStatePolicy = onPolicy;
102         }
103 
getGroupId()104         public String getGroupId() {
105             return mGroupId;
106         }
107 
getWaitForVHALStatePolicy()108         public String getWaitForVHALStatePolicy() {
109             return mWaitForVHALStatePolicy;
110         }
111 
getOnStatePolicy()112         public String getOnStatePolicy() {
113             return mOnStatePolicy;
114         }
115 
toShellCommandString()116         public String toShellCommandString() {
117             return String.format("%s WaitForVHAL:%s On:%s", mGroupId,
118                     mWaitForVHALStatePolicy, mOnStatePolicy);
119         }
120 
121         @Override
toString()122         public String toString() {
123             StringBuilder str = new StringBuilder();
124             str.append("  ").append(mGroupId).append('\n');
125             str.append("    - WaitForVHAL --> ").append(mWaitForVHALStatePolicy).append('\n');
126             str.append("    - On --> ").append(mOnStatePolicy).append('\n');
127             return str.toString();
128         }
129 
130         @Override
equals(Object o)131         public boolean equals(Object o) {
132             if (this == o) return true;
133             if (o == null || getClass() != o.getClass()) return false;
134             PowerPolicyGroupDef that = (PowerPolicyGroupDef) o;
135             return Objects.equals(mGroupId, that.mGroupId)
136                     && Objects.equals(mWaitForVHALStatePolicy, that.mWaitForVHALStatePolicy)
137                     && Objects.equals(mOnStatePolicy, that.mOnStatePolicy);
138         }
139 
140         @Override
hashCode()141         public int hashCode() {
142             return Objects.hash(mGroupId, mWaitForVHALStatePolicy, mOnStatePolicy);
143         }
144     }
145 
146     public static final class TestSet {
147         public static final String GROUP_ID1 = "policy_group1";
148         public static final String GROUP_ID2 = "policy_group2";
149 
150         public static final PowerPolicyGroupDef POLICY_GROUP_DEF1 =
151                 new PowerPolicyGroupDef(GROUP_ID1, PowerPolicyDef.IdSet.TEST1,
152                     PowerPolicyDef.IdSet.TEST2);
153 
154         public static final PowerPolicyGroupDef POLICY_GROUP_DEF2 =
155                 new PowerPolicyGroupDef(GROUP_ID2, PowerPolicyDef.IdSet.TEST2,
156                     PowerPolicyDef.IdSet.TEST1);
157 
158         public static final PowerPolicyGroups POLICY_GROUPS1 = new PowerPolicyGroups(
159                 new PowerPolicyGroupDef[]{POLICY_GROUP_DEF1, POLICY_GROUP_DEF2});
160 
TestSet()161         private TestSet() { }
162     }
163 }
164