| /** |
| ****************************************************************************** |
| * Copyright © 2018 PTA GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| ****************************************************************************** |
| */ |
| |
| package org.eclipse.openK.core.controller; |
| |
| import org.apache.log4j.Logger; |
| import org.eclipse.jetty.http.HttpStatus; |
| import org.eclipse.openK.api.VersionInfo; |
| import org.eclipse.openK.api.ServiceDistributionCluster; |
| import org.eclipse.openK.core.common.Globals; |
| import org.eclipse.openK.core.communication.RestServiceWrapper; |
| import org.eclipse.openK.core.exceptions.HttpStatusException; |
| |
| import java.util.Arrays; |
| import java.util.List; |
| import java.util.Optional; |
| import java.util.stream.Collectors; |
| |
| public class BackendController { |
| private static final Logger logger = Logger.getLogger(BackendController.class); |
| |
| public ServiceDistributionCluster readServerDistribution(String cluster) throws HttpStatusException { |
| ServiceDistributionCluster[] dcs = ServicesConfigCache.getInstance().getCache(); |
| |
| for (ServiceDistributionCluster item : dcs) { |
| if (item.getClustername().equalsIgnoreCase(cluster) && item.isActive()) { |
| return removeInactiveItems(item); |
| } |
| } |
| throw new HttpStatusException(HttpStatus.NOT_FOUND_404); |
| } |
| |
| public VersionInfo getVersionInfo(String version ) { |
| VersionInfo vi = new VersionInfo(); |
| vi.setBackendVersion( version ); |
| return vi; |
| } |
| |
| public String getServiceHealthState(String clustername, String servicename) throws HttpStatusException { |
| |
| ServiceDistributionCluster.ServiceDistribution dist = findDistribution(clustername, servicename); |
| RestServiceWrapper w = new RestServiceWrapper(false); |
| return w.performGetRequest(dist.getProtocol() + "://" + dist.getHost() + ":" |
| + dist.getPortHealth() + "/" |
| + Globals.HEALTH_CHECK_ADD_PATH); |
| } |
| |
| private ServiceDistributionCluster.ServiceDistribution findDistribution(String clustername, |
| String servicename) throws HttpStatusException { |
| ServiceDistributionCluster cluster = getCluster(ServicesConfigCache.getInstance().getCache(), clustername); |
| |
| Optional<ServiceDistributionCluster.ServiceDistribution> dist = Arrays.stream(cluster.getDistributions()) |
| .filter(e -> e.isActive() && e.getName().equals(servicename)) |
| .findFirst(); |
| |
| if(dist.isPresent()) { |
| return dist.get(); |
| } |
| else { |
| throw new HttpStatusException(HttpStatus.NOT_FOUND_404); |
| } |
| } |
| |
| private ServiceDistributionCluster getCluster(ServiceDistributionCluster[] clusterArray, String clustername) throws HttpStatusException { |
| List<ServiceDistributionCluster> clusterList = Arrays.asList(clusterArray); |
| Optional<ServiceDistributionCluster> ret = clusterList.stream() |
| .filter(c -> c.getClustername().equals(clustername)).findFirst(); |
| |
| if (ret.isPresent()) { |
| return ret.get(); |
| } else { |
| logger.info("Could not find a cluster with the name: " + clustername); |
| throw new HttpStatusException(HttpStatus.NOT_FOUND_404); |
| } |
| } |
| |
| private ServiceDistributionCluster removeInactiveItems( ServiceDistributionCluster cluster ) { |
| List<ServiceDistributionCluster.ServiceDistribution> dlist = Arrays.asList(cluster.getDistributions()); |
| dlist = dlist.stream().filter(ServiceDistributionCluster.ServiceDistribution::isActive).collect(Collectors.toList()); |
| ServiceDistributionCluster.ServiceDistribution[] distArr |
| = new ServiceDistributionCluster.ServiceDistribution[dlist.size()]; |
| cluster.setDistributions(dlist.toArray(distArr)); |
| return cluster; |
| } |
| |
| |
| } |