package io.subutai.common.environment;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.subutai.hub.share.quota.ContainerCpuResource;
import io.subutai.hub.share.quota.ContainerDiskResource;
import io.subutai.hub.share.quota.ContainerQuota;
import io.subutai.hub.share.quota.ContainerRamResource;
import io.subutai.hub.share.quota.ContainerSize;
import io.subutai.hub.share.quota.Quota;
import io.subutai.hub.share.resource.ContainerResourceType;


@JsonIgnoreProperties( ignoreUnknown = true )
public class ContainerQuotaDto
{
    @JsonProperty( value = "containerSize" )
    private ContainerSize containerSize;

    @JsonProperty( value = "cpuQuota" )
    private String cpu;

    @JsonProperty( value = "ramQuota" )
    private String ram;

    @JsonProperty( value = "diskQuota" )
    private String disk;


    public ContainerQuotaDto( @JsonProperty( value = "containerSize" ) final ContainerSize containerSize,
                              @JsonProperty( value = "cpuQuota" ) final String cpu,
                              @JsonProperty( value = "ramQuota" ) final String ram,
                              @JsonProperty( value = "diskQuota" ) final String disk )
    {
        this.containerSize = containerSize;
        this.cpu = cpu;
        this.ram = ram;
        this.disk = disk;
    }


    public ContainerQuotaDto( final ContainerQuota quota )
    {
        this.containerSize = quota.getContainerSize();
        this.cpu = quota.get( ContainerResourceType.CPU ).getAsCpuResource().getWriteValue();
        this.ram = quota.get( ContainerResourceType.RAM ).getAsRamResource().getWriteValue();
        this.disk = quota.get( ContainerResourceType.DISK ).getAsDiskResource().getWriteValue();
    }


    public ContainerSize getContainerSize()
    {
        return containerSize;
    }


    public String getCpu()
    {
        return cpu;
    }


    public String getRam()
    {
        return ram;
    }


    public String getDisk()
    {
        return disk;
    }


    @JsonIgnore
    public ContainerQuota getContainerQuota()
    {
        ContainerQuota quota = new ContainerQuota( this.containerSize );
        if ( this.containerSize == ContainerSize.CUSTOM )
        {
            quota.add( new Quota( new ContainerCpuResource( this.getCpu() ), 0 ) );
            quota.add( new Quota( new ContainerRamResource( this.getRam() ), 0 ) );
            quota.add( new Quota( new ContainerDiskResource( this.getDisk() ), 0 ) );
        }
        return quota;
    }
}

@@ -1547,24 +1547,101 @@
 inerSize();%0A
+%0A        if ( quota.get( ContainerResourceType.CPU ) != null )%0A        %7B%0A    
         this
@@ -1714,32 +1714,119 @@
 etWriteValue();%0A
+        %7D%0A%0A        if ( quota.get( ContainerResourceType.RAM ) != null )%0A        %7B%0A    
         this.ram
@@ -1895,32 +1895,120 @@
 etWriteValue();%0A
+        %7D%0A%0A        if ( quota.get( ContainerResourceType.DISK ) != null )%0A        %7B%0A    
         this.dis
@@ -2084,24 +2084,34 @@
 iteValue();%0A
+        %7D%0A
     %7D%0A%0A%0A    

fix NPE in quota parsing