1package com.android.hotspot2.pps;
2
3import com.android.hotspot2.Utils;
4import com.android.hotspot2.omadm.MOManager;
5import com.android.hotspot2.omadm.OMAException;
6import com.android.hotspot2.omadm.OMANode;
7
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import static com.android.hotspot2.omadm.MOManager.TAG_Country;
14import static com.android.hotspot2.omadm.MOManager.TAG_DLBandwidth;
15import static com.android.hotspot2.omadm.MOManager.TAG_FQDN_Match;
16import static com.android.hotspot2.omadm.MOManager.TAG_IPProtocol;
17import static com.android.hotspot2.omadm.MOManager.TAG_MaximumBSSLoadValue;
18import static com.android.hotspot2.omadm.MOManager.TAG_MinBackhaulThreshold;
19import static com.android.hotspot2.omadm.MOManager.TAG_NetworkType;
20import static com.android.hotspot2.omadm.MOManager.TAG_PolicyUpdate;
21import static com.android.hotspot2.omadm.MOManager.TAG_PortNumber;
22import static com.android.hotspot2.omadm.MOManager.TAG_PreferredRoamingPartnerList;
23import static com.android.hotspot2.omadm.MOManager.TAG_Priority;
24import static com.android.hotspot2.omadm.MOManager.TAG_RequiredProtoPortTuple;
25import static com.android.hotspot2.omadm.MOManager.TAG_SPExclusionList;
26import static com.android.hotspot2.omadm.MOManager.TAG_SSID;
27import static com.android.hotspot2.omadm.MOManager.TAG_ULBandwidth;
28
29public 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
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
109    public List<PreferredRoamingPartner> getPreferredRoamingPartners() {
110        return mPreferredRoamingPartners;
111    }
112
113    public List<MinBackhaul> getMinBackhaulThresholds() {
114        return mMinBackhaulThresholds;
115    }
116
117    public UpdateInfo getPolicyUpdate() {
118        return mPolicyUpdate;
119    }
120
121    public List<String> getSPExclusionList() {
122        return mSPExclusionList;
123    }
124
125    public Map<Integer, List<Integer>> getRequiredProtos() {
126        return mRequiredProtos;
127    }
128
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
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
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
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
175        public String toString() {
176            return "MinBackhaul{" +
177                    "home=" + mHome +
178                    ", DL=" + mDL +
179                    ", UL=" + mUL +
180                    '}';
181        }
182    }
183
184    @Override
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