Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the code in order to work with latest siddhi released version and few improvements #7

Merged
merged 10 commits into from
Apr 12, 2018
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ ENV/

# Rope project settings
.ropeproject
.idea/
15 changes: 8 additions & 7 deletions PySiddhi4/SiddhiLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# under the License.

import logging
import os

import sys

Expand All @@ -28,13 +29,14 @@
_PythonJavaClass = None
_JavaClass = None


def addExtensionPath(class_path):
def addExtensionPath():
'''
Adds an Extension to Siddhi. Should be called prior to importing any Siddhi Libraries.
:param class_path: Path to Jar File. Wild Card (*) directory selection is accepted
:return:
'''
siddhi_home = os.getenv("SIDDHISDK_HOME")
class_path = siddhi_home.append("/lib/*")
if "siddhi_api_configured" in globals():
raise Exception("Cannot add extensions after loading library.")

Expand Down Expand Up @@ -70,14 +72,14 @@ def _resumeLibrary():

def loadLibrary():
'''
Loads Siddi CEP Library
Loads Siddi Library
:return:
'''

siddhi_home = os.getenv("SIDDHISDK_HOME")
# Test whether Java Library is already loaded
if "siddhi_api_configured" in globals():
if globals()["siddhi_api_configured"] != 4:
raise Exception("Unable to use multiple versions of Siddhi CEP Library")
raise Exception("Unable to use multiple versions of Siddhi Library")
# Resume global variables if already loaded
_resumeLibrary()
return
Expand All @@ -92,8 +94,7 @@ def loadLibrary():
jnius_config.add_options('-Djava.library.path=' + PySiddhi4.root_path + "/__PySiddhi4Proxy")

# Determine library class path
class_paths = ['.', PySiddhi4.root_path + '/__PySiddhi4Proxy/target/lib/*',
PySiddhi4.root_path + '/__PySiddhi4Proxy/target/*']
class_paths = ['.', siddhi_home + '/lib/*']

# Add Extensions
if not "extensions" in globals():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

from enum import Enum

from PySiddhi4.das.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.das.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping
from PySiddhi4.das.__Util import decodeField, decodeObject
from PySiddhi4.sp.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.sp.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping
from PySiddhi4.sp.__Util import decodeField, decodeObject


class AttributeConfiguration(APIObject):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import json
import logging

from PySiddhi4.das.__Communication.RestClient import RestClient
from PySiddhi4.das.EventSimulator.FeedSimulationConfiguration import FeedSimulationConfiguration
from PySiddhi4.sp.__Communication.RestClient import RestClient
from PySiddhi4.sp.EventSimulator.FeedSimulationConfiguration import FeedSimulationConfiguration
from requests.auth import HTTPBasicAuth


class EventSimulatorClient(RestClient):
'''
Client used to access DAS Event Simulator End Points
Client used to access SP Event Simulator End Points
'''

