All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService Maven / Gradle / Ivy

/*
 * Copyright (c) 2019 Red Hat, Inc. and others. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.mdsal.dom.spi;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.Beta;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.util.concurrent.ListenableFuture;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;

/**
 * {@link DOMSchemaService} (and {@link DOMYangTextSourceProvider}) implementations backed by a
 * {@link EffectiveModelContextProvider} (and {@link SchemaSourceProvider}) which are known to be fixed and never change
 * schemas.
 *
 * @author Michael Vorburger.ch
 */
@Beta
public class FixedDOMSchemaService extends AbstractDOMSchemaService {
    private static final class WithYangTextSources extends FixedDOMSchemaService implements DOMYangTextSourceProvider {
        private final @NonNull SchemaSourceProvider schemaSourceProvider;

        WithYangTextSources(final EffectiveModelContextProvider schemaContextProvider,
                final SchemaSourceProvider schemaSourceProvider) {
            super(schemaContextProvider);
            this.schemaSourceProvider = requireNonNull(schemaSourceProvider);
        }

        @Override
        public ClassToInstanceMap getExtensions() {
            return ImmutableClassToInstanceMap.of(DOMYangTextSourceProvider.class, this);
        }

        @Override
        public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) {
            return schemaSourceProvider.getSource(sourceIdentifier);
        }
    }

    private final @NonNull EffectiveModelContextProvider schemaContextProvider;

    private FixedDOMSchemaService(final EffectiveModelContextProvider schemaContextProvider) {
        this.schemaContextProvider = requireNonNull(schemaContextProvider);
    }

    public static @NonNull DOMSchemaService of(final EffectiveModelContext effectiveModel) {
        final EffectiveModelContext checked = requireNonNull(effectiveModel);
        return of(() -> checked);
    }

    public static @NonNull DOMSchemaService of(final EffectiveModelContextProvider schemaContextProvider) {
        return new FixedDOMSchemaService(schemaContextProvider);
    }

    public static @NonNull DOMSchemaService of(final EffectiveModelContextProvider schemaContextProvider,
            final SchemaSourceProvider yangTextSourceProvider) {
        return new WithYangTextSources(schemaContextProvider, requireNonNull(yangTextSourceProvider));
    }

    @Override
    public final EffectiveModelContext getGlobalContext() {
        return schemaContextProvider.getEffectiveModelContext();
    }

    @Override
    public final @NonNull ListenerRegistration registerSchemaContextListener(
            final EffectiveModelContextListener listener) {
        listener.onModelContextUpdated(getGlobalContext());
        return NoOpListenerRegistration.of(listener);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy