Aihua Guo | 1ce2dd1 | 2016-08-12 23:37:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.tetopology.management.api; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.onosproject.tetopology.management.api.link.NetworkLink; |
| 21 | import org.onosproject.tetopology.management.api.node.NetworkNode; |
| 22 | |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.base.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Default Network implementation. |
| 28 | * <p> |
| 29 | * The Set/Get methods below are defined to accept and pass references because |
| 30 | * the object class is treated as a "composite" object class that holds |
| 31 | * references to various member objects and their relationships, forming a |
| 32 | * data tree. Internal routines of the TE topology manager may use the |
| 33 | * following example methods to construct and manipulate any piece of data in |
| 34 | * the data tree: |
| 35 | * <pre> |
| 36 | * newNode.getTe().setAdminStatus(), or |
| 37 | * newNode.getSupportingNodeIds().add(nodeId), etc. |
| 38 | * </pre> |
| 39 | * Same for constructors where, for example, a child list may be constructed |
| 40 | * first and passed in by reference to its parent object constructor. |
| 41 | */ |
| 42 | public class DefaultNetwork implements Network { |
| 43 | private KeyId networkId; |
| 44 | private List<KeyId> supportingNetworkIds; |
| 45 | private List<NetworkNode> nodes; |
| 46 | private List<NetworkLink> links; |
| 47 | private TeTopologyId teTopologyId; |
| 48 | private boolean serverProvided; |
| 49 | |
| 50 | /** |
| 51 | * Constructor with all fields. |
| 52 | * |
| 53 | * @param networkId network identifier |
| 54 | * @param supportingNetworkIds supporting network identifier |
| 55 | * @param nodes list of nodes within the network |
| 56 | * @param links list of links within the network |
| 57 | * @param teTopologyId TE topology identifier |
| 58 | * @param serverProvided whether the network is received from server |
| 59 | */ |
| 60 | public DefaultNetwork(KeyId networkId, List<KeyId> supportingNetworkIds, |
| 61 | List<NetworkNode> nodes, List<NetworkLink> links, TeTopologyId teTopologyId, |
| 62 | boolean serverProvided) { |
| 63 | this.networkId = networkId; |
| 64 | this.supportingNetworkIds = supportingNetworkIds; |
| 65 | this.nodes = nodes; |
| 66 | this.links = links; |
| 67 | this.teTopologyId = teTopologyId; |
| 68 | this.serverProvided = serverProvided; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Constructor with key only. |
| 73 | * |
| 74 | * @param networkId network identifier |
| 75 | */ |
| 76 | public DefaultNetwork(KeyId networkId) { |
| 77 | this.networkId = networkId; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates an instance of DefaultNetwork from an existing Network object. |
| 82 | * |
| 83 | * @param network network identifier |
| 84 | */ |
| 85 | public DefaultNetwork(Network network) { |
| 86 | this.networkId = network.networkId(); |
| 87 | this.supportingNetworkIds = network.getSupportingNetworkIds(); |
| 88 | this.nodes = network.getNodes(); |
| 89 | this.links = network.getLinks(); |
| 90 | this.teTopologyId = network.getTeTopologyId(); |
| 91 | this.serverProvided = network.isServerProvided(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public KeyId networkId() { |
| 96 | return networkId; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public List<KeyId> getSupportingNetworkIds() { |
| 101 | return supportingNetworkIds; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public List<NetworkNode> getNodes() { |
| 106 | return nodes; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public NetworkNode getNode(KeyId nodeId) { |
| 111 | |
| 112 | for (NetworkNode node : nodes) { |
| 113 | if (node.nodeId().equals(nodeId)) { |
| 114 | return node; |
| 115 | } |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public List<NetworkLink> getLinks() { |
| 122 | return links; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public NetworkLink getLink(KeyId linkId) { |
| 127 | |
| 128 | for (NetworkLink link : links) { |
| 129 | if (link.linkId().equals(linkId)) { |
| 130 | return link; |
| 131 | } |
| 132 | } |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public boolean isServerProvided() { |
| 138 | return serverProvided; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public TeTopologyId getTeTopologyId() { |
| 143 | return teTopologyId; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Sets the supporting network keys. |
| 149 | * |
| 150 | * @param supportingNetworkIds the supportingNetworkIds to set |
| 151 | */ |
| 152 | public void setSupportingNetworkIds(List<KeyId> supportingNetworkIds) { |
| 153 | this.supportingNetworkIds = supportingNetworkIds; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the list of nodes . |
| 158 | * |
| 159 | * @param nodes the nodes to set |
| 160 | */ |
| 161 | public void setNodes(List<NetworkNode> nodes) { |
| 162 | this.nodes = nodes; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets the links. |
| 167 | * |
| 168 | * @param links the links to set |
| 169 | */ |
| 170 | public void setLinks(List<NetworkLink> links) { |
| 171 | this.links = links; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets the attribute serverProvided. |
| 176 | * |
| 177 | * @param serverProvided the attribute to set |
| 178 | */ |
| 179 | public void setServerProvided(boolean serverProvided) { |
| 180 | this.serverProvided = serverProvided; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Sets the TE Topology Id. |
| 185 | * |
| 186 | * @param teTopologyId the teTopologyId to set |
| 187 | */ |
| 188 | public void setTeTopologyId(TeTopologyId teTopologyId) { |
| 189 | this.teTopologyId = teTopologyId; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int hashCode() { |
| 194 | return Objects.hashCode(networkId, supportingNetworkIds, |
| 195 | nodes, links, serverProvided, teTopologyId); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean equals(Object object) { |
| 200 | if (this == object) { |
| 201 | return true; |
| 202 | } |
| 203 | if (object instanceof DefaultNetwork) { |
| 204 | DefaultNetwork that = (DefaultNetwork) object; |
| 205 | return Objects.equal(this.networkId, that.networkId) && |
| 206 | Objects.equal(this.supportingNetworkIds, that.supportingNetworkIds) && |
| 207 | Objects.equal(this.nodes, that.nodes) && |
| 208 | Objects.equal(this.links, that.links) && |
| 209 | Objects.equal(this.serverProvided, that.serverProvided) && |
| 210 | Objects.equal(this.teTopologyId, that.teTopologyId); |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public String toString() { |
| 217 | return MoreObjects.toStringHelper(this) |
| 218 | .add("networkId", networkId) |
| 219 | .add("supportingNetworkIds", supportingNetworkIds) |
| 220 | .add("nodes", nodes) |
| 221 | .add("links", links) |
| 222 | .add("serverProvided", serverProvided) |
| 223 | .add("teTopologyId", teTopologyId) |
| 224 | .toString(); |
| 225 | } |
| 226 | |
| 227 | } |