summaryrefslogtreecommitdiff
path: root/service/java/com/android/server/wifi/hotspot2/soap/command/PpsMoData.java
blob: 9c44a10a5d188e0f06ba0342f101cc97b2e34794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi.hotspot2.soap.command;

import android.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapPrimitive;

import java.util.Objects;

/**
 * Represents PPS (PerProviderSubscription) MO (Management Object) defined by SPP (Subscription
 * Provisioning Protocol).
 */
public class PpsMoData implements SppCommand.SppCommandData {
    @VisibleForTesting
    public static final String ADD_MO_COMMAND = "addMO";
    @VisibleForTesting
    public static final String ATTRIBUTE_MANAGEMENT_TREE_URI = "managementTreeURI";
    @VisibleForTesting
    public static final String ATTRIBUTE_MO_URN = "moURN";

    private static final String TAG = "PasspointPpsMoData";
    private final String mBaseUri;
    private final String mUrn;
    private final String mPpsMoTree;

    private PpsMoData(String baseUri, String urn, String ppsMoTree) {
        mBaseUri = baseUri;
        mUrn = urn;
        mPpsMoTree = ppsMoTree;
    }

    /**
     * Create an instance of {@link PpsMoData}
     *
     * @param command command message embedded in SOAP sppPostDevDataResponse.
     * @return instance of {@link PpsMoData}, {@code null} in any failure.
     */
    public static PpsMoData createInstance(@NonNull PropertyInfo command) {
        if (command == null || command.getValue() == null) {
            Log.e(TAG, "command message is null");
            return null;
        }

        if (!TextUtils.equals(command.getName(), ADD_MO_COMMAND)) {
            Log.e(TAG, "the response is not for addMO command");
            return null;
        }

        if (!(command.getValue() instanceof SoapPrimitive)) {
            Log.e(TAG, "the addMO element is not valid format");
            return null;
        }

        SoapPrimitive soapObject = (SoapPrimitive) command.getValue();
        if (!soapObject.hasAttribute(ATTRIBUTE_MANAGEMENT_TREE_URI)) {
            Log.e(TAG, "managementTreeURI Attribute is missing");
            return null;
        }

        if (!soapObject.hasAttribute(ATTRIBUTE_MO_URN)) {
            Log.e(TAG, "moURN Attribute is missing");
            return null;
        }

        if (soapObject.getValue() == null) {
            Log.e(TAG, "PPSMO Tree is missing");
            return null;
        }

        return new PpsMoData(
                (String) soapObject.getAttributeSafelyAsString(ATTRIBUTE_MANAGEMENT_TREE_URI),
                (String) soapObject.getAttributeSafelyAsString(ATTRIBUTE_MO_URN),
                soapObject.getValue().toString());
    }

    /**
     * Get PPS (PerProviderSubscription) MO (Management Object) with XML format.
     *
     * @return PPS MO Tree
     */
    public String getPpsMoTree() {
        return mPpsMoTree;
    }

    @Override
    public boolean equals(Object thatObject) {
        if (this == thatObject) return true;
        if (thatObject == null) return false;
        if (!(thatObject instanceof PpsMoData)) return false;
        PpsMoData ppsMoData = (PpsMoData) thatObject;
        return TextUtils.equals(mBaseUri, ppsMoData.mBaseUri)
                && TextUtils.equals(mUrn, ppsMoData.mUrn)
                && TextUtils.equals(mPpsMoTree, ppsMoData.mPpsMoTree);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mBaseUri, mUrn, mPpsMoTree);
    }

    @Override
    public String toString() {
        return "PpsMoData{Base URI: " + mBaseUri + ", MOURN: " + mUrn + ", PPS MO: " + mPpsMoTree
                + "}";
    }
}