2 * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.netconf.nettyutil.handler.exi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.io.ByteSource;
13 import com.google.common.io.Resources;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException;
17 import org.opendaylight.netconf.shaded.exificient.core.grammars.Grammars;
18 import org.opendaylight.netconf.shaded.exificient.grammars.GrammarFactory;
21 * Enumeration of schema modes defined by the NETCONF EXI capability.
23 public enum EXISchema {
24 NONE("none", GrammarFactory.newInstance().createSchemaLessGrammars()),
25 BUILTIN("builtin", createBuiltinGrammar()),
26 BASE_1_1("base:1.1", createNetconfGrammar());
28 private String option;
29 private Grammars grammar;
31 EXISchema(final String option, final Grammars grammar) {
32 this.option = requireNonNull(option);
33 this.grammar = requireNonNull(grammar);
36 final String getOption() {
40 final Grammars getGrammar() {
44 static EXISchema forOption(final String id) {
45 for (EXISchema s : EXISchema.values()) {
46 if (id.equals(s.getOption())) {
54 private static Grammars createNetconfGrammar() {
55 final ByteSource source = Resources.asByteSource(EXISchema.class.getResource("/rfc6241.xsd"));
56 try (InputStream is = source.openStream()) {
57 final Grammars g = GrammarFactory.newInstance().createGrammars(is);
58 g.setSchemaId("base:1.1");
60 } catch (EXIException | IOException e) {
61 throw new IllegalStateException("Failed to create RFC6241 grammar", e);
65 private static Grammars createBuiltinGrammar() {
67 return GrammarFactory.newInstance().createXSDTypesOnlyGrammars();
68 } catch (EXIException e) {
69 throw new IllegalStateException("Failed to create builtin grammar", e);