70de8d359e5be9a52f44e38c5abe4d76b5dfbc2c
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / YangSchemaExportBodyWriter.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.util.concurrent.ExecutionException;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.Provider;
20 import org.opendaylight.restconf.common.schema.SchemaExportContext;
21 import org.opendaylight.yangtools.yang.common.Revision;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26
27 @Provider
28 @Produces(YangConstants.RFC6020_YANG_MEDIA_TYPE)
29 public class YangSchemaExportBodyWriter extends AbstractSchemaExportBodyWriter {
30     @Override
31     public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType,
32             final Annotation[] annotations, final MediaType mediaType,
33             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {
34         final Module module = context.getModule();
35         final SourceIdentifier sourceId = new SourceIdentifier(module.getName(),
36                 module.getQNameModule().getRevision().map(Revision::toString).orElse(null));
37         final YangTextSchemaSource yangTextSchemaSource;
38         try {
39             yangTextSchemaSource = context.getSourceProvider().getSource(sourceId).get();
40         } catch (InterruptedException | ExecutionException e) {
41             throw new WebApplicationException("Unable to retrieve source from SourceProvider.", e);
42         }
43         yangTextSchemaSource.copyTo(entityStream);
44     }
45 }