blob: 627465e71271e4017f90c5f696d813eefb577444 [file] [log] [blame]
alshabib8c2a8b32015-03-31 16:31:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib8c2a8b32015-03-31 16:31:03 -07003 *
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 */
16package org.onosproject.net.behaviour;
17
andreaed976a42015-10-05 14:38:25 -070018import com.google.common.base.Preconditions;
alshabib8c2a8b32015-03-31 16:31:03 -070019import org.onlab.packet.IpAddress;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090020import org.onosproject.net.Annotated;
21import org.onosproject.net.Annotations;
22import org.onosproject.net.DefaultAnnotations;
23
24import static com.google.common.base.Preconditions.checkNotNull;
alshabib8c2a8b32015-03-31 16:31:03 -070025
andreaed976a42015-10-05 14:38:25 -070026import java.util.Objects;
27
alshabib8c2a8b32015-03-31 16:31:03 -070028/**
29 * Represents information for a device to connect to a controller.
30 */
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090031public class ControllerInfo implements Annotated {
alshabib8c2a8b32015-03-31 16:31:03 -070032
andreaed976a42015-10-05 14:38:25 -070033 private IpAddress ip = IpAddress.valueOf("0.0.0.0");
34 private int port = 6653;
35 private String type = "error";
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090036 private final Annotations annotations;
alshabib8c2a8b32015-03-31 16:31:03 -070037
alshabibfaa1e362015-04-02 15:01:54 -070038 /**
39 * Information for contacting the controller.
40 *
andreaed976a42015-10-05 14:38:25 -070041 * @param ip the ip address
42 * @param port the tcp port
Brian O'Connor52515622015-10-09 17:03:44 -070043 * @param type the connection type
alshabibfaa1e362015-04-02 15:01:54 -070044 */
andreaed976a42015-10-05 14:38:25 -070045 public ControllerInfo(IpAddress ip, int port, String type) {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090046 this(ip, port, type, DefaultAnnotations.EMPTY);
andreaed976a42015-10-05 14:38:25 -070047 }
48
49 /**
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090050 * 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 /**
andreaed976a42015-10-05 14:38:25 -070067 * 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 Yamanouchi54f28e22016-06-24 13:25:21 +090072 *
73 * @deprecated in Hummingbird (1.7.0)
andreaed976a42015-10-05 14:38:25 -070074 */
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090075 @Deprecated
andreaed976a42015-10-05 14:38:25 -070076 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 Yamanouchi54f28e22016-06-24 13:25:21 +090096 this.annotations = DefaultAnnotations.EMPTY;
andreaed976a42015-10-05 14:38:25 -070097 }
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 Yamanouchi54f28e22016-06-24 13:25:21 +0900126 @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
andreaed976a42015-10-05 14:38:25 -0700135 public String target() {
136 if (type.startsWith("p")) {
137 return type + ":" + port + ":" + ip;
138 } else {
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900139 if (annotations.equals(DefaultAnnotations.EMPTY)) {
140 return type + ":" + ip + ":" + port;
141 } else {
142 return type + ":" + ip + ":" + port + ":" + annotations.toString();
143 }
andreaed976a42015-10-05 14:38:25 -0700144 }
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 Yamanouchi54f28e22016-06-24 13:25:21 +0900156 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);
andreaed976a42015-10-05 14:38:25 -0700160 }
161 return false;
alshabib8c2a8b32015-03-31 16:31:03 -0700162 }
163}