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 REST call to get orphan links #4115

Merged
merged 4 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
import org.openhab.core.io.rest.Stream2JSONInputStream;
import org.openhab.core.io.rest.core.link.BrokenItemChannelLinkDTO;
import org.openhab.core.io.rest.core.link.EnrichedItemChannelLinkDTO;
import org.openhab.core.io.rest.core.link.EnrichedItemChannelLinkDTOMapper;
import org.openhab.core.items.Item;
Expand All @@ -52,6 +53,7 @@
import org.openhab.core.thing.link.AbstractLink;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.link.ItemChannelLinkRegistry.ItemChannelLinkProblem;
import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
import org.openhab.core.thing.link.dto.ItemChannelLinkDTO;
import org.openhab.core.thing.profiles.ProfileType;
Expand Down Expand Up @@ -87,6 +89,7 @@
* @author Yannick Schaus - Added filters to getAll
* @author Markus Rathgeb - Migrated to JAX-RS Whiteboard Specification
* @author Wouter Born - Migrated to OpenAPI annotations
* @author Arne Seime - Added orphan links detection
*/
@Component(service = { RESTResource.class, ItemChannelLinkResource.class })
@JaxrsResource
Expand Down Expand Up @@ -314,4 +317,18 @@ private boolean isEditable(String linkId) {
}
return thing.getChannel(channelUID);
}

@GET
@Path("/orphans")
@Operation(operationId = "getOrphanLinks", summary = "Get orphan links between items and broken/non-existent thing channels", responses = {
@ApiResponse(responseCode = "200", description = "List of broken links") })
public Response getOrphanLinks() {
Map<ItemChannelLink, ItemChannelLinkProblem> orphanLinks = itemChannelLinkRegistry.getOrphanLinks();
List<BrokenItemChannelLinkDTO> brokenLinks = orphanLinks.entrySet().stream()
.map(e -> new BrokenItemChannelLinkDTO(EnrichedItemChannelLinkDTOMapper.map(e.getKey(),
managedItemChannelLinkProvider.get(e.getKey().getUID()) != null), e.getValue()))
.toList();

return Response.ok(brokenLinks).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2010-2024 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.io.rest.core.link;

import org.openhab.core.thing.link.ItemChannelLinkRegistry.ItemChannelLinkProblem;

/**
* Transfer object for broken item channel links.
*
* @author Arne Seime - Initial contribution
*/
public class BrokenItemChannelLinkDTO {
public EnrichedItemChannelLinkDTO itemChannelLink;

public ItemChannelLinkProblem problem;

public BrokenItemChannelLinkDTO(EnrichedItemChannelLinkDTO itemChannelLink, ItemChannelLinkProblem problem) {
this.itemChannelLink = itemChannelLink;
this.problem = problem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
*/
package org.openhab.core.thing.link;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -155,27 +153,54 @@ public int removeLinksForItem(final String itemName) {
}

/**
* Remove all orphaned (item or channel missing) links
* Remove all orphaned (item or channel missing) links that are editable
*
* @see #getOrphanLinks()
* @return the number of removed links
*/
public int purge() {
ManagedProvider<ItemChannelLink, String> managedProvider = getManagedProvider()
.orElseThrow(() -> new IllegalStateException("ManagedProvider is not available"));

Set<String> allItems = itemRegistry.stream().map(Item::getName).collect(Collectors.toSet());
Set<ChannelUID> allChannels = thingRegistry.stream().map(Thing::getChannels).flatMap(List::stream)
.map(Channel::getUID).collect(Collectors.toSet());

Set<String> toRemove = managedProvider.getAll().stream()
.filter(link -> !allItems.contains(link.getItemName()) || !allChannels.contains(link.getLinkedUID()))
.map(ItemChannelLink::getUID).collect(Collectors.toSet());
List<String> toRemove = getOrphanLinks().keySet().stream().map(ItemChannelLink::getUID)
.filter(i -> managedProvider.get(i) != null).toList();

toRemove.forEach(managedProvider::remove);

return toRemove.size();
}

/**
* Get all orphan links (item or channel missing)
*
* @see #purge()
*
* @return a map with orphan links as key and reason they are broken as value
*/
public Map<ItemChannelLink, ItemChannelLinkProblem> getOrphanLinks() {
Collection<Item> items = itemRegistry.getItems();
Collection<Thing> things = thingRegistry.getAll();

Map<ItemChannelLink, ItemChannelLinkProblem> results = new HashMap<>();

Collection<ChannelUID> channelUIDS = things.stream().map(Thing::getChannels).flatMap(List::stream)
.map(Channel::getUID).collect(Collectors.toSet());
Collection<String> itemNames = items.stream().map(Item::getName).collect(Collectors.toSet());

getAll().forEach(itemChannelLink -> {
if (!channelUIDS.contains(itemChannelLink.getLinkedUID())) {
if (!itemNames.contains(itemChannelLink.getItemName())) {
results.put(itemChannelLink, ItemChannelLinkProblem.ITEM_AND_THING_CHANNEL_MISSING);
} else {
results.put(itemChannelLink, ItemChannelLinkProblem.THING_CHANNEL_MISSING);
}
} else if (!itemNames.contains(itemChannelLink.getItemName())) {
results.put(itemChannelLink, ItemChannelLinkProblem.ITEM_MISSING);
}
});
return results;
}

@Override
protected void notifyListenersAboutAddedElement(final ItemChannelLink element) {
super.notifyListenersAboutAddedElement(element);
Expand All @@ -193,4 +218,17 @@ protected void notifyListenersAboutUpdatedElement(final ItemChannelLink oldEleme
super.notifyListenersAboutUpdatedElement(oldElement, element);
// it is not needed to send an event, because links can not be updated
}

private boolean isEditable(String linkId) {
if (getManagedProvider().isPresent()) {
return getManagedProvider().get().get(linkId) != null;
}
return false;
}

public enum ItemChannelLinkProblem {
THING_CHANNEL_MISSING,
ITEM_MISSING,
ITEM_AND_THING_CHANNEL_MISSING;
}
}