1 package com.android.hotspot2.pps;
2 
3 import com.android.hotspot2.Utils;
4 import com.android.hotspot2.omadm.MOManager;
5 import com.android.hotspot2.omadm.OMAException;
6 import com.android.hotspot2.omadm.OMANode;
7 
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 
13 import static com.android.hotspot2.omadm.MOManager.TAG_Country;
14 import static com.android.hotspot2.omadm.MOManager.TAG_DLBandwidth;
15 import static com.android.hotspot2.omadm.MOManager.TAG_FQDN_Match;
16 import static com.android.hotspot2.omadm.MOManager.TAG_IPProtocol;
17 import static com.android.hotspot2.omadm.MOManager.TAG_MaximumBSSLoadValue;
18 import static com.android.hotspot2.omadm.MOManager.TAG_MinBackhaulThreshold;
19 import static com.android.hotspot2.omadm.MOManager.TAG_NetworkType;
20 import static com.android.hotspot2.omadm.MOManager.TAG_PolicyUpdate;
21 import static com.android.hotspot2.omadm.MOManager.TAG_PortNumber;
22 import static com.android.hotspot2.omadm.MOManager.TAG_PreferredRoamingPartnerList;
23 import static com.android.hotspot2.omadm.MOManager.TAG_Priority;
24 import static com.android.hotspot2.omadm.MOManager.TAG_RequiredProtoPortTuple;
25 import static com.android.hotspot2.omadm.MOManager.TAG_SPExclusionList;
26 import static com.android.hotspot2.omadm.MOManager.TAG_SSID;
27 import static com.android.hotspot2.omadm.MOManager.TAG_ULBandwidth;
28 
29 public class Policy {
30     private final List<PreferredRoamingPartner> mPreferredRoamingPartners;
31     private final List<MinBackhaul> mMinBackhaulThresholds;
32     private final UpdateInfo mPolicyUpdate;
33     private final List<String> mSPExclusionList;
34     private final Map<Integer, List<Integer>> mRequiredProtos;
35     private final int mMaxBSSLoad;
36 
Policy(OMANode node)37     public Policy(OMANode node) throws OMAException {
38 
39         OMANode rpNode = node.getChild(TAG_PreferredRoamingPartnerList);
40         if (rpNode == null) {
41             mPreferredRoamingPartners = null;
42         } else {
43             mPreferredRoamingPartners = new ArrayList<>(rpNode.getChildren().size());
44             for (OMANode instance : rpNode.getChildren()) {
45                 if (instance.isLeaf()) {
46                     throw new OMAException("Not expecting leaf node in " +
47                             TAG_PreferredRoamingPartnerList);
48                 }
49                 mPreferredRoamingPartners.add(new PreferredRoamingPartner(instance));
50             }
51         }
52 
53         OMANode bhtNode = node.getChild(TAG_MinBackhaulThreshold);
54         if (bhtNode == null) {
55             mMinBackhaulThresholds = null;
56         } else {
57             mMinBackhaulThresholds = new ArrayList<>(bhtNode.getChildren().size());
58             for (OMANode instance : bhtNode.getChildren()) {
59                 if (instance.isLeaf()) {
60                     throw new OMAException("Not expecting leaf node in " +
61                             TAG_MinBackhaulThreshold);
62                 }
63                 mMinBackhaulThresholds.add(new MinBackhaul(instance));
64             }
65         }
66 
67         mPolicyUpdate = new UpdateInfo(node.getChild(TAG_PolicyUpdate));
68 
69         OMANode sxNode = node.getChild(TAG_SPExclusionList);
70         if (sxNode == null) {
71             mSPExclusionList = null;
72         } else {
73             mSPExclusionList = new ArrayList<>(sxNode.getChildren().size());
74             for (OMANode instance : sxNode.getChildren()) {
75                 if (instance.isLeaf()) {
76                     throw new OMAException("Not expecting leaf node in " + TAG_SPExclusionList);
77                 }
78                 mSPExclusionList.add(MOManager.getString(instance, TAG_SSID));
79             }
80         }
81 
82         OMANode rptNode = node.getChild(TAG_RequiredProtoPortTuple);
83         if (rptNode == null) {
84             mRequiredProtos = null;
85         } else {
86             mRequiredProtos = new HashMap<>(rptNode.getChildren().size());
87             for (OMANode instance : rptNode.getChildren()) {
88                 if (instance.isLeaf()) {
89                     throw new OMAException("Not expecting leaf node in " +
90                             TAG_RequiredProtoPortTuple);
91                 }
92                 int protocol = (int) MOManager.getLong(instance, TAG_IPProtocol, null);
93                 String[] portSegments = MOManager.getString(instance, TAG_PortNumber).split(",");
94                 List<Integer> ports = new ArrayList<>(portSegments.length);
95                 for (String portSegment : portSegments) {
96                     try {
97                         ports.add(Integer.parseInt(portSegment));
98                     } catch (NumberFormatException nfe) {
99                         throw new OMAException("Port is not a number: " + portSegment);
100                     }
101                 }
102                 mRequiredProtos.put(protocol, ports);
103             }
104         }
105 
106         mMaxBSSLoad = (int) MOManager.getLong(node, TAG_MaximumBSSLoadValue, Long.MAX_VALUE);
107     }
108 
getPreferredRoamingPartners()109     public List<PreferredRoamingPartner> getPreferredRoamingPartners() {
110         return mPreferredRoamingPartners;
111     }
112 
getMinBackhaulThresholds()113     public List<MinBackhaul> getMinBackhaulThresholds() {
114         return mMinBackhaulThresholds;
115     }
116 
getPolicyUpdate()117     public UpdateInfo getPolicyUpdate() {
118         return mPolicyUpdate;
119     }
120 
getSPExclusionList()121     public List<String> getSPExclusionList() {
122         return mSPExclusionList;
123     }
124 
getRequiredProtos()125     public Map<Integer, List<Integer>> getRequiredProtos() {
126         return mRequiredProtos;
127     }
128 
getMaxBSSLoad()129     public int getMaxBSSLoad() {
130         return mMaxBSSLoad;
131     }
132 
133     private static class PreferredRoamingPartner {
134         private final List<String> mDomain;
135         private final Boolean mIncludeSubDomains;
136         private final int mPriority;
137         private final String mCountry;
138 
PreferredRoamingPartner(OMANode node)139         private PreferredRoamingPartner(OMANode node)
140                 throws OMAException {
141 
142             String[] segments = MOManager.getString(node, TAG_FQDN_Match).split(",");
143             if (segments.length != 2) {
144                 throw new OMAException("Bad FQDN match string: " + TAG_FQDN_Match);
145             }
146             mDomain = Utils.splitDomain(segments[0]);
147             mIncludeSubDomains = MOManager.getSelection(TAG_FQDN_Match, segments[1]);
148             mPriority = (int) MOManager.getLong(node, TAG_Priority, null);
149             mCountry = MOManager.getString(node, TAG_Country);
150         }
151 
152         @Override
toString()153         public String toString() {
154             return "PreferredRoamingPartner{" +
155                     "domain=" + mDomain +
156                     ", includeSubDomains=" + mIncludeSubDomains +
157                     ", priority=" + mPriority +
158                     ", country='" + mCountry + '\'' +
159                     '}';
160         }
161     }
162 
163     private static class MinBackhaul {
164         private final Boolean mHome;
165         private final long mDL;
166         private final long mUL;
167 
MinBackhaul(OMANode node)168         private MinBackhaul(OMANode node) throws OMAException {
169             mHome = MOManager.getSelection(node, TAG_NetworkType);
170             mDL = MOManager.getLong(node, TAG_DLBandwidth, Long.MAX_VALUE);
171             mUL = MOManager.getLong(node, TAG_ULBandwidth, Long.MAX_VALUE);
172         }
173 
174         @Override
toString()175         public String toString() {
176             return "MinBackhaul{" +
177                     "home=" + mHome +
178                     ", DL=" + mDL +
179                     ", UL=" + mUL +
180                     '}';
181         }
182     }
183 
184     @Override
toString()185     public String toString() {
186         return "Policy{" +
187                 "preferredRoamingPartners=" + mPreferredRoamingPartners +
188                 ", minBackhaulThresholds=" + mMinBackhaulThresholds +
189                 ", policyUpdate=" + mPolicyUpdate +
190                 ", SPExclusionList=" + mSPExclusionList +
191                 ", requiredProtos=" + mRequiredProtos +
192                 ", maxBSSLoad=" + mMaxBSSLoad +
193                 '}';
194     }
195 }
196