/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.azure;

import com.google.common.hash.Hashing;
import com.microsoft.azure.serializer.AzureJacksonAdapter;
import com.microsoft.rest.RestClient;
import com.microsoft.rest.ServiceClient;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

import java.net.InetAddress;
import java.net.NetworkInterface;

/**
 * ServiceClient is the abstraction for accessing REST operations and their payload data types.
 */
public abstract class AzureServiceClient extends ServiceClient {
    protected AzureServiceClient(String baseUrl, ServiceClientCredentials credentials) {
        this(baseUrl, credentials, new OkHttpClient.Builder(), new Retrofit.Builder());
    }

    /**
     * Initializes a new instance of the ServiceClient class.
     *
     * @param baseUrl the service base uri
     * @param clientBuilder the http client builder
     * @param restBuilder the retrofit rest client builder
     */
    protected AzureServiceClient(String baseUrl, ServiceClientCredentials credentials, OkHttpClient.Builder clientBuilder, Retrofit.Builder restBuilder) {
        this(new RestClient.Builder(clientBuilder, restBuilder)
                .withBaseUrl(baseUrl)
                .withCredentials(credentials)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .build());
    }

    /**
     * Initializes a new instance of the ServiceClient class.
     *
     * @param restClient the REST client
     */
    protected AzureServiceClient(RestClient restClient) {
        super(restClient);
    }

    /**
     * The default User-Agent header. Override this method to override the user agent.
     *
     * @return the user agent string.
     */
    public String userAgent() {
        return String.format("Azure-SDK-For-Java/%s OS:%s MacAddressHash:%s",
                getClass().getPackage().getImplementationVersion(),
                OS,
                MAC_ADDRESS_HASH);
    }

    private static final String MAC_ADDRESS_HASH;
    private static final String OS;

    static {
        OS = System.getProperty("os.name") + "/" + System.getProperty("os.version");
        String macAddress;
        try {
            macAddress = Hashing.sha256().hashBytes(NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress()).toString();
        } catch (Exception e) {
            macAddress = "Unknown";
        }
        MAC_ADDRESS_HASH = macAddress;
    }
}

@@ -510,53 +510,54 @@
 net.
-InetAddress;%0Aimport java.net.NetworkInterface
+NetworkInterface;%0Aimport java.util.Enumeration
 ;%0A%0A/
@@ -2442,24 +2442,36 @@
 g macAddress
+ = %22Unknown%22
 ;%0A        tr
@@ -2490,211 +2490,527 @@
     
-macAddress = Hashing.sha256().hashBytes(NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress()).toString();%0A        %7D catch (Exception e) %7B%0A           
+Enumeration%3CNetworkInterface%3E networks = NetworkInterface.getNetworkInterfaces();%0A            while (networks.hasMoreElements()) %7B%0A                NetworkInterface network = networks.nextElement();%0A                byte%5B%5D mac = network.getHardwareAddress();%0A%0A                if (mac != null) %7B%0A                    macAddress = Hashing.sha256().hashBytes(mac).toString();%0A                    break;%0A                %7D%0A            %7D%0A        %7D catch (Throwable t) %7B%0A            // It's okay ignore
  mac
-A
+ a
 ddress 
-= %22Unknown%22;
+hash telemetry
 %0A   

Fix Mac address hash retrieval logic (#168)