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