Skip to content

Commit

Permalink
Added Version 2.0 Actuation model
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jun 13, 2024
1 parent 4aea388 commit df95983
Show file tree
Hide file tree
Showing 11 changed files with 922 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Plugins/Actuation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>FROST-Server.Plugin.CoreModel</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>FROST-Server.Plugin.CoreModelV2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>FROST-Server.SQLjooq</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static de.fraunhofer.iosb.ilt.frostserver.plugin.actuation.ActuationModelSettings.TAG_ENABLE_ACTUATION;
import static de.fraunhofer.iosb.ilt.frostserver.property.SpecialNames.AT_IOT_ID;
import static de.fraunhofer.iosb.ilt.frostserver.service.InitResult.INIT_DELAY;
import static de.fraunhofer.iosb.ilt.frostserver.service.InitResult.INIT_OK;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
Expand All @@ -29,11 +30,14 @@
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.TableCollection;
import de.fraunhofer.iosb.ilt.frostserver.plugin.coremodel.PluginCoreModel;
import de.fraunhofer.iosb.ilt.frostserver.plugin.coremodelv2.PluginCoreModelV2;
import de.fraunhofer.iosb.ilt.frostserver.plugin.modelloader.PluginModelLoader;
import de.fraunhofer.iosb.ilt.frostserver.property.EntityPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntity;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex;
import de.fraunhofer.iosb.ilt.frostserver.service.InitResult;
import de.fraunhofer.iosb.ilt.frostserver.service.PluginManager;
import de.fraunhofer.iosb.ilt.frostserver.service.PluginModel;
import de.fraunhofer.iosb.ilt.frostserver.service.PluginRootDocument;
import de.fraunhofer.iosb.ilt.frostserver.service.Service;
Expand Down Expand Up @@ -104,14 +108,37 @@ public class PluginActuation implements PluginRootDocument, PluginModel, ConfigD
private ActuationModelSettings modelSettings;
private boolean enabled;
private boolean fullyInitialised;
private boolean isVersion2;

