/*******************************************************************************
 *                                                                              
 *  Copyright FUJITSU LIMITED 2016                                        
 *                                                                              
 *  Creation Date: 2013-11-28                                                      
 *                                                                              
 *******************************************************************************/

package org.oscm.app.openstack;

import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.oscm.app.common.i18n.Messages;
import org.oscm.app.openstack.exceptions.HeatException;
import org.oscm.app.v1_0.exceptions.APPlatformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Keystone is the identity service used by OpenStack for authentication and
 * authorization.
 */
public class KeystoneClient {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(KeystoneClient.class);

    final static String TYPE_HEAT = "orchestration";

    final static String TYPE_NOVA = "compute";

    private final OpenStackConnection connection;

    /**
     *
     * @param connection
     *            The connection that needs to be authenticated
     */
    public KeystoneClient(OpenStackConnection connection) {
        this.connection = connection;
    }

    /**
     * Authenticate the connection that was given in the constructor for V3 API.
     *
     * @param user
     * @param password
     * @param domainName
     * @param tenantId
     *
     * @throws HeatException
     */
    public void authenticate(String user, String password, String domainName,
            String tenantId) throws HeatException, APPlatformException {
        LOGGER.debug("KeystoneClient.authenticate() user: " + user + "  domain:"
                + domainName + " tenant ID:" + tenantId + "  endpoint: "
                + connection.getKeystoneEndpoint());
        String uri = connection.getKeystoneEndpoint() + "/tokens";

        JSONObject request = new JSONObject();
        JSONObject auth = new JSONObject();
        JSONObject identity = new JSONObject();
        JSONObject userName = new JSONObject();
        JSONObject passwordInfo = new JSONObject();
        JSONObject projectId = new JSONObject();
        JSONObject project = new JSONObject();
        JSONArray methodArray = new JSONArray();
        try {
            if(domainName.trim().length() != 0){
              JSONObject domain = new JSONObject();
              domain.put("name", domainName);
              userName.put("domain", domain);
            }
            userName.put("name", user);
            userName.put("password", password);
            methodArray.put("password");
            passwordInfo.put("user", userName);
            identity.put("password", passwordInfo);
            identity.put("methods", methodArray);
            auth.put("identity", identity);
            projectId.put("id", tenantId);
            project.put("project", projectId);
            auth.put("scope", project);
            request.put("auth", auth);
        } catch (JSONException e) {
            // this can basically not happen with string parameters
            throw new RuntimeException(e);
        }
        LOGGER.debug("URL is " + uri + " request is " + request.toString());
        RESTResponse response = connection.processRequest(uri, "POST",
                request.toString());

        if (response.getResponseCode() != 201) {
            throw new RuntimeException(
                    "Failed to retrieve token for authentication, response code "
                            + response.getResponseCode());
        }

        String body = response.getResponseBody();
        String authToken = response.getToken();
        LOGGER.debug("body: " + body + " authToken: " + authToken);
        try {
            String heatEndpoint = null;
            String novaEndpoint = null;
            JSONObject jsonObj = new JSONObject(body);
            JSONObject token = jsonObj.getJSONObject("token");
            JSONArray catalog = token.getJSONArray("catalog");
            int catalogSize = catalog.length();
            for (int i = 0; i < catalogSize; i++) {
                JSONObject entry = catalog.getJSONObject(i);
                if (entry != null) {
                    String type = entry.getString("type");
                    if (TYPE_HEAT.equals(type)) {
                        JSONArray endpoints = entry.getJSONArray("endpoints");
                        int endpointSize = endpoints.length();
                        for (int j = 0; j < endpointSize; j++) {
                            JSONObject endpoint = endpoints.getJSONObject(j);
                            if (endpoint != null) {
                                String endpointUrl = endpoint.getString("url");
                                if (endpointUrl != null
                                        && endpointUrl.trim().length() > 0) {
                                    heatEndpoint = endpointUrl;
                                }
                            }
                        }
                    } else if (TYPE_NOVA.equals(type)) {
                        JSONArray endpoints = entry.getJSONArray("endpoints");
                        int endpointSize = endpoints.length();
                        for (int j = 0; j < endpointSize; j++) {
                            JSONObject endpoint = endpoints.getJSONObject(j);
                            if (endpoint != null) {
                                String endpointUrl = endpoint.getString("url");
                                if (endpointUrl != null
                                        && endpointUrl.trim().length() > 0) {
                                    novaEndpoint = endpointUrl;
                                }
                            }
                        }
                    }
                }
            }
            if (heatEndpoint == null) {
                LOGGER.error(
                        "KeystoneClient.authenticate() heat endpoint not defined");
                throw new APPlatformException(
                        Messages.getAll("error_missing_heat_endpoint"));
            } else {
                LOGGER.debug("KeystoneClient.authenticate() heat endpoint: "
                        + heatEndpoint);
            }
            if (novaEndpoint == null) {
                LOGGER.error(
                        "KeystoneClient.authenticate() nova endpoint not defined");
                throw new APPlatformException(
                        Messages.getAll("error_missing_nova_endpoint"));
            } else {
                LOGGER.debug("KeystoneClient.authenticate() nova endpoint: "
                        + novaEndpoint);
            }
            connection.useAuthentication(authToken);
            connection.setHeatEndpoint(heatEndpoint);
            connection.setNovaEndpoint(novaEndpoint);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -2628,16 +2628,17 @@
       if
+ 
 (domainN
@@ -2662,18 +2662,21 @@
 () != 0)
+ 
 %7B%0A
+  
         
@@ -2733,16 +2733,18 @@
         
+  
 domain.p
@@ -2763,24 +2763,26 @@
 omainName);%0A
+  
             
@@ -3482,85 +3482,8 @@
   %7D%0A
-        LOGGER.debug(%22URL is %22 + uri + %22 request is %22 + request.toString());%0A
     
@@ -3930,76 +3930,8 @@
 ();%0A
-        LOGGER.debug(%22body: %22 + body + %22 authToken: %22 + authToken);%0A
     

Remove password from debug log.