Refactor NetconfDeviceSchemas
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / api / ProvidedSources.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.client.mdsal.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Set;
13 import java.util.stream.Stream;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
19 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
20 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
22
23 /**
24  * A set of sources provided through a single provider. A NETCONF device can have several of these.
25  *
26  * @param representation the {@link SourceRepresentation} of provided sources
27  * @param provider the {@link SchemaSourceProvider} providing the sources
28  * @param sources provided sources
29  */
30 @NonNullByDefault
31 public record ProvidedSources<T extends SourceRepresentation>(
32         Class<T> representation,
33         SchemaSourceProvider<T> provider,
34         // FIXME: NETCONF-840: use SourceIdentifier
35         Set<QName> sources) {
36     public ProvidedSources {
37         representation = requireNonNull(representation);
38         provider = requireNonNull(provider);
39         sources = Set.copyOf(sources);
40     }
41
42     public Stream<Registration> registerWith(final SchemaSourceRegistry registry, final int cost) {
43         return sources.stream()
44             .map(qname -> new SourceIdentifier(qname.getLocalName(), qname.getRevision().orElse(null)))
45             .map(sourceId -> registry.registerSchemaSource(provider,
46                 PotentialSchemaSource.create(sourceId, representation, cost)));
47     }
48 }