Open Kilda Java Documentation
Topology.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.topo;
17 
18 import static java.util.Collections.emptyMap;
19 import static java.util.Collections.unmodifiableMap;
20 
21 import lombok.Value;
22 
23 import java.util.Map;
24 import java.util.TreeSet;
25 
31 @Value
32 public class Topology implements ITopology {
33 
34  // TODO: consider using TopoID or TopoSlug as the key, not String
35  private Map<String, Switch> switches;
36  private Map<String, Link> links;
37 
38  public Topology() {
39  this(emptyMap(), emptyMap());
40  }
41 
42  public Topology(Map<String, Switch> switches, Map<String, Link> links) {
43  this.switches = unmodifiableMap(switches);
44  this.links = unmodifiableMap(links);
45  }
46 
47  String printSwitchConnections() {
48  StringBuilder sb = new StringBuilder(256);
49  sb.append("Links (Abbreviated){\n");
50  for (String key : new TreeSet<>(links.keySet())) {
51  Link link = links.get(key);
52  String s = TopoSlug.toString(link, true);
53  sb.append("\t").append(s).append("\n");
54  }
55  sb.append("}");
56  sb.trimToSize();
57  return sb.toString();
58  }
59 
60  @Override
61  public boolean equivalent(ITopology other) {
62  // size should be the same
63  if (switches.size() != other.getSwitches().size()) {
64  return false;
65  }
66  if (links.size() != other.getLinks().size()) {
67  return false;
68  }
69 
70  // for now, ensure the each entryset matches (ie each is contained in the other)
71  if (!switches.entrySet().containsAll(other.getSwitches().entrySet())) {
72  return false;
73  }
74  if (!other.getSwitches().entrySet().containsAll(switches.entrySet())) {
75  return false;
76  }
77 
78  // ..same strategy for links..
79  if (!links.entrySet().containsAll(other.getLinks().entrySet())) {
80  return false;
81  }
82  if (!other.getLinks().entrySet().containsAll(links.entrySet())) {
83  return false;
84  }
85 
86  return true;
87  }
88 }
Map< String, Switch > getSwitches()
Map< String, Link > getLinks()
static final String toString(Switch aSwitch)
Definition: TopoSlug.java:34
Topology(Map< String, Switch > switches, Map< String, Link > links)
Definition: Topology.java:42
boolean equivalent(ITopology other)
Definition: Topology.java:61