Bug 1362: New AsyncWriteTransaction#submit method
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / exi / EXIParameters.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.netconf.nettyutil.handler.exi;
9
10 import org.opendaylight.controller.netconf.api.NetconfMessage;
11 import org.opendaylight.controller.netconf.util.xml.XmlElement;
12 import org.openexi.proc.common.AlignmentType;
13 import org.openexi.proc.common.EXIOptions;
14 import org.openexi.proc.common.EXIOptionsException;
15
16 import com.google.common.base.Preconditions;
17
18 public final class EXIParameters {
19     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
20     private static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
21     private static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
22     private static final String EXI_PARAMETER_COMPRESSED = "compressed";
23     private static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
24
25     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
26     private static final String EXI_FIDELITY_DTD = "dtd";
27     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
28     private static final String EXI_FIDELITY_COMMENTS = "comments";
29     private static final String EXI_FIDELITY_PIS = "pis";
30     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
31
32     private static final String EXI_PARAMETER_SCHEMA = "schema";
33     private static final String EXI_PARAMETER_SCHEMA_NONE = "none";
34     private static final String EXI_PARAMETER_SCHEMA_BUILT_IN = "builtin";
35     private static final String EXI_PARAMETER_SCHEMA_BASE_1_1 = "base:1.1";
36
37     private final EXIOptions options;
38
39     private EXIParameters(final EXIOptions options) {
40         this.options = Preconditions.checkNotNull(options);
41     }
42
43     public static EXIParameters fromNetconfMessage(final NetconfMessage root) throws EXIOptionsException {
44         return fromXmlElement(XmlElement.fromDomDocument(root.getDocument()));
45     }
46
47     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
48         final EXIOptions options =  new EXIOptions();
49
50         options.setAlignmentType(AlignmentType.bitPacked);
51         if (root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT).getLength() > 0) {
52             if (root.getElementsByTagName(EXI_PARAMETER_BIT_PACKED).getLength() > 0) {
53                 options.setAlignmentType(AlignmentType.bitPacked);
54             } else if (root.getElementsByTagName(EXI_PARAMETER_BYTE_ALIGNED).getLength() > 0) {
55                 options.setAlignmentType(AlignmentType.byteAligned);
56             } else if (root.getElementsByTagName(EXI_PARAMETER_COMPRESSED).getLength() > 0) {
57                 options.setAlignmentType(AlignmentType.compress);
58             } else if (root.getElementsByTagName(EXI_PARAMETER_PRE_COMPRESSION).getLength() > 0) {
59                 options.setAlignmentType(AlignmentType.preCompress);
60             }
61         }
62
63         if (root.getElementsByTagName(EXI_PARAMETER_FIDELITY).getLength() > 0) {
64             if (root.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
65                 options.setPreserveDTD(true);
66             }
67             if (root.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
68                 options.setPreserveLexicalValues(true);
69             }
70             if (root.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
71                 options.setPreserveComments(true);
72             }
73             if (root.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
74                 options.setPreservePIs(true);
75             }
76             if (root.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
77                 options.setPreserveNS(true);
78             }
79         }
80
81         if (root.getElementsByTagName(EXI_PARAMETER_SCHEMA).getLength() > 0) {
82 /*
83                         GrammarFactory grammarFactory = GrammarFactory.newInstance();
84                         if (operationElement
85                                 .getElementsByTagName(EXI_PARAMETER_SCHEMA_NONE)
86                                 .getLength() > 0) {
87                             this.grammars = grammarFactory.createSchemaLessGrammars();
88                         }
89
90                         if (operationElement.getElementsByTagName(
91                                 EXI_PARAMETER_SCHEMA_BUILT_IN).getLength() > 0) {
92                             this.grammars = grammarFactory.createXSDTypesOnlyGrammars();
93                         }
94
95                         if (operationElement.getElementsByTagName(
96                                 EXI_PARAMETER_SCHEMA_BASE_1_1).getLength() > 0) {
97                             this.grammars = grammarFactory
98                                     .createGrammars(NETCONF_XSD_LOCATION);
99                         }
100 */
101
102         }
103
104         return new EXIParameters(options);
105     }
106
107     public final EXIOptions getOptions() {
108         return options;
109     }
110 }