Merge "Update comment and remove unwanted FIXME in SchemaSetup"
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / yanglib / impl / YangLibServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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
9 package org.opendaylight.yanglib.impl;
10
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;
27
28 /**
29  * Provides schema sources from yang library
30  */
31 public class YangLibServiceImpl implements YangLibService {
32     private static final Logger LOG = LoggerFactory.getLogger(YangLibServiceImpl.class);
33
34     private SharedSchemaRepository schemaRepository;
35
36     public YangLibServiceImpl() {
37
38     }
39
40     public void setSchemaRepository(final SharedSchemaRepository schemaRepository) {
41         LOG.debug("Setting schema repository {}", schemaRepository);
42         this.schemaRepository = schemaRepository;
43     }
44
45     @Override
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));
51
52         final CheckedFuture<YangTextSchemaSource, SchemaSourceException> sourceFuture =
53                 schemaRepository.getSchemaSource(sourceId, YangTextSchemaSource.class);
54
55         try {
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);
60         }
61     }
62 }