def __init__(self, event_simulator_url):
Expand All @@ -33,70 +34,72 @@ def __init__(self, event_simulator_url):
'''
RestClient.__init__(self, event_simulator_url)

def saveSimulationFeedConfiguration(self, simulationConfiguration):
def saveSimulationFeedConfiguration(self, simulationConfiguration, username, password):
'''
Saves a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Saves a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationConfiguration:
:return:
'''
r = self._sendPostRequest("/feed", data=json.dumps(simulationConfiguration.toJSONObject()))
r = self._sendPostRequest("/feed", data=json.dumps(simulationConfiguration.toJSONObject()),
auth=HTTPBasicAuth(username, password))
if r.status_code == 201:
return True
elif r.status_code == 409:
raise Exception("EventSimulationConfiguration with same name already exists.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def runSimulationFeedConfiguration(self, simulationConfiguration):
def runSimulationFeedConfiguration(self, simulationConfiguration, username, password):
'''
Runs a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Runs a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationConfiguration:
:return:
'''
r = self._sendPostRequest("/feed/" + simulationConfiguration.properties.simulationName + "/?action=run",
data=json.dumps(simulationConfiguration.toJSONObject()))
data=json.dumps(simulationConfiguration.toJSONObject()),
auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
raise Exception("EventSimulationConfiguration with given name does not exist.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def pauseSimulationFeedConfiguration(self, simulationName):
def pauseSimulationFeedConfiguration(self, simulationName, username, password):
'''
Pauses a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Pauses a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationName:
:return:
'''
r = self._sendPostRequest("/feed/" + simulationName + "/?action=pause")
r = self._sendPostRequest("/feed/" + simulationName + "/?action=pause", auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
raise Exception("EventSimulationConfiguration with given name does not exist.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def resumeSimulationFeedConfiguration(self, simulationName):
def resumeSimulationFeedConfiguration(self, simulationName, username, password):
'''
Resumes a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Resumes a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationName:
:return:
'''
r = self._sendPostRequest("/feed/" + simulationName + "/?action=resume")
r = self._sendPostRequest("/feed/" + simulationName + "/?action=resume", auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
raise Exception("EventSimulationConfiguration with given name does not exist.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def stopSimulationFeedConfiguration(self, simulationName):
def stopSimulationFeedConfiguration(self, simulationName, username, password):
'''
Stops a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Stops a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationName:
:return:
'''
r = self._sendPostRequest("/feed/" + simulationName + "/?action=stop")
r = self._sendPostRequest("/feed/" + simulationName + "/?action=stop", auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
Expand All @@ -106,42 +109,43 @@ def stopSimulationFeedConfiguration(self, simulationName):
else:
raise Exception(str(r.status_code) + ": " + r.text)

def editSimulationFeedConfiguration(self, simulationName, simulationConfiguration):
def editSimulationFeedConfiguration(self, simulationName, simulationConfiguration, username, password):
'''
Edits a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Edits a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationName:
:param simulationConfiguration: new simulationNameConfiguration
:return:
'''
r = self._sendPutRequest("/feed/" + simulationName, data=json.dumps(simulationConfiguration.toJSONObject()))
r = self._sendPutRequest("/feed/" + simulationName, data=json.dumps(simulationConfiguration.toJSONObject()),
auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
raise Exception("EventSimulationConfiguration with specified name does not exist.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def deleteSimulationFeedConfiguration(self, simulationName):
def deleteSimulationFeedConfiguration(self, simulationName, username, password):
'''
Deletes a SimulationFeedConfiguration in WSO2 DAS Event Simulator
Deletes a SimulationFeedConfiguration in WSO2 SP Event Simulator
:param simulationName:
:return:
'''
r = self._sendDeleteRequest("/feed/" + simulationName)
r = self._sendDeleteRequest("/feed/" + simulationName, auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
return True
elif r.status_code == 404:
raise Exception("EventSimulationConfiguration with specified name does not exist.")
else:
raise Exception(str(r.status_code) + ": " + r.text)

def retrieveSimulationFeedConfiguration(self, simulationName):
def retrieveSimulationFeedConfiguration(self, simulationName, username, password):
'''
Retrieves a SimulationFeedConfiguration from WSO2 DAS Event Simulator
Retrieves a SimulationFeedConfiguration from WSO2 SP Event Simulator
:param simulationName:
:return:
'''
r = self._sendGetRequest("/feed/" + simulationName)
r = self._sendGetRequest("/feed/" + simulationName, auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
result = r.json()
if result["status"].lower() == "ok":
Expand All @@ -154,14 +158,15 @@ def retrieveSimulationFeedConfiguration(self, simulationName):
else:
raise Exception(str(r.status_code) + ": " + r.text)

def simulateSingleEvent(self, singleSimulationConfiguration):
def simulateSingleEvent(self, singleSimulationConfiguration, username, password):
'''
Invokes a Single Simulation in WSO2 DAS Event Simulator
Invokes a Single Simulation in WSO2 SP Event Simulator
:param singleSimulationConfiguration:
:return:
'''
logging.info("Sending: " + json.dumps(singleSimulationConfiguration.toJSONObject()))
r = self._sendPostRequest("/single", data=json.dumps(singleSimulationConfiguration.toJSONObject()))
r = self._sendPostRequest("/single", data=json.dumps(singleSimulationConfiguration.toJSONObject()),
auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
logging.info("Received: " + r.text)
result = r.json()
Expand All @@ -174,9 +179,9 @@ def simulateSingleEvent(self, singleSimulationConfiguration):
else:
raise Exception(str(r.status_code) + ": " + r.text)

def uploadCSV(self, fileName, stream=None, path=None):
def uploadCSV(self, fileName, username, password, stream=None, path=None):
'''
Uploads a CSV to WSO2 DAS Event Simulator. Only one of the parameters stream or path should be given.
Uploads a CSV to WSO2 SP Event Simulator. Only one of the parameters stream or path should be given.
:param fileName: fileName of file to be uploaded
:param stream: stream of file to be uploaded
:param path: path of file to be uploaded
Expand All @@ -187,7 +192,7 @@ def uploadCSV(self, fileName, stream=None, path=None):
files = {"file": (fileName, stream)}
else:
files = {"file": (fileName, open(path, "rb"))}
r = self._sendPostRequest("/files", files=files)
r = self._sendPostRequest("/files", files=files, auth=HTTPBasicAuth(username, password))

logging.info(r)

Expand All @@ -196,9 +201,9 @@ def uploadCSV(self, fileName, stream=None, path=None):
else:
raise Exception(str(r.status_code) + ": " + r.text)

