alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 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.net.behaviour; |
| 17 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 20 | import org.onosproject.net.Annotated; |
| 21 | import org.onosproject.net.Annotations; |
| 22 | import org.onosproject.net.DefaultAnnotations; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 25 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Represents information for a device to connect to a controller. |
| 30 | */ |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 31 | public class ControllerInfo implements Annotated { |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 32 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 33 | private IpAddress ip = IpAddress.valueOf("0.0.0.0"); |
| 34 | private int port = 6653; |
| 35 | private String type = "error"; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 36 | private final Annotations annotations; |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 37 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Information for contacting the controller. |
| 40 | * |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 41 | * @param ip the ip address |
| 42 | * @param port the tcp port |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 43 | * @param type the connection type |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 44 | */ |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 45 | public ControllerInfo(IpAddress ip, int port, String type) { |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 46 | this(ip, port, type, DefaultAnnotations.EMPTY); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 50 | * Information for contacting the controller. |
| 51 | * |
| 52 | * @param ip the ip address |
| 53 | * @param port the tcp port |
| 54 | * @param type the connection type |
| 55 | * @param annotations optional key/value annotations |
| 56 | */ |
| 57 | public ControllerInfo(IpAddress ip, int port, String type, Annotations annotations) { |
| 58 | this.ip = checkNotNull(ip); |
| 59 | this.port = port; |
| 60 | this.type = checkNotNull(type); |
| 61 | this.annotations = checkNotNull(annotations); |
| 62 | } |
| 63 | |
| 64 | // TODO Factory method equivalent to this method |
| 65 | // should probably live in OVSDB, NETCONF package. |
| 66 | /** |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 67 | * Information for contacting the controller, if some information |
| 68 | * is not contained in the target string because it's optional |
| 69 | * it's leaved as in the field declaration (default values). |
| 70 | * |
| 71 | * @param target column returned from ovsdb query |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 72 | * |
| 73 | * @deprecated in Hummingbird (1.7.0) |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 74 | */ |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 75 | @Deprecated |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 76 | public ControllerInfo(String target) { |
| 77 | String[] data = target.split(":"); |
| 78 | this.type = data[0]; |
| 79 | Preconditions.checkArgument(!data[0].contains("unix"), |
| 80 | "Unable to create controller info " + |
| 81 | "from {} because it's based " + |
| 82 | "on unix sockets", target); |
| 83 | if (data[0].startsWith("p")) { |
| 84 | if (data.length >= 2) { |
| 85 | this.port = Integer.parseInt(data[1]); |
| 86 | } |
| 87 | if (data.length == 3) { |
| 88 | this.ip = IpAddress.valueOf(data[2]); |
| 89 | } |
| 90 | } else { |
| 91 | this.ip = IpAddress.valueOf(data[1]); |
| 92 | if (data.length == 3) { |
| 93 | this.port = Integer.parseInt(data[2]); |
| 94 | } |
| 95 | } |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 96 | this.annotations = DefaultAnnotations.EMPTY; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Exposes the ip address of the controller. |
| 101 | * |
| 102 | * @return IpAddress ip address |
| 103 | */ |
| 104 | public IpAddress ip() { |
| 105 | return ip; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Exposes the tcp port of the controller. |
| 110 | * |
| 111 | * @return int tcp port |
| 112 | */ |
| 113 | public int port() { |
| 114 | return port; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Exposes the type of the controller connection. |
| 119 | * |
| 120 | * @return String type |
| 121 | */ |
| 122 | public String type() { |
| 123 | return type; |
| 124 | } |
| 125 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 126 | @Override |
| 127 | public Annotations annotations() { |
| 128 | return annotations; |
| 129 | } |
| 130 | |
| 131 | // TODO Method equivalent to this method |
| 132 | // should probably live in OVSDB, NETCONF package. |
| 133 | // @deprecated in Hummingbird (1.7.0) |
| 134 | @Deprecated |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 135 | public String target() { |
| 136 | if (type.startsWith("p")) { |
| 137 | return type + ":" + port + ":" + ip; |
| 138 | } else { |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 139 | if (annotations.equals(DefaultAnnotations.EMPTY)) { |
| 140 | return type + ":" + ip + ":" + port; |
| 141 | } else { |
| 142 | return type + ":" + ip + ":" + port + ":" + annotations.toString(); |
| 143 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return Objects.hash(ip, port, type); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean equals(Object toBeCompared) { |
| 155 | if (toBeCompared instanceof ControllerInfo) { |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 156 | ControllerInfo that = (ControllerInfo) toBeCompared; |
| 157 | return Objects.equals(this.type, that.type) && |
| 158 | Objects.equals(this.ip, that.ip) && |
| 159 | Objects.equals(this.port, that.port); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 160 | } |
| 161 | return false; |
alshabib | 8c2a8b3 | 2015-03-31 16:31:03 -0700 | [diff] [blame] | 162 | } |
| 163 | } |