1 /*
2  * Copyright (C) 2018 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 com.android.server.wifi.hotspot2.soap.command;
18 
19 import android.annotation.NonNull;
20 import android.text.TextUtils;
21 import android.util.Log;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 import org.ksoap2.serialization.PropertyInfo;
26 import org.ksoap2.serialization.SoapPrimitive;
27 
28 import java.util.Objects;
29 
30 /**
31  * Represents PPS (PerProviderSubscription) MO (Management Object) defined by SPP (Subscription
32  * Provisioning Protocol).
33  */
34 public class PpsMoData implements SppCommand.SppCommandData {
35     @VisibleForTesting
36     public static final String ADD_MO_COMMAND = "addMO";
37     @VisibleForTesting
38     public static final String ATTRIBUTE_MANAGEMENT_TREE_URI = "managementTreeURI";
39     @VisibleForTesting
40     public static final String ATTRIBUTE_MO_URN = "moURN";
41 
42     private static final String TAG = "PasspointPpsMoData";
43     private final String mBaseUri;
44     private final String mUrn;
45     private final String mPpsMoTree;
46 
PpsMoData(String baseUri, String urn, String ppsMoTree)47     private PpsMoData(String baseUri, String urn, String ppsMoTree) {
48         mBaseUri = baseUri;
49         mUrn = urn;
50         mPpsMoTree = ppsMoTree;
51     }
52 
53     /**
54      * Create an instance of {@link PpsMoData}
55      *
56      * @param command command message embedded in SOAP sppPostDevDataResponse.
57      * @return instance of {@link PpsMoData}, {@code null} in any failure.
58      */
createInstance(@onNull PropertyInfo command)59     public static PpsMoData createInstance(@NonNull PropertyInfo command) {
60         if (command == null || command.getValue() == null) {
61             Log.e(TAG, "command message is null");
62             return null;
63         }
64 
65         if (!TextUtils.equals(command.getName(), ADD_MO_COMMAND)) {
66             Log.e(TAG, "the response is not for addMO command");
67             return null;
68         }
69 
70         if (!(command.getValue() instanceof SoapPrimitive)) {
71             Log.e(TAG, "the addMO element is not valid format");
72             return null;
73         }
74 
75         SoapPrimitive soapObject = (SoapPrimitive) command.getValue();
76         if (!soapObject.hasAttribute(ATTRIBUTE_MANAGEMENT_TREE_URI)) {
77             Log.e(TAG, "managementTreeURI Attribute is missing");
78             return null;
79         }
80 
81         if (!soapObject.hasAttribute(ATTRIBUTE_MO_URN)) {
82             Log.e(TAG, "moURN Attribute is missing");
83             return null;
84         }
85 
86         if (soapObject.getValue() == null) {
87             Log.e(TAG, "PPSMO Tree is missing");
88             return null;
89         }
90 
91         return new PpsMoData(
92                 (String) soapObject.getAttributeSafelyAsString(ATTRIBUTE_MANAGEMENT_TREE_URI),
93                 (String) soapObject.getAttributeSafelyAsString(ATTRIBUTE_MO_URN),
94                 soapObject.getValue().toString());
95     }
96 
97     /**
98      * Get PPS (PerProviderSubscription) MO (Management Object) with XML format.
99      *
100      * @return PPS MO Tree
101      */
getPpsMoTree()102     public String getPpsMoTree() {
103         return mPpsMoTree;
104     }
105 
106     @Override
equals(Object thatObject)107     public boolean equals(Object thatObject) {
108         if (this == thatObject) return true;
109         if (thatObject == null) return false;
110         if (!(thatObject instanceof PpsMoData)) return false;
111         PpsMoData ppsMoData = (PpsMoData) thatObject;
112         return TextUtils.equals(mBaseUri, ppsMoData.mBaseUri)
113                 && TextUtils.equals(mUrn, ppsMoData.mUrn)
114                 && TextUtils.equals(mPpsMoTree, ppsMoData.mPpsMoTree);
115     }
116 
117     @Override
hashCode()118     public int hashCode() {
119         return Objects.hash(mBaseUri, mUrn, mPpsMoTree);
120     }
121 
122     @Override
toString()123     public String toString() {
124         return "PpsMoData{Base URI: " + mBaseUri + ", MOURN: " + mUrn + ", PPS MO: " + mPpsMoTree
125                 + "}";
126     }
127 }
128