diff options
Diffstat (limited to 'framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java')
-rw-r--r-- | framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java | 100 |
1 files changed, 94 insertions, 6 deletions
diff --git a/framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java b/framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java index 9ff808a9..ded3b3ae 100644 --- a/framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java +++ b/framework/src/onos/core/api/src/main/java/org/onosproject/net/behaviour/ControllerInfo.java @@ -15,24 +15,112 @@ */ package org.onosproject.net.behaviour; +import com.google.common.base.Preconditions; import org.onlab.packet.IpAddress; +import java.util.Objects; + /** * Represents information for a device to connect to a controller. */ public class ControllerInfo { - public final IpAddress ip; - public final int tcpPort; + private IpAddress ip = IpAddress.valueOf("0.0.0.0"); + private int port = 6653; + private String type = "error"; /** * Information for contacting the controller. * - * @param ip the ip address - * @param tcpPort the tcp port + * @param ip the ip address + * @param port the tcp port */ - public ControllerInfo(IpAddress ip, int tcpPort) { + public ControllerInfo(IpAddress ip, int port, String type) { this.ip = ip; - this.tcpPort = tcpPort; + this.port = port; + this.type = type; + } + + /** + * Information for contacting the controller, if some information + * is not contained in the target string because it's optional + * it's leaved as in the field declaration (default values). + * + * @param target column returned from ovsdb query + */ + public ControllerInfo(String target) { + String[] data = target.split(":"); + this.type = data[0]; + Preconditions.checkArgument(!data[0].contains("unix"), + "Unable to create controller info " + + "from {} because it's based " + + "on unix sockets", target); + if (data[0].startsWith("p")) { + if (data.length >= 2) { + this.port = Integer.parseInt(data[1]); + } + if (data.length == 3) { + this.ip = IpAddress.valueOf(data[2]); + } + } else { + this.ip = IpAddress.valueOf(data[1]); + if (data.length == 3) { + this.port = Integer.parseInt(data[2]); + } + } + } + + /** + * Exposes the ip address of the controller. + * + * @return IpAddress ip address + */ + public IpAddress ip() { + return ip; + } + + /** + * Exposes the tcp port of the controller. + * + * @return int tcp port + */ + public int port() { + return port; + } + + /** + * Exposes the type of the controller connection. + * + * @return String type + */ + public String type() { + return type; + } + + public String target() { + if (type.startsWith("p")) { + return type + ":" + port + ":" + ip; + } else { + return type + ":" + ip + ":" + port; + } + } + + + @Override + public int hashCode() { + return Objects.hash(ip, port, type); + } + + @Override + public boolean equals(Object toBeCompared) { + if (toBeCompared instanceof ControllerInfo) { + ControllerInfo controllerInfo = (ControllerInfo) toBeCompared; + if (controllerInfo.type().equals(this.type) + && controllerInfo.ip().equals(this.ip()) + && controllerInfo.port() == this.port) { + return true; + } + } + return false; } } |