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.base.Suppliers;
13 import com.google.common.io.ByteSource;
14 import com.google.common.io.Resources;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.function.Supplier;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException;
21 import org.opendaylight.netconf.shaded.exificient.core.grammars.Grammars;
22 import org.opendaylight.netconf.shaded.exificient.grammars.GrammarFactory;
25 * Enumeration of schema modes defined by the NETCONF EXI capability.
27 public enum EXISchema {
30 Grammars createGrammar() {
31 return GrammarFactory.newInstance().createSchemaLessGrammars();
36 Grammars createGrammar() {
38 return GrammarFactory.newInstance().createXSDTypesOnlyGrammars();
39 } catch (EXIException e) {
40 throw new IllegalStateException("Failed to create builtin grammar", e);
44 BASE_1_1("base:1.1") {
46 Grammars createGrammar() {
47 final ByteSource source = Resources.asByteSource(EXISchema.class.getResource("/rfc6241.xsd"));
48 try (InputStream is = source.openStream()) {
49 final Grammars g = GrammarFactory.newInstance().createGrammars(is);
50 g.setSchemaId(getOption());
52 } catch (EXIException | IOException e) {
53 throw new IllegalStateException("Failed to create RFC6241 grammar", e);
58 private String option;
59 private Supplier<Grammars> grammarsSupplier;
61 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
62 justification = "https://github.com/spotbugs/spotbugs/issues/1867")
63 EXISchema(final String option) {
64 this.option = requireNonNull(option);
65 // Grammar instantiation can be CPU-intensive, hence we instantiate it lazily through a memoizing supplier
66 grammarsSupplier = Suppliers.memoize(this::createGrammar);
69 final String getOption() {
74 * Return the grammar associated with this EXISchema.
76 * @return An EXI grammar.
78 final Grammars getGrammar() {
79 return grammarsSupplier.get();
83 * Create grammars associated with this EXISchema. This is a potentially expensive operation for internal use only,
84 * use {@link #getGrammar()} instead.
86 * @return An EXI grammar.
88 abstract Grammars createGrammar();
90 static @Nullable EXISchema forOption(final String id) {
91 for (EXISchema s : EXISchema.values()) {
92 if (id.equals(s.getOption())) {