Merge "Add exists method on DOMStoreReadTransaction and DOMDataReadTransaction"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / schema / NetconfDeviceSchemaProviderFactory.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.controller.sal.connect.netconf.schema;
9
10 import java.io.InputStream;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.controller.sal.connect.api.SchemaContextProviderFactory;
16 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
21 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
22 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
23 import org.opendaylight.yangtools.yang.parser.impl.util.YangSourceContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Preconditions;
28
29 public final class NetconfDeviceSchemaProviderFactory implements SchemaContextProviderFactory {
30
31     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSchemaProviderFactory.class);
32
33     private final RemoteDeviceId id;
34
35     public NetconfDeviceSchemaProviderFactory(final RemoteDeviceId id) {
36         this.id = id;
37     }
38
39     @Override
40     public SchemaContextProvider createContextProvider(final Collection<QName> capabilities, final SchemaSourceProvider<InputStream> sourceProvider) {
41
42         final YangSourceContext sourceContext = YangSourceContext.createFrom(capabilities, sourceProvider);
43
44         if (sourceContext.getMissingSources().isEmpty() == false) {
45             logger.warn("{}: Sources for following models are missing {}", id, sourceContext.getMissingSources());
46         }
47
48         logger.debug("{}: Trying to create schema context from {}", id, sourceContext.getValidSources());
49         final List<InputStream> modelsToParse = YangSourceContext.getValidInputStreams(sourceContext);
50
51         Preconditions.checkState(sourceContext.getValidSources().isEmpty() == false,
52                 "%s: Unable to create schema context, no sources provided by device", id);
53         try {
54             final SchemaContext schemaContext = tryToParseContext(modelsToParse);
55             logger.debug("{}: Schema context successfully created.", id);
56             return new NetconfSchemaContextProvider(schemaContext);
57         } catch (final RuntimeException e) {
58             logger.error("{}: Unable to create schema context, unexpected error", id, e);
59             throw new IllegalStateException(id + ": Unable to create schema context", e);
60         }
61     }
62
63     private static SchemaContext tryToParseContext(final List<InputStream> modelsToParse) {
64         final YangParserImpl parser = new YangParserImpl();
65         final Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
66         return parser.resolveSchemaContext(models);
67     }
68
69     private static final class NetconfSchemaContextProvider implements SchemaContextProvider {
70         private final SchemaContext schemaContext;
71
72         public NetconfSchemaContextProvider(final SchemaContext schemaContext) {
73             this.schemaContext = schemaContext;
74         }
75
76         @Override
77         public SchemaContext getSchemaContext() {
78             return schemaContext;
79         }
80     }
81 }