@Override public Future<ActionFuture> setTargetTemperatureState(final TemperatureState temperatureState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { ActionDescription build = Services.updateActionDescription(actionDescription, temperatureState, getServiceType()).build(); System.out.println("generated ActionDescription: "+build); return applyAction(build); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set temperatureState", ex); } }
/** * Updates the ActionChain which is a description of actions that lead to this action. * The action chain is updated in a way that the immediate parent is the first element of * the chain. So the index of the chain indicates how many actions are in between this * action and the causing action. * * @param actionDescription the ActionDescription which is updated * @param parentAction the ActionDescription of the action which is the cause for the new action * @return the updated ActionDescription */ public static ActionDescription.Builder updateActionChain(final ActionDescription.Builder actionDescription, final ActionDescriptionOrBuilder parentAction) { actionDescription.addActionChain(getActionReferenceFromActionDescription(parentAction)); actionDescription.addAllActionChain(parentAction.getActionChainList()); return actionDescription; }
/** * Update the slot of the ResourceAllocation based on the current time and the * values of the ActionDescription. * To generate the slot the method {@link #getAllocationInterval(ActionDescription.Builder) getAllocationInterval} is used. * * @param actionDescription the ActionDescription inside which the ResourceAllocation is updated * @return the updated ActionDescription */ public static ActionDescription.Builder updateResourceAllocationSlot(final ActionDescription.Builder actionDescription) { final ResourceAllocation.Builder resourceAllocationBuilder = actionDescription.getResourceAllocationBuilder(); resourceAllocationBuilder.setSlot(getAllocationInterval(actionDescription)); return actionDescription; }
/** * Get an ActionDescription which only misses unit and service information. * Is created with default ActionParameter. * Fields which are still missing after: * <ul> * <li>ActionDescription.Label</li> * <li>ActionDescription.Description</li> * <li>ActionDescription.ResourceAllocation.ResourceId</li> * <li>ActionDescription.ResourceAllocation.Description</li> * <li>ActionDescription.ResourceAllocation.UnitId</li> * <li>ActionDescription.ResourceAllocation.ServiceType</li> * <li>ActionDescription.ResourceAllocation.ServiceAttributeType</li> * <li>ActionDescription.ServiceStateDescription.ServiceAttribute</li> * </ul> * * @param actionAuthority the actionAuthority for the actionDescription * @param initiator the initiator type for the resourceAllocation in the actionDescription * @return */ public static ActionDescription.Builder getActionDescription(final ActionAuthority actionAuthority, final ResourceAllocation.Initiator initiator) { return getActionDescription(getDefaultActionParameter(), actionAuthority, initiator); }
MultiResourceAllocationStrategyType.MultiResourceAllocationStrategy.Strategy multiResourceAllocationStrategy) throws CouldNotPerformException { ActionDescriptionType.ActionDescription.Builder actionDescriptionBuilder = ActionDescriptionProcessor.getActionDescription(actionAuthority, initiator); actionDescriptionBuilder.setExecutionTimePeriod(executionTimePeriod); if (executionTimePeriod != 0) { actionDescriptionBuilder.setLabel(actionDescriptionBuilder.getLabel().replace(ActionDescriptionProcessor.LABEL_KEY, unitRemote.getLabel())); ActionDescriptionProcessor.updateResourceAllocationSlot(actionDescriptionBuilder); ActionDescriptionProcessor.updateResourceAllocationId(actionDescriptionBuilder);
ActionDescriptionProcessor.updateResourceAllocationId(actionDescription); ActionDescriptionProcessor.updateResourceAllocationSlot(actionDescription); try { UnitRemote<? extends GeneratedMessage> unit = Units.getUnit(actionDescription.getServiceStateDescription().getUnitId(), true);
final UnitRemote unitRemote = unitRemoteMap.get(serviceStateDescription.getUnitId()); ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); Services.clearResponsibleAction(serviceAttribute); ActionDescriptionProcessor.updateActionChain(actionDescription, responsibleAction);
resourceAllocation.clearResourceIds(); resourceAllocation.addAllResourceIds(scopeUnitMap.keySet()); ActionDescriptionProcessor.updateResourceAllocationSlot(actionDescriptionBuilder); actionDescriptionBuilder.setActionState(ActionState.newBuilder().setValue(ActionState.State.INITIALIZED).build()); ActionDescriptionProcessor.generateToken(actionDescriptionBuilder); actionFuture.addActionDescription(actionDescriptionBuilder);
private ActionDescription updateActionDescriptionForUnit(ActionDescription actionDescription, UnitRemote unitRemote) throws CouldNotPerformException { // create new builder and copy fields ActionDescription.Builder unitActionDescription = ActionDescription.newBuilder(actionDescription); // get a new resource allocation id ActionDescriptionProcessor.updateResourceAllocationId(unitActionDescription); // update the action chain ActionDescriptionProcessor.updateActionChain(unitActionDescription, actionDescription); // resource ids should only contain that unit ResourceAllocation.Builder unitResourceAllocation = unitActionDescription.getResourceAllocationBuilder(); unitResourceAllocation.clearResourceIds(); unitResourceAllocation.addResourceIds(ScopeGenerator.generateStringRep(unitRemote.getScope())); // update the id in the serviceStateDescription to that of the unit ServiceStateDescription.Builder serviceStateDescription = unitActionDescription.getServiceStateDescriptionBuilder(); serviceStateDescription.setUnitId((String) unitRemote.getId()); return unitActionDescription.build(); }
@Override public void init(final ActionDescription actionDescription) throws InitializationException, InterruptedException { try { // verify this.verifyActionDescription(actionDescription); // prepare this.actionDescriptionBuilder = actionDescription.toBuilder(); this.serviceAttribute = serviceJSonProcessor.deserialize(actionDescription.getServiceStateDescription().getServiceAttribute(), actionDescription.getServiceStateDescription().getServiceAttributeType()); // verify service attribute Services.verifyServiceState(serviceAttribute); // since its an action it has to be an operation service pattern this.serviceDescription = ServiceDescription.newBuilder().setType(actionDescription.getServiceStateDescription().getServiceType()).setPattern(ServicePattern.OPERATION).build(); // set resource allocation interval if not defined yet if (!actionDescription.getResourceAllocation().getSlot().hasBegin()) { ActionDescriptionProcessor.updateResourceAllocationSlot(actionDescriptionBuilder); } } catch (CouldNotPerformException ex) { throw new InitializationException(this, ex); } }
/** * Add a token to the id field of the ResourceAllocation inside the ActionDescription. * This method does nothing if the id already contains a token. * * @param actionDescription the ActionDescription which is updated * @return the updated ActionDescription */ public static ActionDescription.Builder generateToken(final ActionDescription.Builder actionDescription) { ResourceAllocation.Builder resourceAllocation = actionDescription.getResourceAllocationBuilder(); if (hasResourceAllocationToken(actionDescription)) { return actionDescription; } else { String token = UUID.randomUUID().toString(); resourceAllocation.setId(resourceAllocation.getId() + TOKEN_SEPERATOR + token); return actionDescription; } }
@Override protected boolean check(ActionFuture actionFuture) throws CouldNotPerformException { if (actionFuture.getActionDescriptionCount() == 0) { throw new NotAvailableException(dataProvider.getLabel(), "ActionDescription"); } ActionDescription actionDescription = actionFuture.getActionDescription(0); if (!actionDescription.hasTransactionId() || actionDescription.getTransactionId() == 0) { // this is for compatibility reasons with old versions logger.warn("TransactionId has not been set for Action[" + ActionDescriptionProcessor.getDescription(actionFuture.getActionDescriptionList()) + "] of " + dataProvider); return true; } return dataProvider.getLatestTransactionId() >= actionDescription.getTransactionId(); } }
/** * Get a new id value for the id field in the ResourceAllocation of an ActionDescription * while keeping the token if there si one. * * @param actionDescription the action description which is updated as described above * @return the action description which is updated as described above */ public static ActionDescription.Builder updateResourceAllocationId(final ActionDescription.Builder actionDescription) { ResourceAllocation.Builder resourceAllocation = actionDescription.getResourceAllocationBuilder(); String newId = UUID.randomUUID().toString(); if (!hasResourceAllocationToken(actionDescription)) { resourceAllocation.setId(newId); } else { String token = resourceAllocation.getId().split(TOKEN_SEPERATOR)[1]; resourceAllocation.setId(newId + TOKEN_SEPERATOR + token); } return actionDescription; }
@Override public Future<ActionFuture> setBrightnessState(final BrightnessState brightnessState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, brightnessState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set brightnessState", ex); } }
@Override public Future<ActionFuture> setColorState(final ColorState colorState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, colorState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set colorState", ex); } }
@Override public Future<ActionFuture> setBlindState(final BlindState blindState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, blindState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set blindState", ex); } }
@Override public Future<ActionFuture> setStandbyState(final StandbyState standbyState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, standbyState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set standbyState", ex); } }
@Override public Future<ActionFuture> setActivationState(final ActivationState activationState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, activationState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set activationState", ex); } }
@Override public Future<ActionFutureType.ActionFuture> setEmphasisState(EmphasisState emphasisState) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); try { return applyAction(Services.updateActionDescription(actionDescription, emphasisState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set brightnessState", ex); } }
@Override public Future<ActionFuture> setPowerState(final PowerState powerState, final UnitType unitType) throws CouldNotPerformException { ActionDescription.Builder actionDescription = ActionDescriptionProcessor.getActionDescription(ActionAuthority.getDefaultInstance(), ResourceAllocation.Initiator.SYSTEM); ServiceStateDescription.Builder serviceStateDescription = actionDescription.getServiceStateDescriptionBuilder(); serviceStateDescription.setUnitType(unitType); try { return applyAction(Services.updateActionDescription(actionDescription, powerState, getServiceType()).build()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new CouldNotPerformException("Could not set powerState", ex); } }