Remove the option to deliver streams over WebSockets
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / exi / EXISchema.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.netconf.nettyutil.handler.exi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
23
24 /**
25  * Enumeration of schema modes defined by the NETCONF EXI capability.
26  */
27 public enum EXISchema {
28     NONE("none") {
29         @Override
30         Grammars createGrammar() {
31             return GrammarFactory.newInstance().createSchemaLessGrammars();
32         }
33     },
34     BUILTIN("builtin") {
35         @Override
36         Grammars createGrammar() {
37             try {
38                 return GrammarFactory.newInstance().createXSDTypesOnlyGrammars();
39             } catch (EXIException e) {
40                 throw new IllegalStateException("Failed to create builtin grammar", e);
41             }
42         }
43     },
44     BASE_1_1("base:1.1") {
45         @Override
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());
51                 return g;
52             } catch (EXIException | IOException e) {
53                 throw new IllegalStateException("Failed to create RFC6241 grammar", e);
54             }
55         }
56     };
57
58     private String option;
59     private Supplier<Grammars> grammarsSupplier;
60
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);
67     }
68
69     final String getOption() {
70         return option;
71     }
72
73     /**
74      * Return the grammar associated with this EXISchema.
75      *
76      * @return An EXI grammar.
77      */
78     final Grammars getGrammar() {
79         return grammarsSupplier.get();
80     }
81
82     /**
83      * Create grammars associated with this EXISchema. This is a potentially expensive operation for internal use only,
84      * use {@link #getGrammar()} instead.
85      *
86      * @return An EXI grammar.
87      */
88     abstract Grammars createGrammar();
89
90     static @Nullable EXISchema forOption(final String id) {
91         for (EXISchema s : EXISchema.values()) {
92             if (id.equals(s.getOption())) {
93                 return s;
94             }
95         }
96         return null;
97     }
98 }