BUG-3625 Allow replace nested composite nodes in cfg-subsystem
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / AttributeConfigElement.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.Optional;
12 import javax.management.openmbean.OpenType;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
15 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
16
17 /**
18  * Parsed xml element containing configuration for one attribute of an instance
19  * of some module. Contains default value extracted from yang file.
20  */
21 public class AttributeConfigElement {
22     private final Object defaultValue;
23     private final Object value;
24     private final Optional<EditStrategyType> editStrategy;
25
26     private Optional<?> resolvedValue;
27     private Object resolvedDefaultValue;
28     private String jmxName;
29
30     public AttributeConfigElement(Object defaultValue, Object value, final EditStrategyType editStrategyType) {
31         this.defaultValue = defaultValue;
32         this.value = value;
33         this.editStrategy = Optional.fromNullable(editStrategyType);
34     }
35
36     public void setJmxName(String jmxName) {
37         this.jmxName = jmxName;
38     }
39
40     public String getJmxName() {
41         return jmxName;
42     }
43
44     public void resolveValue(AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy,
45             String attrName) throws NetconfDocumentedException {
46         resolvedValue = attributeResolvingStrategy.parseAttribute(attrName, value);
47         Optional<?> resolvedDefault = attributeResolvingStrategy.parseAttribute(attrName, defaultValue);
48         resolvedDefaultValue = resolvedDefault.isPresent() ? resolvedDefault.get() : null;
49     }
50
51     public Optional<EditStrategyType> getEditStrategy() {
52         return editStrategy;
53     }
54
55     public static AttributeConfigElement create(Object nullableDefault, Object value) {
56         return new AttributeConfigElement(nullableDefault, value, null);
57     }
58
59     public static AttributeConfigElement createNullValue(Object nullableDefault) {
60         return new AttributeConfigElement(nullableDefault, null, null);
61     }
62
63     public static AttributeConfigElement create(final String nullableDefault, final Object value, final EditStrategyType editStrategyType) {
64         return new AttributeConfigElement(nullableDefault, value, editStrategyType);
65     }
66
67     public Object getValue() {
68         return value;
69     }
70
71     public Object getDefaultValue() {
72         return defaultValue;
73     }
74
75     public Optional<?> getResolvedValue() {
76         return resolvedValue;
77     }
78
79     public Object getResolvedDefaultValue() {
80         return resolvedDefaultValue;
81     }
82
83     @Override
84     public String toString() {
85         return "AttributeConfigElement [defaultValue=" + defaultValue + ", value=" + value + "]";
86     }
87
88 }