Fix license header violations in netconf
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfEXICodec.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.netconf.nettyutil.handler;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import org.openexi.proc.HeaderOptionsOutputType;
16 import org.openexi.proc.common.EXIOptions;
17 import org.openexi.proc.common.EXIOptionsException;
18 import org.openexi.proc.common.GrammarOptions;
19 import org.openexi.proc.grammars.GrammarCache;
20 import org.openexi.sax.EXIReader;
21 import org.openexi.sax.Transmogrifier;
22 import org.openexi.sax.TransmogrifierException;
23 import org.xml.sax.EntityResolver;
24 import org.xml.sax.InputSource;
25
26 public final class NetconfEXICodec {
27     /**
28      * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it
29      * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head
30      * of the stream. This is really useful, so let's output it now.
31      */
32     private static final boolean OUTPUT_EXI_COOKIE = true;
33     /**
34      * OpenEXI does not allow us to directly prevent resolution of external entities. In order
35      * to prevent XXE attacks, we reuse a single no-op entity resolver.
36      */
37     private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() {
38         @Override
39         public InputSource resolveEntity(final String publicId, final String systemId) {
40             return new InputSource();
41         }
42     };
43
44     /**
45      * Since we have a limited number of options we can have, instantiating a weak cache
46      * will allow us to reuse instances where possible.
47      */
48     private static final LoadingCache<Short, GrammarCache> GRAMMAR_CACHES = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Short, GrammarCache>() {
49         @Override
50         public GrammarCache load(final Short key) {
51             return new GrammarCache(key);
52         }
53     });
54
55     /**
56      * Grammar cache acts as a template and is duplicated by the Transmogrifier and the Reader
57      * before use. It is safe to reuse a single instance.
58      */
59     private final GrammarCache exiGrammarCache;
60     private final EXIOptions exiOptions;
61
62     public NetconfEXICodec(final EXIOptions exiOptions) {
63         this.exiOptions = Preconditions.checkNotNull(exiOptions);
64         this.exiGrammarCache = createGrammarCache(exiOptions);
65     }
66
67     private static GrammarCache createGrammarCache(final EXIOptions exiOptions) {
68         short go = GrammarOptions.DEFAULT_OPTIONS;
69         if (exiOptions.getPreserveComments()) {
70             go = GrammarOptions.addCM(go);
71         }
72         if (exiOptions.getPreserveDTD()) {
73             go = GrammarOptions.addDTD(go);
74         }
75         if (exiOptions.getPreserveNS()) {
76             go = GrammarOptions.addNS(go);
77         }
78         if (exiOptions.getPreservePIs()) {
79             go = GrammarOptions.addPI(go);
80         }
81
82         return GRAMMAR_CACHES.getUnchecked(go);
83     }
84
85     EXIReader getReader() throws EXIOptionsException {
86         final EXIReader r = new EXIReader();
87         r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
88         r.setGrammarCache(exiGrammarCache);
89         r.setEntityResolver(ENTITY_RESOLVER);
90         return r;
91     }
92
93     Transmogrifier getTransmogrifier() throws EXIOptionsException, TransmogrifierException {
94         final Transmogrifier transmogrifier = new Transmogrifier();
95         transmogrifier.setAlignmentType(exiOptions.getAlignmentType());
96         transmogrifier.setBlockSize(exiOptions.getBlockSize());
97         transmogrifier.setGrammarCache(exiGrammarCache);
98         transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
99         transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
100         transmogrifier.setResolveExternalGeneralEntities(false);
101         return transmogrifier;
102     }
103 }