import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
-import com.google.common.base.Strings;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.File;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.util.HashMap;
-import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
-import java.util.function.Predicate;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.mdsal.binding.api.WriteTransaction;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
import org.opendaylight.yanglib.api.YangLibService;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* along with source identifier to
* ietf-netconf-yang-library/modules-state/module list.
*/
-public class YangLibProvider implements AutoCloseable, SchemaSourceListener, YangLibService {
+@Singleton
+@Component(service = YangLibService.class, configurationPid = "org.opendaylight.netconf.yanglib")
+@Designate(ocd = YangLibProvider.Configuration.class)
+public final class YangLibProvider implements YangLibService, SchemaSourceListener, AutoCloseable {
+ @ObjectClassDefinition
+ public @interface Configuration {
+ @AttributeDefinition(min = "1",
+ description = "Local filesystem folder to use as cache + to load yang models from")
+ @NonNull String cache$_$folder() default "cache/schema";
+
+ @AttributeDefinition(
+ description = "Binding address is necessary for generating proper URLS (accessible from the outside world) "
+ + "for models present directly in the library")
+ @NonNull String binding$_$address() default "localhost";
+
+ @AttributeDefinition(required = true, min = "1", max = "65535",
+ description = "binding port is necessary for generating proper URLS (accessible from the outside world) "
+ + "for models present directly in the library")
+ int binding$_$port() default 8181;
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(YangLibProvider.class);
private static final Predicate<PotentialSchemaSource<?>> YANG_SCHEMA_SOURCE =
input -> YangTextSchemaSource.class.isAssignableFrom(input.getRepresentation());
private final DataBroker dataBroker;
- private final YanglibConfig yanglibConfig;
+ private final String bindingAddress;
+ private final Uint32 bindingPort;
private final SharedSchemaRepository schemaRepository;
- private SchemaListenerRegistration schemaListenerRegistration;
- public YangLibProvider(final YanglibConfig yanglibConfig, final DataBroker dataBroker,
- final YangParserFactory parserFactory) {
- this.yanglibConfig = requireNonNull(yanglibConfig);
- this.dataBroker = requireNonNull(dataBroker);
- schemaRepository = new SharedSchemaRepository("yang-library", parserFactory);
- }
+ private final SchemaListenerRegistration schemaListenerRegistration;
- @Override
- public void close() {
- if (schemaListenerRegistration != null) {
- schemaListenerRegistration.close();
- }
+ @Inject
+ @Activate
+ public YangLibProvider(@Reference final @NonNull DataBroker dataBroker,
+ @Reference final @NonNull YangParserFactory parserFactory, final @NonNull Configuration configuration) {
+ this(dataBroker, parserFactory, configuration.cache$_$folder(), configuration.binding$_$address(),
+ Uint32.valueOf(configuration.binding$_$port()));
}
- public void init() {
- if (Strings.isNullOrEmpty(yanglibConfig.getCacheFolder())) {
- LOG.info("No cache-folder set in yanglib-config - yang library services will not be available");
- return;
- }
+ @VisibleForTesting
+ YangLibProvider(final @NonNull DataBroker dataBroker, final @NonNull YangParserFactory parserFactory,
+ final @NonNull String cacheFolder, final @NonNull String bindingAddress,
+ final @NonNull Uint32 bindingPort) {
+ this.bindingAddress = bindingAddress;
+ this.bindingPort = bindingPort;
+ this.dataBroker = requireNonNull(dataBroker);
- final File cacheFolderFile = new File(yanglibConfig.getCacheFolder());
+ final File cacheFolderFile = new File(cacheFolder);
if (cacheFolderFile.exists()) {
LOG.info("cache-folder {} already exists", cacheFolderFile);
} else {
}
checkArgument(cacheFolderFile.isDirectory(), "cache-folder %s is not a directory", cacheFolderFile);
- final FilesystemSchemaSourceCache<YangTextSchemaSource> cache =
- new FilesystemSchemaSourceCache<>(schemaRepository, YangTextSchemaSource.class, cacheFolderFile);
+ schemaRepository = new SharedSchemaRepository("yang-library", parserFactory);
+ final var cache = new FilesystemSchemaSourceCache<>(schemaRepository, YangTextSchemaSource.class,
+ cacheFolderFile);
schemaRepository.registerSchemaSourceListener(cache);
schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
LOG.info("Started yang library with sources from {}", cacheFolderFile);
}
+ @PreDestroy
+ @Deactivate
+ @Override
+ public void close() {
+ schemaListenerRegistration.close();
+ }
+
@Override
public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
// NOOP
@Override
public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
- final Map<ModuleKey, Module> newModules = new HashMap<>();
+ final var newModules = new HashMap<ModuleKey, Module>();
- for (PotentialSchemaSource<?> potentialYangSource : Iterables.filter(sources, YANG_SCHEMA_SOURCE::test)) {
- final YangIdentifier moduleName =
- new YangIdentifier(potentialYangSource.getSourceIdentifier().name().getLocalName());
+ for (var potentialYangSource : Iterables.filter(sources, YANG_SCHEMA_SOURCE::test)) {
+ final var moduleName = new YangIdentifier(potentialYangSource.getSourceIdentifier().name().getLocalName());
- final Module newModule = new ModuleBuilder()
+ final var newModule = new ModuleBuilder()
.setName(moduleName)
.setRevision(LegacyRevisionUtils.fromYangCommon(
Optional.ofNullable(potentialYangSource.getSourceIdentifier().revision())))
final var yangTextSchemaFuture = schemaRepository.getSchemaSource(sourceId, YangTextSchemaSource.class);
try {
final var yangTextSchemaSource = yangTextSchemaFuture.get();
- return yangTextSchemaSource.asCharSource(StandardCharsets.UTF_8).read();
+ return yangTextSchemaSource.read();
} catch (ExecutionException e) {
if (e.getCause() instanceof MissingSchemaSourceException) {
throw new NotFoundException("Schema source " + sourceId + " not found", e);
}
private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) {
- return new Uri("http://" + yanglibConfig.getBindingAddr() + ':' + yanglibConfig.getBindingPort()
- + "/yanglib/schemas/" + sourceIdentifier.name().getLocalName() + revString(sourceIdentifier));
+ return new Uri("http://" + bindingAddress + ':' + bindingPort + "/yanglib/schemas/"
+ + sourceIdentifier.name().getLocalName() + revString(sourceIdentifier));
}
private static String revString(final SourceIdentifier id) {