package org.gcube.dataanalysis.dataminer.poolmanager.clients;

import java.io.BufferedReader;
***REMOVED***
import java.io.InputStreamReader;
***REMOVED***
import java.util.LinkedList;
***REMOVED***

***REMOVED***
***REMOVED***
***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Cluster;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Host;
import org.gcube.dataanalysis.dataminer.poolmanager.util.CheckPermission;
***REMOVED***
import au.com.bytecode.opencsv.CSVReader;

public class HAProxy ***REMOVED***

	private CSVReader reader;

***REMOVED***	public Cluster getClusterByHProxy() throws IOException ***REMOVED***
***REMOVED***		Cluster cl = new Cluster();
***REMOVED***		String HProxy = ISClient.getHProxy();
***REMOVED***		URL stockURL = new URL("http:***REMOVED***data.d4science.org/Yk4zSFF6V3JOSytNd3JkRDlnRFpDUUR5TnRJZEw2QjRHbWJQNStIS0N6Yz0");
***REMOVED***		BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
***REMOVED***		reader = new CSVReader(in);
***REMOVED***		String[] nextLine;
***REMOVED***		while ((nextLine = reader.readNext()) != null) ***REMOVED***
***REMOVED***			***REMOVED*** rules to add
***REMOVED***			if (HProxy.contains(nextLine[0])) ***REMOVED***
***REMOVED***				cl.setName(nextLine[0]);
***REMOVED***		***REMOVED***
***REMOVED***	***REMOVED***
***REMOVED***		return cl;
***REMOVED***
***REMOVED******REMOVED***

	public Cluster MapCluster() throws IOException ***REMOVED***
		Cluster cl = new Cluster();
		String HProxy = ISClient.getHProxy();
		***REMOVED***Prod HAproxy
		if (HProxy.equals("dataminer-cluster1.d4science.org")) ***REMOVED***
			cl.setName("dataminer_cluster1");
	***REMOVED***
		if (HProxy.equals("dataminer-bigdata.d4science.org")) ***REMOVED***
			cl.setName("bigdata");
	***REMOVED***
		if (HProxy.equals("dataminer-cluster1.d4science.org")) ***REMOVED***
			cl.setName("dataminer_cluster1");
	***REMOVED***
		if (HProxy.equals("dataminer-cloud1.d4science.org")) ***REMOVED***
			cl.setName("dataminer_cloud1");
	***REMOVED***
		if (HProxy.equals("dataminer-prototypes.d4science.org")) ***REMOVED***
			cl.setName("prototypes");
	***REMOVED***
		if (HProxy.equals("dataminer.d4science.org")) ***REMOVED***
			cl.setName("gcubeapps");
	***REMOVED***
		if (HProxy.equals("dataminer-genericworkers.d4science.org")) ***REMOVED***
			cl.setName("genericworkers");
	***REMOVED***
		if (HProxy.equals("dataminer-genericworkers-proto.d4science.org")) ***REMOVED***
			cl.setName("genericworkers_proto");
	***REMOVED***
		***REMOVED***dev HAProxy
		if (HProxy.equals("dataminer-d-workers.d4science.org")||(HProxy.equals("dataminer-d-d4s.d4science.org"))) ***REMOVED***
			cl.setName("devnext_backend");
	***REMOVED***	
		***REMOVED***preProd HAProxy
***REMOVED***		if (HProxy.equals("dataminer1-pre.d4science.org")) ***REMOVED***
***REMOVED***			cl.setName("dataminer1-pre.d4science.org");
***REMOVED***	***REMOVED***
		return cl;

***REMOVED***
	
	
	public List<Host> listDataMinersByCluster(String targetVREToken,String targetVRE) throws IOException ***REMOVED***
		
		SecurityTokenProvider.instance.set(targetVREToken);
		ScopeProvider.instance.set(targetVRE);

		***REMOVED*** next op to use when Cluster info available in the IS
		***REMOVED*** Cluster cluster = this.getClusterByHProxy();
		Cluster cluster = this.MapCluster();
		List<Host> out = new LinkedList<Host>();
		Host a = new Host();

		***REMOVED***no proxy dataminer (preprod)
		if (cluster.getName() == null)***REMOVED***
			a.setName(ISClient.getHProxy());
			out.add(a);
	***REMOVED***
		
		***REMOVED*** if preprod, just one dm available
***REMOVED***		if (cluster.getName().equals("dataminer1-pre.d4science.org")) ***REMOVED***
***REMOVED***			a.setName("dataminer1-pre.d4science.org");
***REMOVED***			out.add(a);
		***REMOVED******REMOVED*** 
		else ***REMOVED***
			***REMOVED*** prod
			***REMOVED***URL stockURL = new
			***REMOVED***URL("http:***REMOVED***data.d4science.org/Yk4zSFF6V3JOSytNd3JkRDlnRFpDUUR5TnRJZEw2QjRHbWJQNStIS0N6Yz0");
			URL stockURL = new URL("http:***REMOVED***"+ ISClient.getHProxy() +":8880/;csv");
			***REMOVED***URL stockURL = new URL("http:***REMOVED***data.d4science.org/c29KTUluTkZnRlB0WXE5NVNaZnRoR0dtYThUSmNTVlhHbWJQNStIS0N6Yz0");
			***REMOVED***System.out.println(stockURL);
			***REMOVED*** dev
			***REMOVED***URL stockURL = new
			***REMOVED***URL("http:***REMOVED***data.d4science.org/c29KTUluTkZnRlB0WXE5NVNaZnRoR0dtYThUSmNTVlhHbWJQNStIS0N6Yz0");
			BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
			reader = new CSVReader(in, ',');
			String[] nextLine;
			while ((nextLine = reader.readNext()) != null) ***REMOVED***
				if (nextLine[1].equals("BACKEND") || (nextLine[1].equals("FRONTEND"))) ***REMOVED***
					continue;
			***REMOVED***
				if (nextLine[0].equals(cluster.getName())) ***REMOVED***
					Host b = new Host();
					b.setName(nextLine[1]);
					out.add(b);
					System.out.println(b.getFullyQualifiedName());
			***REMOVED***
		***REMOVED***
	***REMOVED***
		System.out.println(out);
		return out;

***REMOVED***

	public static void main(String[] args) throws ObjectNotFound, Exception ***REMOVED***
		HAProxy a = new HAProxy();
		***REMOVED***ScopeProvider.instance.set("/gcube/devNext/NextNext");
		***REMOVED***ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
		SecurityTokenProvider.instance.set("***REMOVED***");
		
		CheckPermission test = new CheckPermission();
		CheckPermission.apply("***REMOVED***", "/gcube/devNext/NextNext");
		
		
		***REMOVED***ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
		***REMOVED*** System.out.println(a.getHProxy());
		***REMOVED*** System.out.println(a.MapCluster());
		***REMOVED***System.out.println(a.listDataMinersByCluster("***REMOVED***","/gcube/devNext/NextNext"));
		***REMOVED*** System.out.println(a.listDataMinersByCluster());

		***REMOVED*** List<Dependency> list = new LinkedList<Dependency>();
		***REMOVED*** Dependency aa = new Dependency();
		***REMOVED*** aa.setName("testnunzio");
		***REMOVED*** aa.setType("cran:");
		***REMOVED*** list.add(aa);

		***REMOVED*** a.checkSVNdep();
		***REMOVED*** System.out.println(a.getDataminer("dataminer1-devnext.d4science.org").getDomain());
		***REMOVED*** System.out.println(a.listDataminersInVRE());
***REMOVED***
***REMOVED***
