iotcs.messaging.client.device.policy module

class iotcs.messaging.client.device.policy.DeviceAnalog

Bases: abc.ABC

DeviceAnalog encapsulates a device (endpoint id and attributes) with a model.

call(actionName, argumentValues)

Invoke an action.

The argumentValues parameter may be empty if there are no arguments to pass, but will not be None.

Parameters:
  • actionName – the name of the action to call
  • argumentValues – the data, possibly None or empty, to pass to the action
getAttributeValue(attribute)

Return the value of the named attribute.

Parameters:attribute – the attribute to get
Returns:the value of the attribute, possibly None
getDeviceModel()

Return the device model of the device. :return: the device model

getEndpointId()

Return the endpoint id of the device. :return: the device endpoint id

queueMessage(message)

Queue a message for dispatch to the server.

The implementation of this method may send the message directly without queuing. :param message: the message to be queued

setAttributeValue(attribute, value)

Set the named attribute to the given value.

Parameters:
  • attribute – the attribute to set
  • value – the value of the attribute
Raises:

ArgumentException – if the attribute is not in the device model, the value is None, or the value does not match the attribute type.

class iotcs.messaging.client.device.policy.DeviceAnalogImpl(directlyConnectedDevice, deviceModel, endpointId)

Bases: iotcs.messaging.client.device.policy.DeviceAnalog

An implementation of DeviceAnalog.

Parameters:
  • directlyConnectedDevice
  • deviceModel
  • endpointId
attributeValueMap
call(actionName, args)

Invoke the device model action actionName.

Parameters:
  • actionName – a device model action
  • args – a list of action arguments
deviceModel
directlyConnectedDevice
endpointId
getAttributeValue(attribute)

Return the value of attribute.

Parameters:attribute
Returns:the value of attribute or its default value.
getDeviceModel()

Return deviceModel.

getEndpointId()

Return endpointId.

classmethod getLogger()

Return a logger for DeviceAnalogImpl.

queueMessage(message)

Queue a message. :param message: the message to queue.

setAttributeValue(attribute, value)

Add the attribute with value to attributeValueMap.

Parameters:
  • attribute – An attribute name
  • value – the value for attribute must not be None
Raises:

ArgumentExceptio if value is None or not an appropriate DeviceModelAttribute.Type type.

class iotcs.messaging.client.device.policy.DevicePolicy(id, deviceModelURN, pipelines, description, lastModified, enabled)

Bases: object

Detailed information on a device policy.

Create a DevicePolicy instance.

Parameters:
  • id – the policy id
  • deviceModelURN – the :classs:`.DeviceModel` urn
  • pipelines – the policy function pipelines
  • description – the policy description
  • lastModified – the policy’s last modification date
  • enabledTrue if the policy is in effect, else False
ALL_ATTRIBUTES = '*'
class Function(id, parameters)

Bases: object

A cLass to encapsulate :class:.`DeviceFunction`s.

Create an instance of Function

Parameters:
  • id – the function id
  • parameters – the function parameters
getId()

Return the function id.

getParameters()

Return the function parameters.

id
parameters
description
deviceModelURN
dumpPipeline(dumpstring, attributeName, pipeline, first)

Return a string representation of a pipeline.

Parameters:
  • dumpstring – the pipelines string representation
  • attributeName – the attribute association with a pipeline
  • pipeline – the Function pipeline
  • first – append a comma if first is False
enabled
getDescription()

Return the free form description of the device policy.

getDeviceModelURN()

Return the target device model URN.

Returns:the URN of the target device model
getId()

Return the policy id.

Returns:the policy id
getLastModified()

Return the date of last modification.

Returns:the date of last modification
getPipeline(attributeName)

Return the function pipeline of this policy for attribute.

If attributeName is None return the piplines for all attributes. See :attrr:`.ALL_ATTRIBUTES`

Parameters:attributeName – a device model attibute
Returns:the function pipeline of this policy for attribute
getPipelines()

Return the pipelines of this policy.

The map key is an attribute name, the value is a list of Function for that attribute.

Returns:the pipelines of this policy
id
isEnabled()

Return the enabled state of the device policy.

