// Path: discovery-client/src/main/java/com/comcast/tvx/cloud/discovery/EasyDiscoveryBuilder.java
// public class EasyDiscoveryBuilder<T> {
// 
//     private CuratorFramework        client;
//     private String                  basePath;
//     private InstanceSerializer<T>   serializer;
//     private ServiceInstance<T>      thisInstance;
// 
//     /**
//      * Return a new builder. The builder will be defaulted with a {@link JsonInstanceSerializer}.
//      *
//      * @param payloadClass the class of the payload of your service instance (you can use {@link Void}
//      * if your instances don't need a payload)
//      * @return new builder
//      */
//     public static<T> EasyDiscoveryBuilder<T>     builder(Class<T> payloadClass)
//     {
//         return new EasyDiscoveryBuilder<T>(payloadClass).serializer(new JsonInstanceSerializer<T>(payloadClass));
//     }
// 
//     /**
//      * Build a new service discovery with the currently set values
//      *
//      * @return new service discovery
//      */
//     public ServiceDiscovery<T> build() {
//         return new EasyDiscoveryImpl<T>(client, basePath, serializer, thisInstance);
//     }
// 
//     /**
//      * Required - set the client to use
//      *
//      * @param client client
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   client(CuratorFramework client)
//     {
//         this.client = client;
//         return this;
//     }
// 
//     /**
//      * Required - set the base path to store in ZK
//      *
//      * @param basePath base path
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   basePath(String basePath)
//     {
//         this.basePath = basePath;
//         return this;
//     }
// 
//     /**
//      * optional - change the serializer used (the default is {@link JsonInstanceSerializer}
//      *
//      * @param serializer the serializer
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   serializer(InstanceSerializer<T> serializer)
//     {
//         this.serializer = serializer;
//         return this;
//     }
// 
//     /**
//      * Optional - instance that represents the service that is running. The instance will get auto-registered
//      *
//      * @param thisInstance initial instance
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   thisInstance(ServiceInstance<T> thisInstance)
//     {
//         this.thisInstance = thisInstance;
//         return this;
//     }
// 
//     EasyDiscoveryBuilder(Class<T> payloadClass)
//     {
//     }
// 
// }


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.comcast.tvx.cloud.discovery.EasyDiscoveryBuilder;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.EnsurePath;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceInstanceBuilder;
import org.apache.curator.x.discovery.ServiceType;

        ServiceInstanceBuilder<MetaData> builder = ServiceInstance.builder();

        // Address is optional.  The Curator library will automatically use the IP from the first
        // ethernet device
        String registerAddress = (serviceAddress == null) ? builder.build().getAddress() : serviceAddress;

        MetaData metadata = new MetaData(UUID.randomUUID(), registerAddress, servicePort, serviceName);
        metadata.setParameters(parameters);

        builder.name(serviceName).payload(metadata).id(registerAddress + ":" +
                String.valueOf(servicePort)).serviceType(ServiceType.DYNAMIC).address(registerAddress).port(servicePort);

        return builder.build();
    }

    /**
     * Gets the discovery.
     *
     * @param   basePath          - Registration path
     * @param   curatorFramework  - Curator
     * @return  the discovery
     * @throws  Exception  the exception
     */
    protected static ServiceDiscovery<MetaData> getDiscovery(String basePath, CuratorFramework curatorFramework)
        throws Exception {

        new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());

        ServiceDiscovery<MetaData> result =
// Path: discovery-client/src/main/java/com/comcast/tvx/cloud/discovery/EasyDiscoveryBuilder.java
// public class EasyDiscoveryBuilder<T> {
// 
//     private CuratorFramework        client;
//     private String                  basePath;
//     private InstanceSerializer<T>   serializer;
//     private ServiceInstance<T>      thisInstance;
// 
//     /**
//      * Return a new builder. The builder will be defaulted with a {@link JsonInstanceSerializer}.
//      *
//      * @param payloadClass the class of the payload of your service instance (you can use {@link Void}
//      * if your instances don't need a payload)
//      * @return new builder
//      */
//     public static<T> EasyDiscoveryBuilder<T>     builder(Class<T> payloadClass)
//     {
//         return new EasyDiscoveryBuilder<T>(payloadClass).serializer(new JsonInstanceSerializer<T>(payloadClass));
//     }
// 
//     /**
//      * Build a new service discovery with the currently set values
//      *
//      * @return new service discovery
//      */
//     public ServiceDiscovery<T> build() {
//         return new EasyDiscoveryImpl<T>(client, basePath, serializer, thisInstance);
//     }
// 
//     /**
//      * Required - set the client to use
//      *
//      * @param client client
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   client(CuratorFramework client)
//     {
//         this.client = client;
//         return this;
//     }
// 
//     /**
//      * Required - set the base path to store in ZK
//      *
//      * @param basePath base path
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   basePath(String basePath)
//     {
//         this.basePath = basePath;
//         return this;
//     }
// 
//     /**
//      * optional - change the serializer used (the default is {@link JsonInstanceSerializer}
//      *
//      * @param serializer the serializer
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   serializer(InstanceSerializer<T> serializer)
//     {
//         this.serializer = serializer;
//         return this;
//     }
// 
//     /**
//      * Optional - instance that represents the service that is running. The instance will get auto-registered
//      *
//      * @param thisInstance initial instance
//      * @return this
//      */
//     public EasyDiscoveryBuilder<T>   thisInstance(ServiceInstance<T> thisInstance)
//     {
//         this.thisInstance = thisInstance;
//         return this;
//     }
// 
//     EasyDiscoveryBuilder(Class<T> payloadClass)
//     {
//     }
// 
// }


// Path: discovery-client/src/main/java/com/comcast/tvx/cloud/ServiceUtil.java
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.comcast.tvx.cloud.discovery.EasyDiscoveryBuilder;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.EnsurePath;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceInstanceBuilder;
import org.apache.curator.x.discovery.ServiceType;


        ServiceInstanceBuilder<MetaData> builder = ServiceInstance.builder();

        // Address is optional.  The Curator library will automatically use the IP from the first
        // ethernet device
        String registerAddress = (serviceAddress == null) ? builder.build().getAddress() : serviceAddress;

        MetaData metadata = new MetaData(UUID.randomUUID(), registerAddress, servicePort, serviceName);
        metadata.setParameters(parameters);

        builder.name(serviceName).payload(metadata).id(registerAddress + ":" +
                String.valueOf(servicePort)).serviceType(ServiceType.DYNAMIC).address(registerAddress).port(servicePort);

        return builder.build();
    }

    /**
     * Gets the discovery.
     *
     * @param   basePath          - Registration path
     * @param   curatorFramework  - Curator
     * @return  the discovery
     * @throws  Exception  the exception
     */
    protected static ServiceDiscovery<MetaData> getDiscovery(String basePath, CuratorFramework curatorFramework)
        throws Exception {

        new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());

        ServiceDiscovery<MetaData> result =
            EasyDiscoveryBuilder.builder(MetaData.class).basePath(basePath).client(curatorFramework).build();