<commit_before>package org.opentosca.planinvocationengine.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.opentosca.core.model.csar.id.CSARID;
import org.opentosca.csarinstancemanagement.service.ICSARInstanceManagementService;
import org.opentosca.model.csarinstancemanagement.CSARInstanceID;
import org.opentosca.model.tosca.TParameter;
import org.opentosca.model.tosca.TPlan;
import org.opentosca.model.tosca.extension.helpers.PlanTypes;
import org.opentosca.model.tosca.extension.planinvocationevent.PlanInvocationEvent;
import org.opentosca.model.tosca.extension.transportextension.TParameterDTO;
import org.opentosca.model.tosca.extension.transportextension.TPlanDTO;
import org.opentosca.planinvocationengine.service.IPlanInvocationEngine;
import org.opentosca.planinvocationengine.service.impl.messages.generation.RESTMessageGenerator;
import org.opentosca.planinvocationengine.service.impl.messages.generation.SOAPMessageGenerator;
import org.opentosca.planinvocationengine.service.impl.messages.parsing.ResponseParser;
import org.opentosca.settings.Settings;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

/**
 * The Implementation of the Engine. Also deals with OSGI events for
 * communication with the mock-up Servicebus.
 * 
 * Copyright 2013 <NAME>
 * 
 * @author <EMAIL>
 * 
 */
public class PlanInvocationEngine implements IPlanInvocationEngine, EventHandler {
	
	
	private final SOAPMessageGenerator soapInvokeMessageGenerator = new SOAPMessageGenerator();
	private final ResponseParser responseParser = new ResponseParser();
	private final RESTMessageGenerator restInvokeMessageGenerator = new RESTMessageGenerator();
	
	private final Logger LOG = LoggerFactory.getLogger(PlanInvocationEngine.class);
	
