blob: 98f2599d42fb4540c82fc680b606617727e40435 [file] [log] [blame]
alshabib8c2a8b32015-03-31 16:31:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
20
andreaed976a42015-10-05 14:38:25 -070021import java.util.Objects;
22
alshabib8c2a8b32015-03-31 16:31:03 -070023/**
24 * Represents information for a device to connect to a controller.
25 */
26public class ControllerInfo {
27
andreaed976a42015-10-05 14:38:25 -070028 private IpAddress ip = IpAddress.valueOf("0.0.0.0");
29 private int port = 6653;
30 private String type = "error";
alshabib8c2a8b32015-03-31 16:31:03 -070031
alshabibfaa1e362015-04-02 15:01:54 -070032 /**
33 * Information for contacting the controller.
34 *
andreaed976a42015-10-05 14:38:25 -070035 * @param ip the ip address
36 * @param port the tcp port
Brian O'Connor52515622015-10-09 17:03:44 -070037 * @param type the connection type
alshabibfaa1e362015-04-02 15:01:54 -070038 */
andreaed976a42015-10-05 14:38:25 -070039 public ControllerInfo(IpAddress ip, int port, String type) {
alshabib8c2a8b32015-03-31 16:31:03 -070040 this.ip = ip;
andreaed976a42015-10-05 14:38:25 -070041 this.port = port;
42 this.type = type;
43 }
44
45 /**
46 * Information for contacting the controller, if some information
47 * is not contained in the target string because it's optional
48 * it's leaved as in the field declaration (default values).
49 *
50 * @param target column returned from ovsdb query
51 */
52 public ControllerInfo(String target) {
53 String[] data = target.split(":");
54 this.type = data[0];
55 Preconditions.checkArgument(!data[0].contains("unix"),
56 "Unable to create controller info " +
57 "from {} because it's based " +
58 "on unix sockets", target);
59 if (data[0].startsWith("p")) {
60 if (data.length >= 2) {
61 this.port = Integer.parseInt(data[1]);
62 }
63 if (data.length == 3) {
64 this.ip = IpAddress.valueOf(data[2]);
65 }
66 } else {
67 this.ip = IpAddress.valueOf(data[1]);
68 if (data.length == 3) {
69 this.port = Integer.parseInt(data[2]);
70 }
71 }
72 }
73
74 /**
75 * Exposes the ip address of the controller.
76 *
77 * @return IpAddress ip address
78 */
79 public IpAddress ip() {
80 return ip;
81 }
82
83 /**
84 * Exposes the tcp port of the controller.
85 *
86 * @return int tcp port
87 */
88 public int port() {
89 return port;
90 }
91
92 /**
93 * Exposes the type of the controller connection.
94 *
95 * @return String type
96 */
97 public String type() {
98 return type;
99 }
100
101 public String target() {
102 if (type.startsWith("p")) {
103 return type + ":" + port + ":" + ip;
104 } else {
105 return type + ":" + ip + ":" + port;
106 }
107 }
108
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(ip, port, type);
113 }
114
115 @Override
116 public boolean equals(Object toBeCompared) {
117 if (toBeCompared instanceof ControllerInfo) {
118 ControllerInfo controllerInfo = (ControllerInfo) toBeCompared;
119 if (controllerInfo.type().equals(this.type)
120 && controllerInfo.ip().equals(this.ip())
121 && controllerInfo.port() == this.port) {
122 return true;
123 }
124 }
125 return false;
alshabib8c2a8b32015-03-31 16:31:03 -0700126 }
127}