package org.fiteagle.north.sfa.provision;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.fiteagle.api.core.IGeni;
import org.fiteagle.api.core.IMessageBus;
import org.fiteagle.api.core.MessageBusOntologyModel;
import org.fiteagle.api.core.MessageUtil;
import org.fiteagle.north.sfa.am.ISFA_AM;
import org.fiteagle.north.sfa.am.dm.SFA_AM_MDBSender;
import org.fiteagle.north.sfa.util.URN;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

public class ProcessProvision {
  
  private final static Logger LOGGER = Logger.getLogger(ProcessProvision.class.getName());
  
  @SuppressWarnings("unchecked")
  public static void parseProvsionParameter(final List<?> parameter, final HashMap<String, Object> provisionParameters) {
    
    LOGGER.log(Level.INFO, " parsing provision parameter ");
    System.out.println(parameter.size());
    
    for (final Object param : parameter) {
      if (param instanceof Map<?, ?>) {
        LOGGER.log(Level.INFO, "parameter is a map");
        final Map<String, ?> param2 = (Map<String, ?>) param;
        if (!param2.isEmpty()) {
          for (Map.Entry<String, ?> parameters : param2.entrySet()) {
            provisionParameters.put(parameters.getKey(), parameters.getValue().toString());
            LOGGER.log(Level.INFO, "parameters in map " + parameters.getKey() + " " + parameters.getValue().toString());
          }
        }
      }
      if (param instanceof List<?>) {
    	  LOGGER.log(Level.INFO, "parameter is a list");
        final List<Object> param2 = (List<Object>) param;
        if (!param2.isEmpty()) {
          provisionParameters.put(ISFA_AM.URN, param2);
          for (Object parametersString : (List<Object>) provisionParameters.get(ISFA_AM.URN)) {
        	  LOGGER.log(Level.INFO, "provision urns are " + parametersString);
          }
        }
      }
    }
  }
  
  @SuppressWarnings("unchecked")
	public static Model provisionInstances(
			List<URN> urns) {

		LOGGER.log(Level.INFO, "create provision model ");
		Model requestModel = ModelFactory.createDefaultModel();
		for (URN urn : urns) {
			Resource reservation = requestModel.createResource(urn.toString());

			if (ISFA_AM.SLICE.equals(urn.getType())) {
				reservation.addProperty(RDF.type,
						MessageBusOntologyModel.classGroup);
			}else{
				if (ISFA_AM.Sliver.equals(urn.getType())) {
					reservation.addProperty(RDF.type,
							MessageBusOntologyModel.classReservation);
				}
			}

		}

		String serializedModel = MessageUtil.serializeModel(requestModel,
				IMessageBus.SERIALIZATION_TURTLE);
		LOGGER.log(Level.INFO, "send provision request ...");
		Model provisionResponse = SFA_AM_MDBSender.getInstance()
				.sendRDFRequest(serializedModel, IMessageBus.TYPE_CONFIGURE,
						IMessageBus.TARGET_ORCHESTRATOR);
		LOGGER.log(Level.INFO,
				"provision reply received." + provisionResponse.getGraph());
		return provisionResponse;
	}
  
  public static void addProvisionValue(final HashMap<String, Object> result, Model provisionResponse){
	  
	  final Map<String, Object> value = new HashMap<>();
	  value.put(IGeni.GENI_RSPEC,"should be geni_manifest");
	  final List<Map<String, Object>> geniSlivers = new LinkedList<>();
	  
	  StmtIterator stmtIterator = provisionResponse.listStatements(null, RDF.type, MessageBusOntologyModel.classReservation);
	    while (stmtIterator.hasNext()) {
	      Statement statement = stmtIterator.next();
	      Resource reservation = statement.getSubject();
	      
	      /**
	       * defines a loop depending on the slivers number.
	       * In the loop, Map is created for each sliver containing 
	       * sliver urn, experires, allocateion_status and operation_status.
	       * The created maps should be added to geniSlivers list.
	       */ 
	      final Map<String, Object> sliverMap = new HashMap<>();
	      sliverMap.put(IGeni.GENI_SLIVER_URN, reservation.getURI());
	      if(reservation.hasProperty(MessageBusOntologyModel.endTime)){
	    	  sliverMap.put(IGeni.GENI_EXPIRES, reservation.getProperty(MessageBusOntologyModel.endTime).getLiteral().getString());
	      } else {
	    	  sliverMap.put(IGeni.GENI_EXPIRES, "");
	      }
	      sliverMap.put(IGeni.GENI_ALLOCATION_STATUS, reservation.getProperty(MessageBusOntologyModel.hasState).getLiteral().getString());
	      sliverMap.put(IGeni.GENI_OPERATIONAL_STATUS, "");
	      sliverMap.put(IGeni.GENI_ERROR, "NO ERROR");
	      
	      geniSlivers.add(sliverMap);
	    }
	    value.put(IGeni.GENI_SLIVERS, geniSlivers);
	    result.put(ISFA_AM.VALUE, value);
  }
  
}

@@ -907,1273 +907,8 @@
 %0A  %0A
-  @SuppressWarnings(%22unchecked%22)%0A  public static void parseProvsionParameter(final List%3C?%3E parameter, final HashMap%3CString, Object%3E provisionParameters) %7B%0A    %0A    LOGGER.log(Level.INFO, %22 parsing provision parameter %22);%0A    System.out.println(parameter.size());%0A    %0A    for (final Object param : parameter) %7B%0A      if (param instanceof Map%3C?, ?%3E) %7B%0A        LOGGER.log(Level.INFO, %22parameter is a map%22);%0A        final Map%3CString, ?%3E param2 = (Map%3CString, ?%3E) param;%0A        if (!param2.isEmpty()) %7B%0A          for (Map.Entry%3CString, ?%3E parameters : param2.entrySet()) %7B%0A            provisionParameters.put(parameters.getKey(), parameters.getValue().toString());%0A            LOGGER.log(Level.INFO, %22parameters in map %22 + parameters.getKey() + %22 %22 + parameters.getValue().toString());%0A          %7D%0A        %7D%0A      %7D%0A      if (param instanceof List%3C?%3E) %7B%0A    %09  LOGGER.log(Level.INFO, %22parameter is a list%22);%0A        final List%3CObject%3E param2 = (List%3CObject%3E) param;%0A        if (!param2.isEmpty()) %7B%0A          provisionParameters.put(ISFA_AM.URN, param2);%0A          for (Object parametersString : (List%3CObject%3E) provisionParameters.get(ISFA_AM.URN)) %7B%0A        %09  LOGGER.log(Level.INFO, %22provision urns are %22 + parametersString);%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A
   %0A 
@@ -1888,39 +1888,8 @@
 ed.%22
- + provisionResponse.getGraph()
 );%0A%09

clean up