2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.connect.netconf.schema;
10 import java.io.InputStream;
11 import java.util.Collection;
12 import java.util.List;
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;
27 import com.google.common.base.Preconditions;
29 public final class NetconfDeviceSchemaProviderFactory implements SchemaContextProviderFactory {
31 private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSchemaProviderFactory.class);
33 private final RemoteDeviceId id;
35 public NetconfDeviceSchemaProviderFactory(final RemoteDeviceId id) {
40 public SchemaContextProvider createContextProvider(final Collection<QName> capabilities, final SchemaSourceProvider<InputStream> sourceProvider) {
42 final YangSourceContext sourceContext = YangSourceContext.createFrom(capabilities, sourceProvider);
44 if (sourceContext.getMissingSources().isEmpty() == false) {
45 logger.warn("{}: Sources for following models are missing {}", id, sourceContext.getMissingSources());
48 logger.debug("{}: Trying to create schema context from {}", id, sourceContext.getValidSources());
49 final List<InputStream> modelsToParse = YangSourceContext.getValidInputStreams(sourceContext);
51 Preconditions.checkState(sourceContext.getValidSources().isEmpty() == false,
52 "%s: Unable to create schema context, no sources provided by device", id);
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);
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);
69 private static final class NetconfSchemaContextProvider implements SchemaContextProvider {
70 private final SchemaContext schemaContext;
72 public NetconfSchemaContextProvider(final SchemaContext schemaContext) {
73 this.schemaContext = schemaContext;
77 public SchemaContext getSchemaContext() {