Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / SimpleAttributeReadingStrategy.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
10
11 import com.google.common.base.Preconditions;
12 import java.util.List;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.util.xml.XmlElement;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class SimpleAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
19     private static final Logger logger = LoggerFactory.getLogger(SimpleAttributeReadingStrategy.class);
20
21
22     public SimpleAttributeReadingStrategy(String nullableDefault) {
23         super(nullableDefault);
24     }
25
26     @Override
27     AttributeConfigElement readElementHook(List<XmlElement> configNodes) throws NetconfDocumentedException {
28         XmlElement xmlElement = configNodes.get(0);
29         Preconditions.checkState(configNodes.size() == 1, "This element should be present only once " + xmlElement
30                 + " but was " + configNodes.size());
31
32         String textContent = "";
33         try{
34             textContent = readElementContent(xmlElement);
35         }catch(IllegalStateException | NullPointerException e) {
36             // yuma sends <attribute /> for empty value instead of <attribute></attribute>
37             logger.warn("Ignoring exception caused by failure to read text element", e);
38         }
39
40         if (null == textContent){
41             throw new NetconfDocumentedException(String.format("This element should contain text %s", xmlElement),
42                     NetconfDocumentedException.ErrorType.application,
43                     NetconfDocumentedException.ErrorTag.invalid_value,
44                     NetconfDocumentedException.ErrorSeverity.error);
45         }
46         return AttributeConfigElement.create(postprocessNullableDefault(getNullableDefault()),
47                 postprocessParsedValue(textContent));
48     }
49
50     protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
51         return xmlElement.getTextContent();
52     }
53
54     @Override
55     protected Object postprocessNullableDefault(String nullableDefault) {
56         return nullableDefault;
57     }
58
59     protected Object postprocessParsedValue(String textContent) {
60         return textContent;
61     }
62
63 }