package com.orange.oss.cloudfoundry.cscpi;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class UserDataGeneratorImpl implements UserDataGenerator {

	private static Logger logger=LoggerFactory.getLogger(UserDataGeneratorImpl.class.getName());
	
	/**
	 * see https://github.com/cloudfoundry/bosh-agent/blob/585d2cc3a47129aa875738f09a26101ec6e0b1d1/infrastructure/http_metadata_service.go
	 * for bosh agent http user data expectations
	 * @author pierre
	 *
	 */
	public static class Server {
		Server(String name){
			this.Name=name;
		}
		public String Name;
	}
	
	public static class Registry {
		Registry(String endpoint){
			this.Endpoint=endpoint;
		}
		
		public String Endpoint;
	}
	
	public static class DNS {
		DNS(String nameServer){
			this.NameServer=nameServer;
		}
		public String NameServer;
	}
	
	
	public class UserData{
		Server server;
		Registry registry;
		DNS dns;
	}
	
	
	@Override
	public String vmMetaData(){
		
		UserData datas=new UserData();
		//compose the user data from cpi create vm call
		datas.server=new Server("nomduserver");
		datas.registry=new Registry("http://xx.xx.xx:8080");
		datas.dns=new DNS("10.0.0.1");
		
		//serialize to json
		ObjectMapper mapper = new ObjectMapper();
		mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
		String userData;
		try {
			userData = mapper.writeValueAsString(datas);
		} catch (JsonProcessingException e) {
			throw new IllegalArgumentException("Cant serialize JSON userData", e);
		}
		
		logger.info("generated user data : \n{}",userData);
		
		//base64 encore resulting String
        final byte[] bytes = userData.getBytes(StandardCharsets.UTF_8);
        final String encoded = Base64.getEncoder().encodeToString(bytes);
        logger.debug(userData + " => " + encoded);
		return encoded;
	}
	
}

@@ -53,61 +53,54 @@
 ava.
-io.ByteArrayOutputStream;%0Aimport java.io.OutputStream
+net.MalformedURLException;%0Aimport java.net.URL
 ;%0Aim
@@ -221,16 +221,115 @@
 Factory;
+%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.util.Assert;
 %0A%0Aimport
@@ -719,16 +719,73 @@
 Name());
+%0A%0A%09%0A%09@Value(%22$%7Bregistry.endpoint%7D%22)%0A%09String endpoint;%0A%09%0A%09
 %0A%09%0A%09/**%0A
@@ -1592,24 +1592,296 @@
 duserver%22);%0A
+%09%09%0A%09%09try %7B%0A%09%09%09URL url=new URL(this.endpoint);%0A%09%09%7D catch (MalformedURLException e) %7B%0A%09%09%09logger.error(%22Registry endpoint is malformed %7B%7D : %7B%7D%22,this.endpoint,e.getMessage());%0A%09%09%09throw new IllegalArgumentException(%22Reigstry Endpoint is incorrect : %22+this.endpoint, e);%0A%09%09%7D%0A%09%09%0A
 %09%09datas.regi
@@ -1902,30 +1902,21 @@
 try(
-%22http://xx.xx.xx:8080%22
+this.endpoint
 );%0A%09

add URL format control for registry