def updateCSV(self, uploadedFileName, newFileName, stream=None, path=None):
def updateCSV(self, uploadedFileName, newFileName, username, password, stream=None, path=None):
'''
Updates a CSV file uploaded to WSO2 DAS Event Simulator. Only one of parameters stream or path should
Updates a CSV file uploaded to WSO2 SP Event Simulator. Only one of parameters stream or path should
be provided.
:param uploadedFileName: previous file name
:param newFileName: new file name
Expand All @@ -211,7 +216,7 @@ def updateCSV(self, uploadedFileName, newFileName, stream=None, path=None):
files = {"file": (newFileName, stream)}
else:
files = {"file": (newFileName, open(path, "rb"))}
r = self._sendPutRequest("/files/" + uploadedFileName, files=files)
r = self._sendPutRequest("/files/" + uploadedFileName, files=files, auth=HTTPBasicAuth(username, password))

logging.info(r)

Expand All @@ -220,13 +225,13 @@ def updateCSV(self, uploadedFileName, newFileName, stream=None, path=None):
else:
raise Exception(str(r.status_code) + ": " + r.text)

def deleteCSV(self, fileName):
def deleteCSV(self, fileName, username, password):
'''
Deletes a CSV file uploaded to WSO2 DAS Event Simulator
Deletes a CSV file uploaded to WSO2 SP Event Simulator
:param fileName:
:return:
'''
r = self._sendDeleteRequest("/files/" + fileName)
r = self._sendDeleteRequest("/files/" + fileName, auth=HTTPBasicAuth(username, password))
logging.info(r)

if r.status_code == 200:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
# specific language governing permissions and limitations
# under the License.

from PySiddhi4.das.EventSimulator.SimulationProperties import SimulationProperties
from PySiddhi4.das.EventSimulator.SimulationSource import SimulationSource
from PySiddhi4.das.ObjectMapping.APIObject import APIObject
from PySiddhi4.das.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping
from PySiddhi4.sp.EventSimulator.SimulationProperties import SimulationProperties
from PySiddhi4.sp.EventSimulator.SimulationSource import SimulationSource
from PySiddhi4.sp.ObjectMapping.APIObject import APIObject
from PySiddhi4.sp.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping


class FeedSimulationConfiguration(APIObject):
'''
FeedSimulationConfiguration API Object which could be passed to WSO2 DAS Event Simulator via EventSimulatorClient.
FeedSimulationConfiguration API Object which could be passed to WSO2 SP Event Simulator via EventSimulatorClient.
'''

def __init__(self, simulation_name=None, properties=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import random

from PySiddhi4.das.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.das.ObjectMapping.FieldMapping import FieldMapping
from PySiddhi4.das.__Util import decodeField
from PySiddhi4.sp.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.sp.ObjectMapping.FieldMapping import FieldMapping
from PySiddhi4.sp.__Util import decodeField

ran = random

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

from enum import Enum

from PySiddhi4.das.EventSimulator.AttributeConfiguration import AttributeConfiguration
from PySiddhi4.das.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.das.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping, strOrInt
from PySiddhi4.sp.EventSimulator.AttributeConfiguration import AttributeConfiguration
from PySiddhi4.sp.ObjectMapping.APIObject import APIObject, NotSet
from PySiddhi4.sp.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping, strOrInt


class SimulationSource(APIObject):
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, simulationType=Type.RANDOM_DATA_SIMULATION, streamName=NotSet
dataSourceLocation=NotSet(), driver=NotSet(),
username=NotSet(), password=NotSet(), tableName=NotSet(), columnNamesList=NotSet()):
'''
Instantiates Simulation Source. Refer DAS4 Documentation for details on parameters
Instantiates Simulation Source. Refer SP4 Documentation for details on parameters
:param simulationType: Type of SimulationSource
:param streamName:
:param siddhiAppName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
# specific language governing permissions and limitations
# under the License.

from PySiddhi4.das.ObjectMapping.APIObject import APIObject
from PySiddhi4.das.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping
from PySiddhi4.sp.ObjectMapping.APIObject import APIObject
from PySiddhi4.sp.ObjectMapping.FieldMapping import FieldMapping, ListFieldMapping


class SingleSimulationConfiguration(APIObject):
'''
SingleSimulationConfiguration APIObject which may be passed to WSO2 DAS Event Simulator via EventSimulatorClient.
SingleSimulationConfiguration APIObject which may be passed to WSO2 SP Event Simulator via EventSimulatorClient.
'''

def __init__(self, siddhiAppName, streamName, data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from abc import ABCMeta
from future.utils import with_metaclass

from PySiddhi4.das.__Util import decodeField, encodeField
from PySiddhi4.sp.__Util import decodeField, encodeField


class NotSet(object):
Expand Down
Loading