Open Kilda Java Documentation
TopoSlug.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 java.net.URISyntaxException;
19 
20 import static org.openkilda.topo.TopoSlug.Type.LINK;
21 
25 public class TopoSlug {
26 
27  public static final String DELIM = ":";
28  public static final String EP_DELIM = "->";
29  public static final String NULL_SWITCH = "S.0";
30  public static final String NULL_PORT = NULL_SWITCH + DELIM + "P.0";
31  public static final String NULL_ENDPOINT = NULL_PORT + DELIM + "Q.0";
32  public static final String NULL_LINK = "L" + DELIM + NULL_ENDPOINT + EP_DELIM + NULL_ENDPOINT;
33 
34  public static final String toString(Switch aSwitch) {
35  return "S." + aSwitch.getId();
36  }
37 
38  public static final String toString(Port port) {
39  // NB: mostly a bug to have a null parent .. but allow it for now
40  String parentSlug = (port.getParent() != null) ? port.getParent().getSlug() : NULL_SWITCH;
41  return parentSlug + DELIM + "P." + port.getId();
42  }
43 
44  public static final String toString(PortQueue portQueue) {
45  String parentSlug = (portQueue.getParent() != null)
46  ? portQueue.getParent().getSlug() : NULL_PORT;
47  return parentSlug + DELIM + "Q." + portQueue.getId();
48  }
49 
50  public static final String toString(LinkEndpoint ep){
51  // At present, the Endpoint slug is the same as the Queue slug
52  return (ep == null) ? NULL_ENDPOINT : ep.getPortQueue().getSlug();
53  }
54 
55  public static final String toString (Link link, boolean abbreviate){
56  if (link == null) {
57  return NULL_LINK;
58  } else if (!abbreviate) {
59  return "L" + DELIM + toString(link.getSrc()) + EP_DELIM + toString(link.getDst());
60  } else {
61  // abbreviated form doesn't include "L::", and only includes valid objects;
62  StringBuilder sb = new StringBuilder(64);
63  toAbbrString(sb,link.getSrc());
64  sb.append(EP_DELIM);
65  toAbbrString(sb,link.getDst());
66  return sb.toString();
67  }
68  }
69 
70 
71  /* Create an abbreviated endpoint - minimize based on real values, vs NULL values */
72  private static final void toAbbrString(StringBuilder sb, LinkEndpoint ep){
73  if (ep != null) {
74  sb.append(ep.getTopoSwitch().getId());
75  if (ep.getSwitchPort() != null && ep.getSwitchPort().getSlug() != NULL_PORT)
76  sb.append(DELIM).append(ep.getSwitchPort().getId());
77  if (ep.getPortQueue() != null && ep.getPortQueue().getSlug() != NULL_ENDPOINT)
78  sb.append(DELIM).append(ep.getPortQueue().getId());
79  } else {
80  sb.append(NULL_SWITCH);
81  }
82  }
83 
84  // TODO: We are not using TopoSlug anywhere .. probably should .. if not, kill some of the below
85  private final String slug;
86  private final Type type;
87 
88  // TODO: Consider migrating the static toString methods to the Type enum
89  // TODO: Is the enum needed? It'll help identify the slug type .. tighter integration between types and code.
90  public enum Type {
91  SWITCH('S'), PORT('P'), QUEUE('Q'), LINK('L') ;
92 
93  // Enable a "coded" version of the TopoSlug.Type
94  private final char code;
95  Type(char code){
96  this.code = code;
97  }
98  public char getCode() {
99  return code;
100  }
101  public static Type fromCode(char code){
102  switch (code) {
103  case 'S' : return SWITCH;
104  case 'P' : return PORT;
105  case 'Q' : return QUEUE;
106  case 'L' : return LINK;
107  default: throw new IllegalArgumentException("Uknown TopoSlug.Type: " + code);
108  }
109  }
110  }
111 
112  public TopoSlug(String slug) {
113  this.slug = slug;
114  this.type = Type.fromCode(slug.charAt(0));
115  switch(this.type) {
116  case SWITCH: break;
117  case PORT: break;
118  case QUEUE: break;
119  case LINK: break;
120  }
121  }
122 
132  public static final Link toLink (TopoSlug slug){
133  // TODO: will we create objects from slugs? if so, we'll need to ensure some of the other datamembers are not 'final'
134  return null;
135  }
136 
137 
138  @Override
139  public String toString() {
140  return "TopoSlug{" +
141  "slug='" + slug + '\'' +
142  ", type=" + type +
143  '}';
144  }
145 
146  public static void main(String[] args) {
147  TopoSlug ts = new TopoSlug("Q:123:145:367");
148 
149  System.out.println("WTF: " + ts);
150  }
151 
152 
153 }
static final Link toLink(TopoSlug slug)
Definition: TopoSlug.java:132
static final String toString(Port port)
Definition: TopoSlug.java:38
static final String NULL_SWITCH
Definition: TopoSlug.java:29
static final String toString(Switch aSwitch)
Definition: TopoSlug.java:34
static final String NULL_PORT
Definition: TopoSlug.java:30
static final String toString(LinkEndpoint ep)
Definition: TopoSlug.java:50
static final String NULL_ENDPOINT
Definition: TopoSlug.java:31
static final String NULL_LINK
Definition: TopoSlug.java:32
static final String toString(PortQueue portQueue)
Definition: TopoSlug.java:44
static final String EP_DELIM
Definition: TopoSlug.java:28
static final String DELIM
Definition: TopoSlug.java:27
static void main(String[] args)
Definition: TopoSlug.java:146
static Type fromCode(char code)
Definition: TopoSlug.java:101
static final String toString(Link link, boolean abbreviate)
Definition: TopoSlug.java:55