Returns:True if the policy is in effect else False
lastModified
pipelines
class iotcs.messaging.client.device.policy.DevicePolicyManager(secureConnection)

Bases: object

Policy manager for /iot/privateapi/v2/devicePolicies.

Return a DevicePolicyManager instance.

CAPABILITY_URN = 'urn:oracle:iot:dcd:capability:device_policy'
class ChangeListener

Bases: abc.ABC

An interface to respond notification assigned or unassigned policies.

policyAssigned(devicePolicy, assignedDevices)

Called if a policy has been assigned to some devices.

The listener should check to see if the device is in the set of assigned devices. If the device is not in the set of assigned devices, the listener should take no action.

param devicePolicy: the policies that have been assigned. param assignedDevices: the set of devices that to which the

policy has been assigned.
policyUnassigned(devicePolicy, unassignedDevices)

Called if a policy has been unassigned from some devices.

The listener should check to see if the device is in the set of unassigned devices. If the device is not in the set of unassigned devices, the listener should take no action. :param devicePolicy: the policies that have been unassigned. :param unassignedDevices: the set of devices from which the policy has been unassigned.

PATH = 'deviceModels/urn:oracle:iot:dcd:capability:device_policy/policyChanged'
PERSISTENCE_LOCK = <unlocked _thread.RLock object owner=0 count=0>
addChangeListener(changelistener)

Add a listener, called when policies are assigned or unassigned.

assignPolicyToDevice(deviceModelUrn, policyId, deviceId, lastModified)

Assign a policy to a device.

Parameters:
  • deviceModelUrn – the device model urn of the policy being assigned
  • policyId – the id of the policy that being assigned.
  • deviceId – the device id receiving the policy assigned.
  • lastModified – the date of last modification
changelisteners
changelistenerslock
classmethod devicePolicyFromJSON(deviceModelURN, jsonObject)

Return a DevicePolicy instance from jsonObject.

Parameters:
  • deviceModelURN – the device model urn
  • jsonObject – a device policy json object
Returns:

a DevicePolicy instance

downloadPolicy(deviceModelUrn, policyId)

Return the DevicePolicy for policyId.

GET iot/privateapi/v2/deviceModels/{deviceModelUrn}/devicePolicies/{policyId}. The gotten policy is persisted, the policiesByPolicyId map is updated, and an entry is made (if necessary) in the policiesByDeviceModelUrn map.

Parameters:
  • deviceModelUrn – the device model urn
  • policyId – the policy id
Raise:

TransportException if request fails

Returns:

the DevicePolicy for policyId

classmethod getDevicePolicyManager(directlyConnectedDevice)

Return a DevicePolicyManager for directlyConnectedDevice.

:param directlyConnectedDevice; the directly connected device
associated with the returned DevicePolicyManager
Returns:Return a DevicePolicyManager for directlyConnectedDevice else None if there no policy manager registerd for this client.
getIndirectlyConnectedDeviceIdsForPolicy(deviceModelUrn, policyId, directlyConnectedOwner)

Return the ids of indirectly connected devices with assigned policyId. :param deviceModelUrn: the device model urn :param policyId: the policy id :param directlyConnectedOwner: the gateway device :return: a set of indirectly connected device ids

classmethod getLogger()

Return a logger for .DevicePolicyManager

param from: a tag for the log location. return: a logger for .DevicePolicyManager

getPolicy(deviceModelURN, deviceId)

Return the :classs:`.DevicePolicy` for the given device model and device id.

:param deviceModelURN the device model URN, which may not be {@code null} :param deviceId the device id, which may not be {@code null} :return a {@code DevicePolicy}, possibly {@code null}

lookupPolicyForDevice(deviceModelUrn, deviceId)

Return a DevicePolicy for deviceModelUrn and deviceId :param deviceModelUrn: :param deviceId: :return:

notifyPolicyAssigned(devicePolicy, assignedDevices)

Invoke policyAssigned() method on change listeners.

Parameters:
  • devicePolicy – the assigned policy
  • assignedDevices – the devices to which the policy was assigned.
notifyPolicyUnassigned(devicePolicy, unassignedDevices)

Invoke policyUnassigned() method on change listeners.

