2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yanglib.impl;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.io.ByteStreams;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.io.IOException;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.Produces;
20 import org.opendaylight.yanglib.api.YangLibService;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Provides schema sources from yang library
31 public class YangLibServiceImpl implements YangLibService {
32 private static final Logger LOG = LoggerFactory.getLogger(YangLibServiceImpl.class);
34 private SharedSchemaRepository schemaRepository;
36 public YangLibServiceImpl() {
40 public void setSchemaRepository(final SharedSchemaRepository schemaRepository) {
41 LOG.debug("Setting schema repository {}", schemaRepository);
42 this.schemaRepository = schemaRepository;
46 public String getSchema(final String name, final String revision) {
47 Preconditions.checkNotNull(schemaRepository, "Schema repository is not initialized");
48 LOG.debug("Attempting load for schema source {}:{}", name, revision);
49 final SourceIdentifier sourceId =
50 new SourceIdentifier(name, Optional.fromNullable(revision.equals("") ? null : revision));
52 final CheckedFuture<YangTextSchemaSource, SchemaSourceException> sourceFuture =
53 schemaRepository.getSchemaSource(sourceId, YangTextSchemaSource.class);
56 final YangTextSchemaSource source = sourceFuture.checkedGet();
57 return new String(ByteStreams.toByteArray(source.openStream()));
58 } catch (SchemaSourceException|IOException e) {
59 throw new IllegalStateException("Unable to get schema" + sourceId, e);