Skip to content

Commit

Permalink
Move generic repository stuff to bundle org.openhab.core.model.core
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Jul 9, 2023
1 parent 330b8d7 commit c36b8f4
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.semantics.model.yaml;
package org.openhab.core.model.core.internal.yaml;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -24,6 +24,10 @@
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.model.core.yaml.YamlElement;
import org.openhab.core.model.core.yaml.YamlFile;
import org.openhab.core.model.core.yaml.YamlModelListener;
import org.openhab.core.model.core.yaml.YamlParseException;
import org.openhab.core.service.WatchService;
import org.openhab.core.service.WatchService.Kind;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -73,10 +77,12 @@ public void deactivate() {
watchService.unregisterListener(this);
}

// The method is "synchronized" to avoid concurrent files processing
@Override
public synchronized void processWatchEvent(Kind kind, Path path) {
Path fullPath = watchPath.resolve(path);
String dirName = path.subpath(0, 1).toString();
logger.info("dirName={}", dirName);

if (Files.isDirectory(fullPath) || fullPath.toFile().isHidden() || !fullPath.toString().endsWith(".yaml")) {
logger.debug("Ignored {}", fullPath);
Expand All @@ -87,7 +93,7 @@ public synchronized void processWatchEvent(Kind kind, Path path) {
}

private void processWatchEvent(String dirName, Kind kind, Path fullPath, YamlModelListener<?> listener) {
logger.debug("processWatchEvent dirName={} kind={} fullPath={} listener={}", dirName, kind, fullPath,
logger.info("processWatchEvent dirName={} kind={} fullPath={} listener={}", dirName, kind, fullPath,
listener.getClass().getSimpleName());
Map<String, ? extends YamlElement> oldObjects;
Map<String, ? extends YamlElement> newObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.semantics.model.yaml;
package org.openhab.core.model.core.yaml;

/**
* The {@link YamlElement} interface offers an identifier to any element defined in a YAML configuration file.
* 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
*/
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
*
* @throws YamlParseException if something is wrong
*/
void checkValidity() throws YamlParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.semantics.model.yaml;
package org.openhab.core.model.core.yaml;

import java.util.List;

Expand All @@ -26,19 +26,44 @@
@NonNullByDefault
public abstract class YamlFile {

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

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

/**
* Check that the version in the YAML file in the expected one.
*
* @throws YamlParseException if the version in the file is not the expected one
*/
protected abstract void checkVersion() throws YamlParseException;

/**
* Check that the file content is valid.
* It includes the check of the version, the check of duplicated elements (same identifier)
* and the check of each element.
*
* @throws YamlParseException if something is invalid
*/
public void checkValidity() throws YamlParseException {
// Checking version
checkVersion();

// Checking duplicated elements
List<? extends YamlElement> elts = getElements();
long nbDistinctIds = elts.stream().map(YamlElement::getId).distinct().count();
if (nbDistinctIds < elts.size()) {
throw new YamlParseException((elts.size() - nbDistinctIds + 1) + " elements with same ids");
throw new YamlParseException("Elements with same ids detected in the file");
}

// Checking each element
for (int i = 0; i < elts.size(); i++) {
try {
elts.get(i).checkValidity();
Expand Down
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.core.yaml;

import java.util.List;

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 list of added elements
*/
void addedModel(String modelName, List<? 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 list of updated elements
*/
void updatedModel(String modelName, List<? 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 list of removed elements
*/
void removedModel(String modelName, List<? 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 YamlFile> 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
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.semantics.model.yaml;
package org.openhab.core.model.core.yaml;

import org.eclipse.jdt.annotation.NonNullByDefault;

Expand Down
5 changes: 5 additions & 0 deletions bundles/org.openhab.core.semantics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<artifactId>org.openhab.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.model.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.util.Objects;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.semantics.model.yaml.YamlElement;
import org.openhab.core.semantics.model.yaml.YamlParseException;
import org.openhab.core.model.core.yaml.YamlElement;
import org.openhab.core.model.core.yaml.YamlParseException;

/**
* The {@link YamlSemanticTag} is a data transfer object used to serialize a semantic tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.registry.AbstractProvider;
import org.openhab.core.model.core.yaml.YamlElement;
import org.openhab.core.model.core.yaml.YamlFile;
import org.openhab.core.model.core.yaml.YamlModelListener;
import org.openhab.core.semantics.SemanticTag;
import org.openhab.core.semantics.SemanticTagImpl;
import org.openhab.core.semantics.SemanticTagProvider;
import org.openhab.core.semantics.SemanticTagRegistry;
import org.openhab.core.semantics.model.yaml.YamlElement;
import org.openhab.core.semantics.model.yaml.YamlFile;
import org.openhab.core.semantics.model.yaml.YamlModelListener;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.semantics.model.yaml.YamlElement;
import org.openhab.core.semantics.model.yaml.YamlFile;
import org.openhab.core.semantics.model.yaml.YamlParseException;
import org.openhab.core.model.core.yaml.YamlElement;
import org.openhab.core.model.core.yaml.YamlFile;
import org.openhab.core.model.core.yaml.YamlParseException;

/**
* The {@link YamlSemanticTags} is a data transfer object used to serialize a list of semantic tags
Expand Down

This file was deleted.

0 comments on commit c36b8f4

Please sign in to comment.