• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.tradefed.log.LogUtil;
20 
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Objects;
24 import java.util.Set;
25 
26 public final class PowerPolicyGroups {
27     private final HashMap<String, PowerPolicyGroupDef> mPolicyGroups = new HashMap<>();
28 
PowerPolicyGroups()29     public PowerPolicyGroups() { }
30 
PowerPolicyGroups(PowerPolicyGroupDef[] defs)31     public PowerPolicyGroups(PowerPolicyGroupDef[] defs) {
32         for (int i = 0; i < defs.length; i++) {
33             mPolicyGroups.put(defs[i].mGroupId, defs[i]);
34         }
35     }
36 
add(String id, String waitForVHALPolicy, String onPolicy)37     public void add(String id, String waitForVHALPolicy, String onPolicy) throws Exception {
38         if (mPolicyGroups.containsKey(id)) {
39             throw new IllegalArgumentException(id + " policy group already exists");
40         }
41         PowerPolicyGroupDef groupDef = new PowerPolicyGroupDef(id, waitForVHALPolicy, onPolicy);
42         mPolicyGroups.put(id, groupDef);
43     }
44 
45     @Override
toString()46     public String toString() {
47         StringBuilder str = new StringBuilder();
48         str.append("Power policy groups:\n");
49         mPolicyGroups.forEach((k, v) -> str.append(v.toString()));
50         return str.toString();
51     }
52 
53     @Override
hashCode()54     public int hashCode() {
55         return mPolicyGroups.hashCode();
56     }
57 
58     @Override
equals(Object o)59     public boolean equals(Object o) {
60         if (this == o) return true;
61         if (o == null || getClass() != o.getClass()) return false;
62         PowerPolicyGroups peer = (PowerPolicyGroups) o;
63         return mPolicyGroups.equals(peer.mPolicyGroups);
64     }
65 
parse(ArrayList<String> defStrs)66     public static PowerPolicyGroups parse(ArrayList<String> defStrs) throws Exception {
67         PowerPolicyGroups policyGroups = new PowerPolicyGroups();
68         String groupId = null;
69         String waitForVHALPolicy = null;
70         String onPolicy = null;
71 
72         String groupDefDelimiter = "-->";
73         for (int i = 0; i < defStrs.size(); ++i) {
74             String line = defStrs.get(i);
75             if (line.contains(groupDefDelimiter)) {
76                 // this is policy group definition
77                 if (line.contains("WaitForVHAL")) {
78                     waitForVHALPolicy = parsePolicyGroupDef("WaitForVHAL", line);
79                 } else if (line.contains("On")) {
80                     onPolicy = parsePolicyGroupDef("On", line);
81                 } else {
82                     LogUtil.CLog.d("Policy group is ignored: " + line);
83                 }
84             } else {
85                 // Found name, if name is not empty, another group was already found
86                 // add previous group to the policyGroups before proceeding with current one
87                 if (groupId != null) {
88                     policyGroups.add(groupId, waitForVHALPolicy, onPolicy);
89                     waitForVHALPolicy = null;
90                     onPolicy = null;
91                 }
92                 groupId = line.trim();
93             }
94         }
95         // If group wasn't saved (indicated by non-null values of policies), save it
96         if (groupId != null && (waitForVHALPolicy != null || onPolicy != null)) {
97             policyGroups.add(groupId, waitForVHALPolicy, onPolicy);
98         }
99         return policyGroups;
100     }
101 
parsePolicyGroupDef(String stateName, String defStr)102     private static String parsePolicyGroupDef(String stateName, String defStr) throws Exception {
103         String[] tokens = defStr.trim().split("(\\s*)(-{1,2})(>?)(\\s*)");
104         if (tokens.length != 3) {
105             throw new IllegalArgumentException("malformatted policy group def str: " + defStr);
106         }
107 
108         if (!stateName.equals(tokens[1].trim())) {
109             String errMsg = String.format("expected power state: %s but got: %s",
110                     stateName, tokens[1]);
111             throw new IllegalArgumentException(errMsg);
112         }
113 
114         return tokens[2].trim();
115     }
116 
getGroupIds()117     public Set<String> getGroupIds() {
118         return mPolicyGroups.keySet();
119     }
120 
getGroup(String groupId)121     public PowerPolicyGroupDef getGroup(String groupId) {
122         return mPolicyGroups.get(groupId);
123     }
124 
containsGroup(String groupId, PowerPolicyGroupDef expectedGroupDef)125     public boolean containsGroup(String groupId, PowerPolicyGroupDef expectedGroupDef) {
126         PowerPolicyGroupDef policyGroup = mPolicyGroups.get(groupId);
127         if (policyGroup == null) {
128             return false;
129         }
130 
131         return policyGroup.equals(expectedGroupDef);
132     }
133 
134     public static final class PowerPolicyGroupDef {
135         private final String mGroupId;
136         private final String mWaitForVHALStatePolicy;
137         private final String mOnStatePolicy;
138 
PowerPolicyGroupDef(String groupId, String waitForVHALPolicy, String onPolicy)139         private PowerPolicyGroupDef(String groupId, String waitForVHALPolicy, String onPolicy) {
140             mGroupId = groupId;
141             mWaitForVHALStatePolicy = waitForVHALPolicy;
142             mOnStatePolicy = onPolicy;
143         }
144 
getGroupId()145         public String getGroupId() {
146             return mGroupId;
147         }
148 
getWaitForVHALStatePolicy()149         public String getWaitForVHALStatePolicy() {
150             return mWaitForVHALStatePolicy;
151         }
152 
getOnStatePolicy()153         public String getOnStatePolicy() {
154             return mOnStatePolicy;
155         }
156 
toShellCommandString()157         public String toShellCommandString() {
158             return String.format("%s WaitForVHAL:%s On:%s", mGroupId,
159                     mWaitForVHALStatePolicy, mOnStatePolicy);
160         }
161 
162         @Override
toString()163         public String toString() {
164             StringBuilder str = new StringBuilder();
165             str.append("  ").append(mGroupId).append('\n');
166             str.append("    - WaitForVHAL --> ").append(mWaitForVHALStatePolicy).append('\n');
167             str.append("    - On --> ").append(mOnStatePolicy).append('\n');
168             return str.toString();
169         }
170 
171         @Override
equals(Object o)172         public boolean equals(Object o) {
173             if (this == o) return true;
174             if (o == null || getClass() != o.getClass()) return false;
175             PowerPolicyGroupDef that = (PowerPolicyGroupDef) o;
176             return Objects.equals(mGroupId, that.mGroupId)
177                     && Objects.equals(mWaitForVHALStatePolicy, that.mWaitForVHALStatePolicy)
178                     && Objects.equals(mOnStatePolicy, that.mOnStatePolicy);
179         }
180 
181         @Override
hashCode()182         public int hashCode() {
183             return Objects.hash(mGroupId, mWaitForVHALStatePolicy, mOnStatePolicy);
184         }
185     }
186 
187     public static final class TestSet {
188         public static final String GROUP_ID1 = "policy_group1";
189         public static final String GROUP_ID2 = "policy_group2";
190 
191         public static final PowerPolicyGroupDef POLICY_GROUP_DEF1 =
192                 new PowerPolicyGroupDef(GROUP_ID1, PowerPolicyDef.IdSet.TEST1,
193                     PowerPolicyDef.IdSet.TEST2);
194 
195         public static final PowerPolicyGroupDef POLICY_GROUP_DEF2 =
196                 new PowerPolicyGroupDef(GROUP_ID2, PowerPolicyDef.IdSet.TEST2,
197                     PowerPolicyDef.IdSet.TEST1);
198 
199         public static final PowerPolicyGroups POLICY_GROUPS1 = new PowerPolicyGroups(
200                 new PowerPolicyGroupDef[]{POLICY_GROUP_DEF1, POLICY_GROUP_DEF2});
201 
TestSet()202         private TestSet() { }
203     }
204 }
205