Merge "Refactor netconf clustered topology tests"
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / 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.netconf.nettyutil.handler.exi;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.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 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19
20 public final class EXIParameters {
21     private static final String EXI_PARAMETER_ALIGNMENT = "alignment";
22     static final String EXI_PARAMETER_BYTE_ALIGNED = "byte-aligned";
23     static final String EXI_PARAMETER_BIT_PACKED = "bit-packed";
24     static final String EXI_PARAMETER_COMPRESSED = "compressed";
25     static final String EXI_PARAMETER_PRE_COMPRESSION = "pre-compression";
26
27     private static final String EXI_PARAMETER_FIDELITY = "fidelity";
28     private static final String EXI_FIDELITY_DTD = "dtd";
29     private static final String EXI_FIDELITY_LEXICAL_VALUES = "lexical-values";
30     private static final String EXI_FIDELITY_COMMENTS = "comments";
31     private static final String EXI_FIDELITY_PIS = "pis";
32     private static final String EXI_FIDELITY_PREFIXES = "prefixes";
33
34     private final EXIOptions options;
35     private static final Logger LOG = LoggerFactory.getLogger(EXIParameters.class);
36
37     private EXIParameters(final EXIOptions options) {
38         this.options = Preconditions.checkNotNull(options);
39     }
40
41     @SuppressWarnings("checkstyle:FallThrough")
42     public static EXIParameters fromXmlElement(final XmlElement root) throws EXIOptionsException {
43         final EXIOptions options =  new EXIOptions();
44         final NodeList alignmentElements = root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT);
45         if (alignmentElements.getLength() > 0) {
46             final Element alignmentElement = (Element) alignmentElements.item(0);
47             final String alignmentTextContent = alignmentElement.getTextContent().trim();
48
49             switch (alignmentTextContent) {
50                 case EXI_PARAMETER_BYTE_ALIGNED:
51                     options.setAlignmentType(AlignmentType.byteAligned);
52                     break;
53                 case EXI_PARAMETER_COMPRESSED:
54                     options.setAlignmentType(AlignmentType.compress);
55                     break;
56                 case EXI_PARAMETER_PRE_COMPRESSION:
57                     options.setAlignmentType(AlignmentType.preCompress);
58                     break;
59                 default:
60                     LOG.warn("Unexpected value in alignmentTextContent: {} , using default value",
61                             alignmentTextContent);
62                 case EXI_PARAMETER_BIT_PACKED:
63                     options.setAlignmentType(AlignmentType.bitPacked);
64                     break;
65             }
66         } else {
67             options.setAlignmentType(AlignmentType.bitPacked);
68         }
69
70         final NodeList fidelityElements = root.getElementsByTagName(EXI_PARAMETER_FIDELITY);
71         if (fidelityElements.getLength() > 0) {
72             final Element fidelityElement = (Element) fidelityElements.item(0);
73             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0) {
74                 options.setPreserveDTD(true);
75             }
76             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0) {
77                 options.setPreserveLexicalValues(true);
78             }
79             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0) {
80                 options.setPreserveComments(true);
81             }
82             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0) {
83                 options.setPreservePIs(true);
84             }
85             if (fidelityElement.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0) {
86                 options.setPreserveNS(true);
87             }
88         }
89         return new EXIParameters(options);
90     }
91
92     public EXIOptions getOptions() {
93         return options;
94     }
95 }