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

Add a YAML file provider for semantic tags #3659

Merged
merged 19 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bom/openhab-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.model.yaml</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.ui</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions bundles/org.openhab.core.model.yaml/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="annotationpath" value="target/dependency"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="annotationpath" value="target/dependency"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions bundles/org.openhab.core.model.yaml/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.core.model.yaml</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
14 changes: 14 additions & 0 deletions bundles/org.openhab.core.model.yaml/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-core

29 changes: 29 additions & 0 deletions bundles/org.openhab.core.model.yaml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.reactor.bundles</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.core.model.yaml</artifactId>

<name>openHAB Core :: Bundles :: Model YAML</name>

<dependencies>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.semantics</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.yaml;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link AbstractYamlFile} is the DTO base class used to map a YAML configuration file.
*
* A YAML configuration file consists of a version and a list of elements.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractYamlFile implements YamlFile {

private final Logger logger = LoggerFactory.getLogger(AbstractYamlFile.class);

/**
* YAML file version
*/
public int version;

@Override
public abstract List<? extends YamlElement> getElements();

@Override
public int getVersion() {
return version;
}

@Override
public boolean isValid() {
// Checking duplicated elements
List<? extends YamlElement> elts = getElements();
long nbDistinctIds = elts.stream().map(YamlElement::getId).distinct().count();
if (nbDistinctIds < elts.size()) {
logger.debug("Elements with same ids detected in the file");
return false;
}

// Checking each element
for (int i = 0; i < elts.size(); i++) {
if (!elts.get(i).isValid()) {
logger.debug("Error in element {}", i + 1);
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.yaml;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link YamlElement} interface offers an identifier and a check validity method
* to any element defined in a YAML configuration file.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface YamlElement {

/**
* Get the identifier of the YAML element
*
* @return the identifier as a string
*/
String getId();

/**
* Check that the YAML element is valid
*
* @return true if all the checks are OK
*/
boolean isValid();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.yaml;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link YamlFile} is the interface to manage the generic content of a YAML configuration file.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface YamlFile {

/**
* Get the list of elements present in the YAML file.
*
* @return the list of elements
*/
List<? extends YamlElement> getElements();

/**
* Get the version present in the YAML file.
*
* @return the version in the file
*/
int getVersion();

/**
* Check that the file content is valid.
* It includes the check of duplicated elements (same identifier) and the check of each element.
*
* @return true if all the checks are OK
*/
boolean isValid();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.yaml;

import java.util.Collection;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link YamlModelListener} interface is responsible for managing a particular model type
* with data processed from YAML configuration files.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface YamlModelListener<T extends YamlElement> {

/**
* Method called by the model repository when elements from a model are added.
*
* @param modelName the name of the model
* @param elements the collection of added elements
*/
void addedModel(String modelName, Collection<? extends YamlElement> elements);

/**
* Method called by the model repository when elements from a model are updated.
*
* @param modelName the name of the model
* @param elements the collection of updated elements
*/
void updatedModel(String modelName, Collection<? extends YamlElement> elements);

/**
* Method called by the model repository when elements from a model are removed.
*
* @param modelName the name of the model
* @param elements the collection of removed elements
*/
void removedModel(String modelName, Collection<? extends YamlElement> elements);

/**
* Get the root name of this model type which is also the name of the root folder
* containing the user files for this model type.
*
* A path is unexpected. What is expected is for example "items" or "things".
*
* @return the model root name
*/
String getRootName();

/**
* Get the DTO class to be used for a file providing objects for this model type.
*
* @return the DTO file class
*/
Class<? extends AbstractYamlFile> getFileClass();

/**
* Get the DTO class to be used for each object of this model type.
*
* @return the DTO element class
*/
Class<T> getElementClass();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.model.yaml;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link YamlParseException} is used when an error is detected when parsing the content
* of a YAML configuration file.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class YamlParseException extends Exception {

private static final long serialVersionUID = 1L;

public YamlParseException(String message) {
super(message);
}

public YamlParseException(Throwable cause) {
super(cause);
}

public YamlParseException(String message, Throwable cause) {
super(message, cause);
}
}
Loading