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