Parameters:
  • devicePolicy – the assigned policy
  • unassignedDevices – the devices to which the policy was assigned.
policiesbydeviceid
policiesbydeviceidlock
policiesbydevicemodelurn
policiesbydevicemodelurnlock
policiesbypolicyid
policiesbypolicyidlock
policyChanged(directlyConnectedDevice, requestMessage)

Handle the policy changed resource request.

deviceModels/urn:oracle:iot:dcd:capability:device_policy/policyChanged

Parameters:
  • directlyConnectedDevice – the device client
  • requestMessage – the RequestMessage from the server
Raise:

Exception if there is an exception from handling the request

policychangelock
processAssign(deviceModelUrn, policyId, endpointId, dcdisgatewaydevice, lastModified)

Assign a policy to a device.

Process the “assign” operation from policyChanged. The method needs to notify listeners that the policy was assigned, then update data structures and persistence to add the association of the policy to the device.

Parameters:
  • deviceModelUrn – the device model urn of the policy being assigned
  • policyId – the id of the policy being assigned.
  • endpointId – the endpointId of the device receiving the policy assignment.
  • dcdisgatewaydeviceTrue if the device is a gateway, else False
  • lastModified – the date of the modification
processPipelineChanged(directlyConnectedDevice, deviceModelUrn, policyId, lastModified)

Process a change to a policy pipeline.

The method needs to fetch the policy. :param directlyConnectedDevice: the directly connected device affected

by the policy
Parameters:
  • deviceModelUrn – the device model urn of the policy that has changed
  • policyId – the id of the policy that has changed
  • lastModified – the date of the modification
Raises:

TransportException from SecureConnection

Raises:

SecurityException from SecureConnection

processUnassign(deviceModelUrn, policyId, endpointId, dcdisgatewaydevice, lastModified)

Unassign a policy from a device.

# Process the “unassign” operation from policyChanged. # The method updates the data structures and persistence to remove the association # of the policy to the device. :param deviceModelUrn: the device model urn of the policy being

unassigned
Parameters:
  • policyId – the id of the policy being unassigned
  • endpointId – the endpointId of the device receiving the policy changed request
  • dcdisgatewaydeviceTrue if the device is a GatewayDevice else False
  • lastModified – the date of last modification
removeChangeListener(changelistener)

Remove a change listener.

secureConnection
unassignPolicyFromDevice(deviceModelUrn, policyId, deviceId, lastModified)

Unassign policy from device.

Handle the logic for unassigning a policy from a device. The only reason for this method to return false is if the client has a more recent change than what it was told by the server.

Parameters:
  • deviceModelUrn – the device model urn from which the policy is unassigned
  • policyId – the policy id of the policy that is unassigned
  • deviceId – the device id having the policy unassigned
  • lastModified – the date of last modification
Returns:

False if the cached policy is newer than policy being unassigned, else True

class iotcs.messaging.client.device.policy.MessagingPolicy(directlyConnectedDevice)

Bases: iotcs.messaging.client.device.policy.ChangeListener

MessagingPolicy supports device policy messaging offer methods.

The offer methods are DirectlyConnectedDevice.offer() and MessageDispatcher.offer(). This class uses the devicePolicy REST API.

The DirectlyConnectedDevice.offer() API invokes the applyPolicies() method and then calls DirectlyConnectedDevice.send() with the resulting messages.

The MessageDispatcher.offer() API invokes the applyPolicies() method and then calls MessageDispatcher.queue() with the resulting messages.

Return an instance of MessagingPolicy.

Constructed in DirectlyConnectedDevice :param directlyConnectedDevice: the DirectlyConnectedDevice

applyAttributePolicies(dataMessage, currentTimeMillis)

Return a DataMessage instance after appying device policies.

Apply policies that are targeted to an attribute :param dataMessage: the message to apply device policies :param currentTimeMillis: the current time :return: a DataMessage after applying device policies

applyAttributePolicy(deviceAnalog, dataItem, pipeline, currentTimeMillis)

Return :class:`.DataItem if it should be included in the Message.

Parameters:
  • deviceAnalog
  • dataItem
  • pipeline
  • currentTimeMillis
Returns:

DataItem if it should be included in the Message, else `None

