blob: a9cfbf8dfb7c9f7f09c5427f7643515972c12131 [file] [log] [blame]
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001package net.onrc.onos.core.topology;
2
3import java.util.Collections;
4import java.util.Map;
5import java.util.Objects;
6import java.util.concurrent.ConcurrentHashMap;
7import java.util.concurrent.ConcurrentMap;
8
9import org.apache.commons.lang.Validate;
10
11/**
12 * Base class for Topology Elements.
13 * <p/>
14 * Self-contained element, it is expected to be used as if it is an immutable
15 * object.
16 *
17 * @param <T> Sub-class' type.
18 * (Required to define a method returning itself's type)
19 */
20public class TopologyElement<T extends TopologyElement<T>>
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -070021 implements ITopologyElement, StringAttributes, UpdateStringAttributes {
22
23 // TODO: Where should the attribute names be defined?
24 /**
25 * Attribute name for type.
26 */
27 public static final String TYPE = "type";
28 /**
29 * Attribute "type" value representing that the object belongs to Packet layer.
30 */
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -070031 public static final String TYPE_PACKET_LAYER = "packet";
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -070032 /**
33 * Attribute "type" value representing that the object belongs to Optical layer.
34 */
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -070035 public static final String TYPE_OPTICAL_LAYER = "optical";
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -070036
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070037 public static final String ELEMENT_CONFIG_STATE = "ConfigState";
38
39 public static final String ELEMENT_ADMIN_STATUS = "AdminStatus";
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070040
41 private boolean isFrozen = false;
42
43 private ConcurrentMap<String, String> stringAttributes;
44
45
46
47 /**
48 * Default constructor for serializer.
49 */
50 protected TopologyElement() {
51 this.isFrozen = false;
52 this.stringAttributes = new ConcurrentHashMap<>();
53 }
54
55 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070056 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 * <p/>
58 * Sub-classes should do a deep-copies if necessary.
59 *
60 * @param original to make copy of.
61 */
62 public TopologyElement(TopologyElement<T> original) {
63 this.isFrozen = false;
64 this.stringAttributes = new ConcurrentHashMap<>(original.stringAttributes);
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070065
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 }
67
68
69 /**
70 * Tests if this instance is frozen.
71 *
72 * @return true if frozen.
73 */
74 public boolean isFrozen() {
75 return isFrozen;
76 }
77
78 /**
79 * Freezes this instance to avoid further modifications.
80 *
81 * @return this
82 */
83 @SuppressWarnings("unchecked")
84 public T freeze() {
85 isFrozen = true;
86 return (T) this;
87 }
88
89 @Override
90 public int hashCode() {
91 return stringAttributes.hashCode();
92 }
93
94 /*
95 * (non-Javadoc)
96 * Equality based only on string attributes.
97 *
98 * Subclasses should call super.equals().
99 */
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj == null) {
106 return false;
107 }
108 if (getClass() != obj.getClass()) {
109 return false;
110 }
111 @SuppressWarnings("unchecked")
112 TopologyElement<T> other = (TopologyElement<T>) obj;
113 return Objects.equals(stringAttributes, other.stringAttributes);
114 }
115
116 @Override
117 public String getStringAttribute(String attr) {
118 return this.stringAttributes.get(attr);
119 }
120
121 @Override
122 public String getStringAttribute(String attr, String def) {
123 final String v = getStringAttribute(attr);
124 if (v == null) {
125 return def;
126 } else {
127 return v;
128 }
129 }
130
131 @Override
132 public Map<String, String> getAllStringAttributes() {
133 return Collections.unmodifiableMap(this.stringAttributes);
134 }
135
136 @Override
137 public boolean createStringAttribute(String attr, String value) {
138 if (isFrozen) {
139 throw new IllegalStateException("Tried to modify frozen object: " + this);
140 }
141 Validate.notNull(value, "attribute value cannot be null");
142
143 return this.stringAttributes.putIfAbsent(attr, value) == null;
144 }
145
146 @Override
147 public boolean replaceStringAttribute(String attr, String oldValue, String value) {
148 if (isFrozen) {
149 throw new IllegalStateException("Tried to modify frozen object: " + this);
150 }
151 Validate.notNull(value, "attribute value cannot be null");
152
153 return this.stringAttributes.replace(attr, oldValue, value);
154 }
155
156 @Override
157 public boolean deleteStringAttribute(String attr, String expectedValue) {
158 if (isFrozen) {
159 throw new IllegalStateException("Tried to modify frozen object: " + this);
160 }
161
162 return this.stringAttributes.remove(attr, expectedValue);
163 }
164
165 @Override
166 public void deleteStringAttribute(String attr) {
167 if (isFrozen) {
168 throw new IllegalStateException("Tried to modify frozen object: " + this);
169 }
170
171 this.stringAttributes.remove(attr);
172 }
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700173
174 @Override
175 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700176 return getStringAttribute(TYPE, TYPE_PACKET_LAYER);
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700177 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700178
179 /**
180 * Returns the config state of topology element.
181 *
182 * @return ConfigState
183 */
184 @Override
185 public ConfigState getConfigState() {
186 return ConfigState.valueOf(getStringAttribute(ELEMENT_CONFIG_STATE));
187 }
188
189 /**
190 * Returns the status of topology element.
191 *
192 * @return AdminStatus
193 */
194 @Override
195 public AdminStatus getStatus() {
196 return AdminStatus.valueOf(getStringAttribute(ELEMENT_ADMIN_STATUS));
197 }
198
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700199}