Merge "Fix thread safety issue with StartExi operation"
[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 org.opendaylight.controller.netconf.util.xml.XmlElement;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.List;
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) {
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 = xmlElement.getTextContent();
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         Preconditions.checkNotNull(textContent, "This element should contain text %s", xmlElement);
41         return AttributeConfigElement.create(postprocessNullableDefault(getNullableDefault()),
42                 postprocessParsedValue(textContent));
43     }
44
45     @Override
46     protected Object postprocessNullableDefault(String nullableDefault) {
47         return nullableDefault;
48     }
49
50     protected Object postprocessParsedValue(String textContent) {
51         return textContent;
52     }
53
54 }