// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.cloud.consoleproxy;

import java.util.List;


import org.apache.log4j.Logger;

import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.info.ConsoleProxyInfo;
import com.cloud.vm.UserVmVO;

public class AgentBasedStandaloneConsoleProxyManager extends AgentBasedConsoleProxyManager {
    private static final Logger s_logger = Logger.getLogger(AgentBasedStandaloneConsoleProxyManager.class);

    @Override
    public ConsoleProxyInfo assignProxy(long dataCenterId, long userVmId) {
        UserVmVO userVm = _userVmDao.findById(userVmId);
        if (userVm == null) {
            s_logger.warn("User VM " + userVmId + " no longer exists, return a null proxy for user vm:" + userVmId);
            return null;
        }

        HostVO host = findHost(userVm);
        if (host != null) {
            HostVO allocatedHost = null;
            /*Is there a consoleproxy agent running on the same machine?*/
            List<HostVO> hosts = _hostDao.listAllIncludingRemoved();
            for (HostVO hv : hosts) {
                if (hv.getType() == Host.Type.ConsoleProxy && hv.getPublicIpAddress().equalsIgnoreCase(host.getPublicIpAddress())) {
                    allocatedHost = hv;
                    break;
                }
            }
            if (allocatedHost == null) {
                /*Is there a consoleproxy agent running in the same pod?*/
                for (HostVO hv : hosts) {
                    if (hv.getType() == Host.Type.ConsoleProxy && hv.getPodId().equals(host.getPodId())) {
                        allocatedHost = hv;
                        break;
                    }
                }
            }
            if (allocatedHost == null) {
                if (s_logger.isDebugEnabled())
                    s_logger.debug("Failed to find a console proxy at host: " + host.getName() + " and in the pod: " + host.getPodId() + " to user vm " + userVmId);
                return null;
            }
            if (s_logger.isDebugEnabled())
                s_logger.debug("Assign standalone console proxy running at " + allocatedHost.getName() + " to user vm " + userVmId + " with public IP " +
                    allocatedHost.getPublicIpAddress());

            // only private IP, public IP, host id have meaningful values, rest of all are place-holder values
            String publicIp = allocatedHost.getPublicIpAddress();
            if (publicIp == null) {
                if (s_logger.isDebugEnabled())
                    s_logger.debug("Host " + allocatedHost.getName() + "/" + allocatedHost.getPrivateIpAddress() +
                        " does not have public interface, we will return its private IP for cosole proxy.");
                publicIp = allocatedHost.getPrivateIpAddress();
            }

            int urlPort = _consoleProxyUrlPort;
            if (allocatedHost.getProxyPort() != null && allocatedHost.getProxyPort().intValue() > 0)
                urlPort = allocatedHost.getProxyPort().intValue();

            return new ConsoleProxyInfo(_sslEnabled, publicIp, _consoleProxyPort, urlPort, _consoleProxyUrlDomain);
        } else {
            s_logger.warn("Host that VM is running is no longer available, console access to VM " + userVmId + " will be temporarily unavailable.");
        }
        return null;
    }
}

@@ -851,17 +851,16 @@
 .List;%0A%0A
-%0A
 import o
@@ -1013,16 +1013,332 @@
 rVmVO;%0A%0A
+/**%0A * This class is intended to replace the use of console proxy VMs managed by the Apache CloudStack (ACS)%0A * to non ACS console proxy services. The documentation that describe its use and requirements can be found in %3Ca href=%22https://cwiki.apache.org/confluence/display/CLOUDSTACK/QuickCloud%22%3EQuickCloud%3C/a%3E.%0A */%0A
 public c
@@ -2857,32 +2857,34 @@
 sDebugEnabled())
+ %7B
 %0A               
@@ -3025,32 +3025,50 @@
  %22 + userVmId);%0A
+                %7D%0A
                 
@@ -3128,32 +3128,34 @@
 sDebugEnabled())
+ %7B
 %0A               
@@ -3290,18 +3290,16 @@
 lic IP %22
- +
 %0A       
@@ -3302,32 +3302,38 @@
                 
+     +
  allocatedHost.g
@@ -3353,16 +3353,30 @@
 ress());
+%0A            %7D
 %0A%0A      
@@ -3628,16 +3628,18 @@
 abled())
+ %7B
 %0A       
@@ -3743,18 +3743,16 @@
 ddress()
- +
 %0A       
@@ -3767,16 +3767,22 @@
         
+     +
  %22 does 
@@ -3855,24 +3855,42 @@
 e proxy.%22);%0A
+                %7D%0A
             
@@ -4104,16 +4104,18 @@
 e() %3E 0)
+ %7B
 %0A       
@@ -4173,16 +4173,30 @@
 Value();
+%0A            %7D
 %0A%0A      

Add Javadoc to AgentBasedStandaloneConsoleProxyManager