Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import java.util.Collections; |
| 4 | import java.util.Map; |
| 5 | import java.util.Objects; |
| 6 | import java.util.concurrent.ConcurrentHashMap; |
| 7 | import java.util.concurrent.ConcurrentMap; |
| 8 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 9 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 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 | */ |
| 20 | public class TopologyElement<T extends TopologyElement<T>> |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 21 | 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 | /** |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 29 | * Attribute "type" value representing that the object belongs to Packet |
| 30 | * layer. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 31 | */ |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 32 | public static final String TYPE_PACKET_LAYER = "packet"; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 33 | /** |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 34 | * Attribute "type" value representing that the object belongs to Optical |
| 35 | * layer. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 36 | */ |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 37 | public static final String TYPE_OPTICAL_LAYER = "optical"; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Attribute "type" value representing that the object belongs to all |
| 40 | * layers. |
| 41 | */ |
| 42 | public static final String TYPE_ALL_LAYERS = "AllLayers"; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 43 | |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 44 | public static final String ELEMENT_CONFIG_STATE = "ConfigState"; |
| 45 | |
| 46 | public static final String ELEMENT_ADMIN_STATUS = "AdminStatus"; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 47 | |
Praseed Balakrishnan | fa21be1 | 2014-07-15 14:42:26 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Attribute name for device type. |
| 50 | */ |
| 51 | public static final String ELEMENT_TYPE = "ElementType"; |
| 52 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 53 | private boolean isFrozen = false; |
| 54 | |
| 55 | private ConcurrentMap<String, String> stringAttributes; |
| 56 | |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Default constructor for serializer. |
| 61 | */ |
| 62 | protected TopologyElement() { |
| 63 | this.isFrozen = false; |
| 64 | this.stringAttributes = new ConcurrentHashMap<>(); |
| 65 | } |
| 66 | |
| 67 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 68 | * Creates an unfrozen copy of given Object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 69 | * <p/> |
| 70 | * Sub-classes should do a deep-copies if necessary. |
| 71 | * |
| 72 | * @param original to make copy of. |
| 73 | */ |
| 74 | public TopologyElement(TopologyElement<T> original) { |
| 75 | this.isFrozen = false; |
| 76 | this.stringAttributes = new ConcurrentHashMap<>(original.stringAttributes); |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 77 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Tests if this instance is frozen. |
| 83 | * |
| 84 | * @return true if frozen. |
| 85 | */ |
| 86 | public boolean isFrozen() { |
| 87 | return isFrozen; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Freezes this instance to avoid further modifications. |
| 92 | * |
| 93 | * @return this |
| 94 | */ |
| 95 | @SuppressWarnings("unchecked") |
| 96 | public T freeze() { |
| 97 | isFrozen = true; |
| 98 | return (T) this; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int hashCode() { |
| 103 | return stringAttributes.hashCode(); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * (non-Javadoc) |
| 108 | * Equality based only on string attributes. |
| 109 | * |
| 110 | * Subclasses should call super.equals(). |
| 111 | */ |
| 112 | @Override |
| 113 | public boolean equals(Object obj) { |
| 114 | if (this == obj) { |
| 115 | return true; |
| 116 | } |
| 117 | if (obj == null) { |
| 118 | return false; |
| 119 | } |
| 120 | if (getClass() != obj.getClass()) { |
| 121 | return false; |
| 122 | } |
| 123 | @SuppressWarnings("unchecked") |
| 124 | TopologyElement<T> other = (TopologyElement<T>) obj; |
| 125 | return Objects.equals(stringAttributes, other.stringAttributes); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String getStringAttribute(String attr) { |
| 130 | return this.stringAttributes.get(attr); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String getStringAttribute(String attr, String def) { |
| 135 | final String v = getStringAttribute(attr); |
| 136 | if (v == null) { |
| 137 | return def; |
| 138 | } else { |
| 139 | return v; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Map<String, String> getAllStringAttributes() { |
| 145 | return Collections.unmodifiableMap(this.stringAttributes); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean createStringAttribute(String attr, String value) { |
| 150 | if (isFrozen) { |
| 151 | throw new IllegalStateException("Tried to modify frozen object: " + this); |
| 152 | } |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 153 | checkNotNull(value, "attribute value cannot be null"); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 154 | |
| 155 | return this.stringAttributes.putIfAbsent(attr, value) == null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean replaceStringAttribute(String attr, String oldValue, String value) { |
| 160 | if (isFrozen) { |
| 161 | throw new IllegalStateException("Tried to modify frozen object: " + this); |
| 162 | } |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 163 | checkNotNull(value, "attribute value cannot be null"); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 164 | |
| 165 | return this.stringAttributes.replace(attr, oldValue, value); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean deleteStringAttribute(String attr, String expectedValue) { |
| 170 | if (isFrozen) { |
| 171 | throw new IllegalStateException("Tried to modify frozen object: " + this); |
| 172 | } |
| 173 | |
| 174 | return this.stringAttributes.remove(attr, expectedValue); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void deleteStringAttribute(String attr) { |
| 179 | if (isFrozen) { |
| 180 | throw new IllegalStateException("Tried to modify frozen object: " + this); |
| 181 | } |
| 182 | |
| 183 | this.stringAttributes.remove(attr); |
| 184 | } |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 185 | |
| 186 | @Override |
| 187 | public String getType() { |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 188 | return getStringAttribute(TYPE, TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 189 | } |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Returns the config state of topology element. |
| 193 | * |
| 194 | * @return ConfigState |
| 195 | */ |
| 196 | @Override |
| 197 | public ConfigState getConfigState() { |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 198 | return ConfigState.valueOf(getStringAttribute(ELEMENT_CONFIG_STATE, |
| 199 | ConfigState.NOT_CONFIGURED.toString())); |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns the status of topology element. |
| 204 | * |
| 205 | * @return AdminStatus |
| 206 | */ |
| 207 | @Override |
| 208 | public AdminStatus getStatus() { |
| 209 | return AdminStatus.valueOf(getStringAttribute(ELEMENT_ADMIN_STATUS)); |
| 210 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 211 | } |