Fix NPE when trying to download restconf provided yang files
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / services / simple / impl / RestconfSchemaServiceImpl.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 package org.opendaylight.restconf.nb.rfc8040.services.simple.impl;
9
10 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
11 import org.opendaylight.restconf.common.schema.SchemaExportContext;
12 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
13 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
14 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
15 import org.opendaylight.restconf.nb.rfc8040.services.simple.api.RestconfSchemaService;
16 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 /**
20  * Implementation of {@link RestconfSchemaService}.
21  *
22  */
23 public class RestconfSchemaServiceImpl implements RestconfSchemaService {
24
25     private SchemaContextHandler schemaContextHandler;
26     private DOMMountPointServiceHandler domMountPointServiceHandler;
27     private DOMYangTextSourceProvider sourceProvider;
28
29     /**
30      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
31      * .
32      *
33      * @param schemaContextHandler
34      *             handling schema context
35      * @param domMountPointServiceHandler
36      *             handling dom mount point service
37      */
38     public RestconfSchemaServiceImpl(final SchemaContextHandler schemaContextHandler,
39                                      final DOMMountPointServiceHandler domMountPointServiceHandler,
40                                      final DOMYangTextSourceProvider sourceProvider) {
41         this.schemaContextHandler = schemaContextHandler;
42         this.domMountPointServiceHandler = domMountPointServiceHandler;
43         this.sourceProvider = sourceProvider;
44     }
45
46     @Override
47     public SchemaExportContext getSchema(final String identifier) {
48         final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
49         return ParserIdentifier.toSchemaExportContextFromIdentifier(schemaContextRef.get(), identifier,
50                 this.domMountPointServiceHandler.get(), sourceProvider);
51     }
52
53     @Override
54     public synchronized void updateHandlers(final Object... handlers) {
55         for (final Object object : handlers) {
56             if (object instanceof SchemaContextHandler) {
57                 schemaContextHandler = (SchemaContextHandler) object;
58             } else if (object instanceof DOMMountPointServiceHandler) {
59                 domMountPointServiceHandler = (DOMMountPointServiceHandler) object;
60             } else if (object instanceof DOMYangTextSourceProvider) {
61                 sourceProvider = (DOMYangTextSourceProvider) object;
62             }
63         }
64     }
65 }