// Path: src/main/java/khs/trouble/util/FormatUrl.java
// public class FormatUrl {
// 	
// 	   public static String url(String route, boolean https ) {
// 		   return https ? "https" : "http" + "://"+ route;
// 	   }
// 
// }


import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import khs.trouble.util.FormatUrl;
/*
 * Copyright 2015 Keyhole Software LLC.
 *
 *    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 khs.trouble.service.impl;




@Service
public class TroubleService {

	private Logger LOG = LoggerFactory.getLogger(TroubleService.class.getName());

	@Autowired
	DiscoveryClient discoveryClient;

	@Autowired
	EventService eventService;

	@Value("${trouble.token}")
	String token;

	@Value("${trouble.timeout:300000}")
	Long timeout;

	@Value("${trouble.blocking-threads:200}")
	Integer threads;

	@Value("${trouble.ssl:false}")
	Boolean ssl;

	@Value("${eureka.client.registry-fetch-interval-seconds}")
	private Long monitorInterval;

	public String randomKill(String ltoken) {
		String serviceName = randomService();
		eventService.randomKilled(serviceName);
		return kill(serviceName, "", ltoken);
	}

	public String kill(String serviceName, String instanceId, String ltoken) {
		if (token != ltoken) {
			throw new RuntimeException("Invalid Access Token");
		}

// Path: src/main/java/khs/trouble/util/FormatUrl.java
// public class FormatUrl {
// 	
// 	   public static String url(String route, boolean https ) {
// 		   return https ? "https" : "http" + "://"+ route;
// 	   }
// 
// }


// Path: src/main/java/khs/trouble/service/impl/TroubleService.java
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import khs.trouble.util.FormatUrl;

/*
 * Copyright 2015 Keyhole Software LLC.
 *
 *    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 khs.trouble.service.impl;




@Service
public class TroubleService {

	private Logger LOG = LoggerFactory.getLogger(TroubleService.class.getName());

	@Autowired
	DiscoveryClient discoveryClient;

	@Autowired
	EventService eventService;

	@Value("${trouble.token}")
	String token;

	@Value("${trouble.timeout:300000}")
	Long timeout;

	@Value("${trouble.blocking-threads:200}")
	Integer threads;

	@Value("${trouble.ssl:false}")
	Boolean ssl;

	@Value("${eureka.client.registry-fetch-interval-seconds}")
	private Long monitorInterval;

	public String randomKill(String ltoken) {
		String serviceName = randomService();
		eventService.randomKilled(serviceName);
		return kill(serviceName, "", ltoken);
	}

	public String kill(String serviceName, String instanceId, String ltoken) {
		if (token != ltoken) {
			throw new RuntimeException("Invalid Access Token");
		}

		String url = FormatUrl.url(serviceInstanceURL(serviceName, instanceId) + "trouble/kill", ssl);