@Override
public InitResult init(CoreSettings settings) {
this.settings = settings;
Settings pluginSettings = settings.getPluginSettings();
enabled = pluginSettings.getBoolean(TAG_ENABLE_ACTUATION, ActuationModelSettings.class);
if (enabled) {
final PluginManager pluginManager = settings.getPluginManager();
modelSettings = new ActuationModelSettings(settings);
boolean pcmV1 = pluginManager.isPluginEnabled(PluginCoreModel.class);
boolean pcmV2 = pluginManager.isPluginEnabled(PluginCoreModelV2.class);
PluginModelLoader pml = pluginManager.getPlugin(PluginModelLoader.class);
if (pcmV1 && !pcmV2) {
LOGGER.info("Using STA Version 1 model.");
isVersion2 = false;
} else if (!pcmV1 && pcmV2) {
LOGGER.info("Using STA Version 2 model.");
isVersion2 = true;
if (pml == null || !pml.isEnabled()) {
LOGGER.warn("PluginModelLoader must be enabled before the Actuation plugin, delaying initialisation...");
return INIT_DELAY;
}
pml.addLiquibaseFile("actuationv2/liquibase/tables.xml");
pml.addModelFile("actuationv2/model/Actuator.json");
pml.addModelFile("actuationv2/model/Task.json");
pml.addModelFile("actuationv2/model/TaskingCapability.json");
} else {
LOGGER.warn("Either CoreModel or CoreModelV2 must be enabled, delaying initialisation...");
return INIT_DELAY;
}
settings.getPluginManager().registerPlugin(this);
}
return INIT_OK;
Expand All @@ -129,6 +156,10 @@ public boolean isEnabled() {

@Override
public void modifyServiceDocument(ServiceRequest request, Map<String, Object> result) {
if (isVersion2) {
// Nothing to do if we run V2.
return;
}
Map<String, Object> serverSettings = (Map<String, Object>) result.get(Service.KEY_SERVER_SETTINGS);
if (serverSettings == null) {
// Nothing to add to.
Expand All @@ -140,6 +171,10 @@ public void modifyServiceDocument(ServiceRequest request, Map<String, Object> re

@Override
public void registerEntityTypes() {
if (isVersion2) {
// Nothing to do if we run V2.
return;
}
LOGGER.info("Initialising Actuation Types...");
ModelRegistry mr = settings.getModelRegistry();

Expand All @@ -154,6 +189,10 @@ public void registerEntityTypes() {

@Override
public boolean linkEntityTypes(PersistenceManager pm) {
if (isVersion2) {
// Nothing to do if we run V2.
return true;
}
LOGGER.info("Linking Actuation Types...");
final PluginCoreModel pluginCoreModel = settings.getPluginManager().getPlugin(PluginCoreModel.class);
if (pluginCoreModel == null || !pluginCoreModel.isFullyInitialised()) {
Expand Down Expand Up @@ -203,6 +242,10 @@ public boolean linkEntityTypes(PersistenceManager pm) {
}

public Map<String, Object> createLiqibaseParams(JooqPersistenceManager ppm, Map<String, Object> target) {
if (isVersion2) {
// Nothing to do if we run V2.
return target;
}
if (target == null) {
target = new LinkedHashMap<>();
}
Expand All @@ -217,6 +260,10 @@ public Map<String, Object> createLiqibaseParams(JooqPersistenceManager ppm, Map<

@Override
public String checkForUpgrades() {
if (isVersion2) {
// Nothing to do if we run V2.
return "";
}
try (PersistenceManager pm = PersistenceManagerFactory.getInstance(settings).create()) {
if (pm instanceof JooqPersistenceManager ppm) {
return ppm.checkForUpgrades(LIQUIBASE_CHANGELOG_FILENAME, createLiqibaseParams(ppm, null));
Expand All @@ -227,6 +274,10 @@ public String checkForUpgrades() {

@Override
public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException {
if (isVersion2) {
// Nothing to do if we run V2.
return true;
}
try (PersistenceManager pm = PersistenceManagerFactory.getInstance(settings).create()) {
if (pm instanceof JooqPersistenceManager ppm) {
return ppm.doUpgrades(LIQUIBASE_CHANGELOG_FILENAME, createLiqibaseParams(ppm, null), out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.1" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!--
Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
Karlsruhe, Germany.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<changeSet author="generated" id="2024-06-10-fk_tasks_tasking_capability_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasks_tasking_capability_id" foreignKeyTableName="tasks" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasks_tasking_capability_id"
baseTableName="tasks" baseColumnNames="tasking_capability_id"
referencedTableName="tasking_capabilities" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasks_feature_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasks_feature_id" foreignKeyTableName="tasks" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasks_feature_id"
baseTableName="tasks" baseColumnNames="feature_id"
referencedTableName="features" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasking_capabilities_actuator_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasking_capabilities_actuator_id" foreignKeyTableName="tasking_capabilities" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasking_capabilities_actuator_id"
baseTableName="tasking_capabilities" baseColumnNames="actuator_id"
referencedTableName="actuators" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasking_capability_observed_property_tasking_capability_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasking_capability_observed_property_tasking_capability_id" foreignKeyTableName="tasking_capability_observed_property" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasking_capability_observed_property_tasking_capability_id"
baseTableName="tasking_capability_observed_property" baseColumnNames="tasking_capability_id"
referencedTableName="tasking_capabilities" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasking_capability_observed_property_observed_property_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasking_capability_observed_property_observed_property_id" foreignKeyTableName="tasking_capability_observed_property" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasking_capability_observed_property_observed_property_id"
baseTableName="tasking_capability_observed_property" baseColumnNames="observed_property_id"
referencedTableName="observed_properties" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasking_capabilities_thing_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasking_capabilities_thing_id" foreignKeyTableName="tasking_capabilities" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasking_capabilities_thing_id"
baseTableName="tasking_capabilities" baseColumnNames="thing_id"
referencedTableName="things" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

<changeSet author="generated" id="2024-06-10-fk_tasking_capabilities_feature_id" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<foreignKeyConstraintExists foreignKeyName="fk_tasking_capabilities_feature_id" foreignKeyTableName="tasking_capabilities" />
</not>
</preConditions>
<addForeignKeyConstraint
constraintName="fk_tasking_capabilities_feature_id"
baseTableName="tasking_capabilities" baseColumnNames="feature_id"
referencedTableName="features" referencedColumnNames="id"
deferrable="false" initiallyDeferred="false"
onDelete="CASCADE" onUpdate="CASCADE"/>
</changeSet>

</databaseChangeLog>

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.1" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!--
Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
Karlsruhe, Germany.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<changeSet author="generated" id="2024-06-10-actuators-1" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<changeLogPropertyDefined property="id-Actuator" value="LONG" />
<not>
<tableExists tableName="actuators" />
</not>
</preConditions>
<createTable tableName="actuators">
<column name="id" type="${idTypeLong}" autoIncrement="true">
<constraints primaryKey="true" primaryKeyName="pk_actuators" />
</column>
</createTable>
</changeSet>

<changeSet author="generated" id="2024-06-10-actuators-2" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<or>
<changeLogPropertyDefined property="id-Actuator" value="STRING" />
<changeLogPropertyDefined property="id-Actuator" value="UUID" />
</or>
<not>
<tableExists tableName="actuators" />
</not>
</preConditions>
<createTable tableName="actuators">
<column name="id" type="${idType-Actuator}" defaultValueComputed="${defaultValueComputed-Actuator}">
<constraints primaryKey="true" primaryKeyName="pk_actuators"/>
</column>
</createTable>
</changeSet>

<changeSet author="generated" id="2024-06-10-actuators-3" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<preConditions onFail="MARK_RAN">
<not>
<columnExists columnName="name" tableName="actuators" />
</not>
</preConditions>
<addColumn tableName="actuators">
<column name="name" type="TEXT"/>
<column name="description" type="TEXT"/>
<column name="encoding_type" type="TEXT"/>
<column name="metadata" type="TEXT"/>
<column name="properties" type="JSONB"/>
</addColumn>
</changeSet>

</databaseChangeLog>

Loading

0 comments on commit df95983

Please sign in to comment.