	private static String nsBPEL = "http://docs.oasis-open.org/wsbpel/2.0/process/executable";
	private static String nsBPMN = "http://www.omg.org/spec/BPMN/2.0";
	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String invokePlan(CSARID csarID, int csarInstanceID, TPlanDTO givenPlan) {
		
		// refill information that might not be sent
		TPlan storedPlan = ServiceHandler.toscaReferenceMapper.getPlanForCSARIDAndPlanID(csarID, givenPlan.getId());
		
		if ((null == storedPlan)) {
			LOG.error("Plan " + givenPlan.getId() + " with name " + givenPlan.getName() + " is null!");
			return null;
		}
		if (!storedPlan.getId().equals(givenPlan.getId().getLocalPart())) {
			LOG.error("Plan " + givenPlan.getId() + " with internal ID " + givenPlan.getName() + " should copy of PublicPlan " + storedPlan.getId() + "!");
			return null;
		}
		
		givenPlan.setName(storedPlan.getName());
		givenPlan.setPlanLanguage(storedPlan.getPlanLanguage());
		givenPlan.setPlanType(storedPlan.getPlanType());
		givenPlan.setOutputParameters(storedPlan.getOutputParameters());
		
		PlanInvocationEvent planEvent = new PlanInvocationEvent();
		
		LOG.info("Invoke the Plan \"" + givenPlan.getId() + "\" of type \"" + givenPlan.getPlanType() + " of CSAR \"" + csarID + "\".");
		
		// fill in the informations about this PublicPlan which is not provided
		// by the PublicPlan received by the REST API
		Map<QName, TPlan> publicPlanMap = ServiceHandler.toscaReferenceMapper.getCSARIDToPlans(csarID).get(PlanTypes.isPlanTypeURI(givenPlan.getPlanType()));
		
		if (null == publicPlanMap) {
			LOG.error("Wrong type! \"" + givenPlan.getPlanType() + "\"");
			return null;
		}
		
		planEvent.setCSARID(csarID.toString());
		planEvent.setInputMessageID(ServiceHandler.toscaReferenceMapper.getPlanInputMessageID(csarID, givenPlan.getId()));
		planEvent.setInterfaceName(ServiceHandler.toscaReferenceMapper.getIntferaceNameOfPlan(csarID, givenPlan.getId()));
		planEvent.setOperationName(ServiceHandler.toscaReferenceMapper.getOperationNameOfPlan(csarID, givenPlan.getId()));
		// planEvent.setOutputMessageID(storedPlan.getOutputMessageID());
		planEvent.setPlanLanguage(storedPlan.getPlanLanguage());
		planEvent.setPlanType(storedPlan.getPlanType());
		planEvent.setPlanID(givenPlan.getId());
		planEvent.setIsActive(true);
		planEvent.setHasFailed(false);
		for (TParameter temp : storedPlan.getInputParameters().getInputParameter()) {
			boolean found = false;
			
			LOG.trace("Processing input parameter {}", temp.getName());
			
			List<TParameterDTO> params = givenPlan.getInputParameters().getInputParameter();
			for (TParameterDTO param : params) {
				
				if (param.getName().equals(temp.getName())) {
					TParameterDTO dto = param;
					// param.setRequired(temp.getRequired());
					// param.setType(temp.getType());
					found = true;
					planEvent.getInputParameter().add(dto);
					LOG.trace("Found input param {} with value {}", param.getName(), param.getValue());
				}
			}
			if (!found) {
				LOG.trace("Did not found input param {}, thus, insert empty one.", temp.getName());
				TParameterDTO newParam = new TParameterDTO();
				newParam.setName(temp.getName());
				newParam.setType(temp.getType());
				newParam.setRequired(temp.getRequired());
				planEvent.getInputParameter().add(newParam);
			}
		}
		for (TParameter temp : storedPlan.getOutputParameters().getOutputParameter()) {
			TParameterDTO param = new TParameterDTO();
			
			param.setName(temp.getName());
			param.setRequired(temp.getRequired());
			param.setType(temp.getType());
			
			planEvent.getOutputParameter().add(param);<commit_msg>in input parameter strings, delete all occurences of carriage returns and decoding line feeds that are encoded with double backslashes<commit_after>package org.opentosca.planinvocationengine.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.opentosca.core.model.csar.id.CSARID;
import org.opentosca.csarinstancemanagement.service.ICSARInstanceManagementService;
import org.opentosca.model.csarinstancemanagement.CSARInstanceID;
import org.opentosca.model.tosca.TParameter;
import org.opentosca.model.tosca.TPlan;
import org.opentosca.model.tosca.extension.helpers.PlanTypes;
import org.opentosca.model.tosca.extension.planinvocationevent.PlanInvocationEvent;
import org.opentosca.model.tosca.extension.transportextension.TParameterDTO;
import org.opentosca.model.tosca.extension.transportextension.TPlanDTO;
import org.opentosca.planinvocationengine.service.IPlanInvocationEngine;
import org.opentosca.planinvocationengine.service.impl.messages.generation.RESTMessageGenerator;
import org.opentosca.planinvocationengine.service.impl.messages.generation.SOAPMessageGenerator;
import org.opentosca.planinvocationengine.service.impl.messages.parsing.ResponseParser;
import org.opentosca.settings.Settings;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

/**
 * The Implementation of the Engine. Also deals with OSGI events for
 * communication with the mock-up Servicebus.
 * 
 * Copyright 2013 <NAME>
 * 
 * @author <EMAIL>
 * 
 */
public class PlanInvocationEngine implements IPlanInvocationEngine, EventHandler {
	
	
	private final SOAPMessageGenerator soapInvokeMessageGenerator = new SOAPMessageGenerator();
	private final ResponseParser responseParser = new ResponseParser();
	private final RESTMessageGenerator restInvokeMessageGenerator = new RESTMessageGenerator();
	
	private final Logger LOG = LoggerFactory.getLogger(PlanInvocationEngine.class);
	
	private static String nsBPEL = "http://docs.oasis-open.org/wsbpel/2.0/process/executable";
	private static String nsBPMN = "http://www.omg.org/spec/BPMN/2.0";
	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String invokePlan(CSARID csarID, int csarInstanceID, TPlanDTO givenPlan) {
		
		// refill information that might not be sent
		TPlan storedPlan = ServiceHandler.toscaReferenceMapper.getPlanForCSARIDAndPlanID(csarID, givenPlan.getId());
		
		if ((null == storedPlan)) {
			LOG.error("Plan " + givenPlan.getId() + " with name " + givenPlan.getName() + " is null!");
			return null;
		}
		if (!storedPlan.getId().equals(givenPlan.getId().getLocalPart())) {
			LOG.error("Plan " + givenPlan.getId() + " with internal ID " + givenPlan.getName() + " should copy of PublicPlan " + storedPlan.getId() + "!");
			return null;
		}
		
		givenPlan.setName(storedPlan.getName());
		givenPlan.setPlanLanguage(storedPlan.getPlanLanguage());
		givenPlan.setPlanType(storedPlan.getPlanType());
		givenPlan.setOutputParameters(storedPlan.getOutputParameters());
		
		PlanInvocationEvent planEvent = new PlanInvocationEvent();
		
		LOG.info("Invoke the Plan \"" + givenPlan.getId() + "\" of type \"" + givenPlan.getPlanType() + " of CSAR \"" + csarID + "\".");
		
		// fill in the informations about this PublicPlan which is not provided
		// by the PublicPlan received by the REST API
		Map<QName, TPlan> publicPlanMap = ServiceHandler.toscaReferenceMapper.getCSARIDToPlans(csarID).get(PlanTypes.isPlanTypeURI(givenPlan.getPlanType()));
		
		if (null == publicPlanMap) {
			LOG.error("Wrong type! \"" + givenPlan.getPlanType() + "\"");
			return null;
		}
		
		planEvent.setCSARID(csarID.toString());
		planEvent.setInputMessageID(ServiceHandler.toscaReferenceMapper.getPlanInputMessageID(csarID, givenPlan.getId()));
		planEvent.setInterfaceName(ServiceHandler.toscaReferenceMapper.getIntferaceNameOfPlan(csarID, givenPlan.getId()));
		planEvent.setOperationName(ServiceHandler.toscaReferenceMapper.getOperationNameOfPlan(csarID, givenPlan.getId()));
		// planEvent.setOutputMessageID(storedPlan.getOutputMessageID());
		planEvent.setPlanLanguage(storedPlan.getPlanLanguage());
		planEvent.setPlanType(storedPlan.getPlanType());
		planEvent.setPlanID(givenPlan.getId());
		planEvent.setIsActive(true);
		planEvent.setHasFailed(false);
		for (TParameter temp : storedPlan.getInputParameters().getInputParameter()) {
			boolean found = false;
			
			LOG.trace("Processing input parameter {}", temp.getName());
			
			List<TParameterDTO> params = givenPlan.getInputParameters().getInputParameter();
			for (TParameterDTO param : params) {
				
				if (param.getName().equals(temp.getName())) {
					TParameterDTO dto = param;
					// param.setRequired(temp.getRequired());
					// param.setType(temp.getType());
					found = true;
					planEvent.getInputParameter().add(dto);
					String value = dto.getValue();
					value = value.replace("\\r", "\r");
					value = value.replace("\r", "");
					value = value.replace("\\n", "\n");
					dto.setValue(value);
					LOG.trace("Found input param {} with value {}", param.getName(), param.getValue());
				}
			}
			if (!found) {
				LOG.trace("Did not found input param {}, thus, insert empty one.", temp.getName());
				TParameterDTO newParam = new TParameterDTO();
				newParam.setName(temp.getName());
				newParam.setType(temp.getType());
				newParam.setRequired(temp.getRequired());
				planEvent.getInputParameter().add(newParam);
			}
		}
		for (TParameter temp : storedPlan.getOutputParameters().getOutputParameter()) {
			TParameterDTO param = new TParameterDTO();
			
			param.setName(temp.getName());
			param.setRequired(temp.getRequired());
			param.setType(temp.getType());
			
			planEvent.getOutputParameter().add(param);
{'fraction_non_alphanumeric': 0.12515530522653856, 'fraction_numerical': 0.001987906899693531, 'mean_word_length': 5.198151950718686, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095708', 'n_tokens_mistral': 4025, 'n_tokens_neox': 3708, 'n_words': 636}