applyDevicePolicies(message, currentTimeMillis)

Return a list of messages by applying device policies to message.

Apply policies that are targeted to a device model.

Parameters:
  • message
  • currentTimeMillis
Returns:

a list of messages by applying device policies to message

applyPolicies(message)

Return a list of messages after applying device policies.

This is the method that applies whatever policies there may be to the message. The method returns zero or more messages, depending on the policies that have been applied to the message. The caller is responsible for sending or queuing the returned messages. The data items in the returned are messages are possibly modified the same message is returned with the temperature value replaced by the avearage temperature. A returned message may also be one that is created by a policy function (such as a computedMetric). Or the returned messages may be messages that have been batched. If no policy applies to the message, the message itself is returned. :param message: A message of any kind :return: a list of messages to be delivered :raises: TransportException possibly thrown during network

communications
Raises:SecurityException possibly thrown accessing or using the secure connection
checkComputedMetrics(dataItems, deviceAnalog, triggerMap, currentTimeMillis)
Parameters:
  • dataItems
  • deviceAnalog
  • triggerMap
  • currentTimeMillis
Returns:

computedMetricTriggers
createDataItem(itemtype, key, newValue)

Return a DataItem instance. :param self: :param itemtype: :param key: :param newValue: :return: a DataItem instance.

damaplock
deviceAnalaogMap
directlyConnectedDevice
expirePolicy(devicePolicy, currentTimeMillis)

Return a list of messages from expired pipelines in devicePolicy.

Parameters:
  • devicePolicy
  • currentTimeMillis
Returns:

a list of messages from expired pipelines in devicePolicy.

classmethod getLogger()

Return a logger for MessagingPolicy.

messagesFromExpiredPolicies
pipelineDataCachepldclock
policyAssigned(devicePolicy, assignedDevices)
Parameters:
  • devicePolicy
  • assignedDevices
Returns:

policyUnassigned(devicePolicy, assignedDevices)
Parameters:
  • devicePolicy
  • assignedDevices
windowMap
class iotcs.messaging.client.device.policy.ScheduledPolicyData(window, slide, timeZero)

Bases: object

A class to encapsulate scheduled policy data.

Return an instance of ScheduledPolicyData :param window: :param slide: :param timeZero:

class Key(window, slide)

Bases: object

A class to represent a key that represents ScheduledPolicyData.

Parameters:
  • window
  • slide
slide
window
addAttribute(attributeName, pipelineIndex)

Add an entry to pipelineIndices.

Parameters:
  • attributeName
  • pipelineIndex
expiry
getDelay(now)

Return the offset (delay) between expiry and now. :param now: :return: the offset (delay) between expiry and now in

fractional seconds.
getLogger()

Return a logger for ScheduledPolicyData.

handleExpiredFunction(virtualDeviceImpl, updatedAttributes)
Parameters:
  • virtualDeviceImpl
  • updatedAttributes
isEmpty()

Return True if pipelineIndices is empty. :return:

pipelineIndices
pipelineIndicesLock
processExpiredFunction(virtualDeviceImpl, updatedAttributes, timeZero)

Perform maintenance for the expired function. :param virtualDeviceImpl: :param updatedAttributes: :param timeZero:

removeAttribute(attributeName, pipelineIndex)

generated source for method removeAttribute

slide
class iotcs.messaging.client.device.policy.TimedPolicyThread(virtualDevice)

Bases: threading.Thread

A thread to run timed policies.

Return an instance of :class:`.TimedPolicyThread.

THREADCOUNT = 0
THREADCOUNTLOCK = <unlocked _thread.RLock object owner=0 count=0>
addTimedPolicyData(data)

Add data to scheduledPolicyData. :param data: a ScheduledPolicyData instance.

cancel()

generated source for method cancel

cancelled
getLogger()

Return a logger for TimedPolicyThread.

isCancelled()

Return True if the thread is cancelled.

removeTimedPolicyData(data)

Remove data from scheduledPolicyData. :param data: a ScheduledPolicyData instance. :return:

run()

generated source for method run

scheduledPolicyData
scheduledPolicyDataLock
virtualDevice