package org.fiteagle.north.sfa.am.delete;

import info.openmultinet.ontology.vocabulary.Omn;
import info.openmultinet.ontology.vocabulary.Omn_lifecycle;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
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.*;
import org.fiteagle.north.sfa.am.ISFA_AM;
import org.fiteagle.north.sfa.am.ReservationStateEnum;
import org.fiteagle.north.sfa.am.common.AbstractMethodProcessor;
import org.fiteagle.north.sfa.am.dm.SFA_AM_MDBSender;
import org.fiteagle.north.sfa.util.GENI_Credential;
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 ProcessDelete extends AbstractMethodProcessor {
	
	private final static Logger LOGGER = Logger.getLogger(ProcessDelete.class.getName());
	
	public ProcessDelete(final List<?> parameter) {
    this.parameter = parameter;
  }
	
	public  Model deleteInstances() throws UnsupportedEncodingException {
		
		LOGGER.log(Level.INFO, "create delete model ");
		Model requestModel = ModelFactory.createDefaultModel();
		for (URN urn : this.urns) {


			if (ISFA_AM.SLICE.equals(urn.getType())) {
                Resource resource = requestModel.createResource(IConfig.TOPOLOGY_NAMESPACE_VALUE+urn.getSubject());
                resource.addProperty(RDF.type, Omn.Topology);
			} else if (ISFA_AM.Sliver.equals(urn.getType())) {
                Resource resource =  requestModel.createResource(URLDecoder.decode(urn.getSubject(), ISFA_AM.UTF_8));
                resource.addProperty(RDF.type, Omn.Resource);
            }
		}

		String serializedModel = MessageUtil.serializeModel(requestModel, IMessageBus.SERIALIZATION_TURTLE);
		LOGGER.log(Level.INFO, "send delete request ...");
		Model deleteResponse = getSender().sendRDFRequest(serializedModel, IMessageBus.TYPE_DELETE,IMessageBus.TARGET_ORCHESTRATOR);
		LOGGER.log(Level.INFO,"delete reply is received.");
		return deleteResponse;
		
	}
	
	public void createResponse(final HashMap<String, Object> result, Model deleteResponse){
		
		  final List<Map<String, Object>> value = new LinkedList<>();
		  
		  StmtIterator stmtIterator = deleteResponse.listStatements(null, RDF.type, Omn.Reservation);
		    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 and allocateion_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());
		    	sliverMap.put(IGeni.GENI_EXPIRES, reservation.getProperty(MessageBusOntologyModel.endTime).getLiteral().getString());
		      sliverMap.put(IGeni.GENI_ALLOCATION_STATUS, ReservationStateEnum.valueOf(reservation.getProperty(Omn_lifecycle.hasReservationState).getResource().getLocalName()));
		      
		      value.add(sliverMap);
		    }
		    
		    result.put(ISFA_AM.VALUE, value);
        this.addCode(result);
        this.addOutput(result);
	}
	

}

@@ -32,24 +32,130 @@
 am.delete;%0A%0A
+import com.hp.hpl.jena.rdf.model.*;%0Aimport info.openmultinet.ontology.translators.geni.ManifestConverter;%0A
 import info.
@@ -837,229 +837,8 @@
 N;%0A%0A
-import com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0Aimport com.hp.hpl.jena.rdf.model.Resource;%0Aimport com.hp.hpl.jena.rdf.model.Statement;%0Aimport com.hp.hpl.jena.rdf.model.StmtIterator;%0A
 impo
@@ -2343,12 +2343,11 @@
 %09%09  
-Stmt
+Res
 Iter
@@ -2351,20 +2351,19 @@
 terator 
-stmt
+res
 Iterator
@@ -2388,39 +2388,37 @@
 list
-Statements(null, RDF.type, 
+ResourcesWithProperty(
 Omn.
+has
 Rese
@@ -2440,20 +2440,19 @@
  while (
-stmt
+res
 Iterator
@@ -2465,24 +2465,25 @@
 xt()) %7B%0A
+%0A
 %09%09      
 Statemen
@@ -2478,102 +2478,52 @@
     
-Statement statement = stmtIterator.next();%0A%09%09      Resource reservation = statement.getSubject
+Resource resource = resIterator.nextResource
 ();%0A
@@ -2873,62 +2873,216 @@
 ;%0A%09%09
-      sliverMap.put(IGeni.GENI_SLIVER_URN, reservation
+%09%09Resource reservation  = resource.getProperty(Omn.hasReservation).getObject().asResource();%0A%09%09      sliverMap.put(IGeni.GENI_SLIVER_URN,  ManifestConverter.generateSliverID(IConfig.DEFAULT_HOSTNAME, resource
 .get
@@ -3087,16 +3087,17 @@
 etURI())
+)
 ;%0A%09%09    
@@ -3384,16 +3384,31 @@
 lName())
+.getGeniState()
 );%0A%09%09   

delete (unstable)