Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / DeviceSources.java
1 /*
2  * Copyright (c) 2019 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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.List;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Revision;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
20 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
23
24 /**
25  * Contains RequiredSources - sources from capabilities.
26  */
27 final class DeviceSources {
28     private final Set<QName> requiredSources;
29     private final Set<QName> providedSources;
30     private final SchemaSourceProvider<YangTextSource> sourceProvider;
31
32     DeviceSources(final Set<QName> requiredSources, final Set<QName> providedSources,
33             final SchemaSourceProvider<YangTextSource> sourceProvider) {
34         this.requiredSources = requireNonNull(requiredSources);
35         this.providedSources = requireNonNull(providedSources);
36         this.sourceProvider = requireNonNull(sourceProvider);
37     }
38
39     Set<QName> getRequiredSourcesQName() {
40         return requiredSources;
41     }
42
43     Set<QName> getProvidedSourcesQName() {
44         return providedSources;
45     }
46
47     List<SourceIdentifier> getRequiredSources() {
48         return requiredSources.stream().map(DeviceSources::toSourceId).collect(Collectors.toList());
49     }
50
51     List<Registration> register(final SchemaSourceRegistry schemaRegistry) {
52         return providedSources.stream()
53             .map(DeviceSources::toSourceId)
54             .map(sourceId -> schemaRegistry.registerSchemaSource(sourceProvider,
55                 PotentialSchemaSource.create(sourceId, YangTextSource.class,
56                     PotentialSchemaSource.Costs.REMOTE_IO.getValue())))
57             .collect(Collectors.toUnmodifiableList());
58     }
59
60     private static SourceIdentifier toSourceId(final QName input) {
61         return new SourceIdentifier(input.getLocalName(), input.getRevision().map(Revision::toString).orElse(null));
62     }
63 }