c2d49c449237274506249139283002e760e31328
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / FixedDOMSchemaService.java
1 /*
2  * Copyright (c) 2019 Red Hat, Inc. 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.mdsal.dom.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.function.Consumer;
16 import java.util.function.Supplier;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
25
26 /**
27  * {@link DOMSchemaService} (and {@link DOMYangTextSourceProvider}) implementations backed by a
28  * {@code Supplier<EffectiveModelContext>} (and {@link SchemaSourceProvider}) which are known to be fixed and never
29  * change schemas.
30  *
31  * @author Michael Vorburger.ch
32  */
33 @Beta
34 @NonNullByDefault
35 public sealed class FixedDOMSchemaService implements DOMSchemaService {
36     private static final class WithYangTextSources extends FixedDOMSchemaService implements DOMYangTextSourceProvider {
37         private final SchemaSourceProvider<YangTextSource> schemaSourceProvider;
38
39         WithYangTextSources(final Supplier<EffectiveModelContext> modelContextSupplier,
40                 final SchemaSourceProvider<YangTextSource> schemaSourceProvider) {
41             super(modelContextSupplier);
42             this.schemaSourceProvider = requireNonNull(schemaSourceProvider);
43         }
44
45         @Override
46         public List<Extension> supportedExtensions() {
47             return List.of(this);
48         }
49
50         @Override
51         public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
52             return schemaSourceProvider.getSource(sourceIdentifier);
53         }
54     }
55
56     private final Supplier<EffectiveModelContext> modelContextSupplier;
57
58     private FixedDOMSchemaService(final Supplier<EffectiveModelContext> modelContextSupplier) {
59         this.modelContextSupplier = requireNonNull(modelContextSupplier);
60     }
61
62     public static DOMSchemaService of(final EffectiveModelContext effectiveModel) {
63         final var checked = requireNonNull(effectiveModel);
64         return new FixedDOMSchemaService(() -> checked);
65     }
66
67     public static DOMSchemaService of(final Supplier<EffectiveModelContext> modelContextSupplier) {
68         return new FixedDOMSchemaService(modelContextSupplier);
69     }
70
71     public static DOMSchemaService of(final Supplier<EffectiveModelContext> modelContextSupplier,
72             final SchemaSourceProvider<YangTextSource> yangTextSourceProvider) {
73         return new WithYangTextSources(modelContextSupplier, requireNonNull(yangTextSourceProvider));
74     }
75
76     @Override
77     public final EffectiveModelContext getGlobalContext() {
78         return modelContextSupplier.get();
79     }
80
81     @Override
82     public final Registration registerSchemaContextListener(final Consumer<EffectiveModelContext> listener) {
83         listener.accept(getGlobalContext());
84         return () -> { };
85     }
86 }