package org.project.openbaton.nfvo.core.core;

import org.project.openbaton.catalogue.mano.common.DeploymentFlavour;
import org.project.openbaton.catalogue.mano.descriptor.VirtualDeploymentUnit;
import org.project.openbaton.catalogue.mano.record.VirtualNetworkFunctionRecord;
import org.project.openbaton.catalogue.nfvo.Quota;
import org.project.openbaton.catalogue.nfvo.VimInstance;
import org.project.openbaton.nfvo.exceptions.VimException;
import org.project.openbaton.nfvo.vim_interfaces.vim.VimBroker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created by lto on 11/06/15.
 */
@Service
@Scope
public class VNFLifecycleOperationGranting implements org.project.openbaton.nfvo.core.interfaces.VNFLifecycleOperationGranting {
    @Autowired
    private VimBroker vimBroker;

    @Override
    public boolean grantLifecycleOperation(VirtualNetworkFunctionRecord virtualNetworkFunctionRecord) throws VimException {
        //HashMap holds how many VDUs are deployed on a specific VimInstance
        HashMap<VimInstance,Integer> countVDUsOnVimInstances = new HashMap<>();
        //Count VDUs on a specific VimInstance
        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) {
            if (vdu.getExtId() != null)
                break;
            if (countVDUsOnVimInstances.containsKey(vdu.getVimInstance())) {
                countVDUsOnVimInstances.put(vdu.getVimInstance(), countVDUsOnVimInstances.get(vdu.getVimInstance()) + 1);
            } else {
                countVDUsOnVimInstances.put(vdu.getVimInstance(), 1);
            }
        }
        //Check if enough resources are available for the deployment
        for (VimInstance vimInstance : countVDUsOnVimInstances.keySet()) {
            Quota leftQuota = vimBroker.getLeftQuota(vimInstance);
            //Fetch the Flavor for getting allocated resources needed
            DeploymentFlavour flavor = null;
            for (DeploymentFlavour currentFlavor : vimInstance.getFlavours()) {
                if (currentFlavor.getFlavour_key().equals(virtualNetworkFunctionRecord.getDeployment_flavour_key())) {
                    flavor = currentFlavor;
                    break;
                }
            }
            //Subtract needed resources from the left resources
            for (int i = 1; i <= countVDUsOnVimInstances.get(vimInstance); i++) {
                leftQuota.setInstances(leftQuota.getInstances() - 1);
                leftQuota.setCores(leftQuota.getCores() - flavor.getVcpus());
                leftQuota.setRam(leftQuota.getRam() - flavor.getRam());
            }
            //If one value is negative, it is not possible to deploy the VNFR on (at least on one VimInstance) -> return false
            if (leftQuota.getInstances() < 0 || leftQuota.getRam() < 0 || leftQuota.getCores() < 0)
                return false;
        }
        //If there are enough resources to deploy the VNFR on the VimInstance, return true
        return true;
    }
}

@@ -500,16 +500,73 @@
 Broker;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -981,16 +981,86 @@
 roker;%0A%0A
+    protected Logger log = LoggerFactory.getLogger(this.getClass());%0A%0A
     @Ove
@@ -1405,121 +1405,363 @@
     
-for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) %7B%0A            if (vdu.getExtId() != null)
+log.info(%22Granting Lifecycle Operation for vnfr: %22 + virtualNetworkFunctionRecord.getName());%0A        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) %7B%0A            log.debug(%22Found VDU: %22 + vdu.getHostname());%0A            if (vdu.getExtId() != null) %7B%0A                log.debug(%22VDU %22 + vdu.getHostname() + %22 is already deployed%22);
 %0A   
@@ -1772,32 +1772,46 @@
          break;%0A
+            %7D%0A
             if (
@@ -2177,16 +2177,109 @@
 loyment%0A
+        log.debug(%22Checking if enough resources are available on the defined VimInstance.%22);%0A
         
@@ -2412,16 +2412,123 @@
 tance);%0A
+            log.debug(%22Left Quota on VimInstance %22 + vimInstance.getName() + %22 at start is %22 + leftQuota);%0A
         
@@ -3298,24 +3298,148 @@
 .getRam());%0A
+                log.debug(%22Left Quota on VimInstance %22 + vimInstance.getName() + %22 after considering VDU is %22 + leftQuota);%0A
             
@@ -3666,16 +3666,139 @@
 s() %3C 0)
+ %7B%0A                log.warn(%22Not enough resources are available to deploy VNFR %22 + virtualNetworkFunctionRecord.getName());
 %0A       
@@ -3820,16 +3820,30 @@
  false;%0A
+            %7D%0A
         
@@ -3935,16 +3935,129 @@
 rn true%0A
+        log.info(%22Enough resources are available for deploying VNFR %22 + virtualNetworkFunctionRecord.getName());%0A
         

add logs, fix bug