SimpleIrdEntryListener implements AutoCloseable, DataTreeChangeListener<IrdConfigurationEntry> { protected Map<ResourceId, List<ResourceId>> resolveDependency( Map<IrdConfigurationEntry, Resource> resources) { Map<ResourceId, Tag> resourceMap = new HashMap<>(); for (Resource resource: resources.values()) { resourceMap.put(resource.getResourceId(), resource.getDefaultTag()); } Map<ResourceId, List<ResourceId>> dependencyMap; dependencyMap = new HashMap<>(); for (Resource resource: resources.values()) { if (resource.getContextTag() == null || resource.getContextTag().isEmpty()) { continue; } Tag tag = resource.getDefaultTag(); if (tag == null) { LOG.error("Error while reading resource {}: no default tag found!", resource.getResourceId().getValue()); return null; } List<InstanceIdentifier<?>> dependencies = null; for (ContextTag ctxTag: resource.getContextTag()) { if (tag.equals(ctxTag.getTag())) { dependencies = ctxTag.getDependency(); } } if (dependencies == null) { LOG.error("Error while reading resource {}: cannot find context tag matching {}", resource.getResourceId().getValue(), tag.getValue()); return null; } List<ResourceId> resourceDependencies = new LinkedList<>(); for (InstanceIdentifier<?> iid: dependencies) { if (!iid.getTargetType().equals(ContextTag.class)) { LOG.error("Dependencies must point to a context tag"); return null; } Uuid contextId = iid.firstKeyOf(Context.class).getContextId(); ResourceId resourceId = iid.firstKeyOf(Resource.class).getResourceId(); Tag contextTag = iid.firstKeyOf(ContextTag.class).getTag(); if (contextId == null || resourceId == null || contextTag == null) { LOG.error("Depends on a resource that doesn't exist!"); return null; } if (!isAcceptableContext(contextId)) { LOG.error("Dependes on a resource that is not in the proper context"); return null; } if (!contextTag.equals(resourceMap.get(resourceId))) { LOG.error("Depends on a mismatched context tag"); return null; } resourceDependencies.add(resourceId); } dependencyMap.put(resource.getResourceId(), resourceDependencies); } return dependencyMap; } SimpleIrdEntryListener(InstanceIdentifier<Resource> instance, Uuid entryContext); void register(DataBroker dataBroker, InstanceIdentifier<IrdConfigurationEntry> iid); @Override synchronized void onDataTreeChanged(Collection<DataTreeModification<IrdConfigurationEntry>> changes); @Override synchronized void close();  }
@Test public void resolveDependency() throws Exception { Map<IrdConfigurationEntry, Resource> resources = new HashMap<>(); IrdConfigurationEntryBuilder iicb = new IrdConfigurationEntryBuilder(); List<ContextTag> contextTagList = new LinkedList<>(); contextTagList.add(new ContextTagBuilder().setTag(new Tag("DEFAULT_CONTEXT_TAG_1")).build()); ResourceBuilder rb = new ResourceBuilder() .setDefaultTag(new Tag("DEFAULT_CONTEXT_TAG_1")) .setContextTag(contextTagList) .setKey(new ResourceKey(new ResourceId("RESOURCE_1"))); resources.put(iicb.build(), rb.build()); simpleIrdEntryListener.resolveDependency(resources); }