blob: 27ac9c91de6ed33d4ad1927fffab3a69e319593b [file] [log] [blame]
Marc De Leenheer57a5af02016-12-02 20:54:41 -08001/*
2 * Copyright 2016-present 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 */
Yuta HIGUCHIc6358352017-06-23 11:56:27 -070016package org.onosproject.tl1;
Marc De Leenheer57a5af02016-12-02 20:54:41 -080017
18import io.netty.channel.Channel;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.TpPort;
Marc De Leenheer57a5af02016-12-02 20:54:41 -080021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a TL1 device.
32 */
33public class DefaultTl1Device implements Tl1Device {
34 private final Logger log = LoggerFactory.getLogger(DefaultTl1Device.class);
35 private static final String TL1 = "tl1";
36
37 private IpAddress ip;
38 private int port;
39 private String username;
40 private String password;
41 private String tid;
42 private Channel channel;
43
44 @Override
45 public void connect(Channel channel) {
46 this.channel = channel;
47 }
48
49 @Override
50 public void disconnect() {
51 this.channel = null;
52 }
53
54 @Override
55 public boolean isConnected() {
56 return channel != null;
57 }
58
59 @Override
60 public IpAddress ip() {
61 return ip;
62 }
63
64 @Override
65 public int port() {
66 return port;
67 }
68
69 @Override
70 public String username() {
71 return username;
72 }
73
74 @Override
75 public String password() {
76 return password;
77 }
78
79 @Override
80 public Channel channel() {
81 return channel;
82 }
83
84 @Override
85 public String tid() {
86 return tid;
87 }
88
89 public DefaultTl1Device(IpAddress ip, int port, String username, String password) {
90 this.ip = checkNotNull(ip);
91 checkArgument((TpPort.MIN_PORT <= port) && (port <= TpPort.MAX_PORT));
92 this.port = port;
93 this.username = checkNotNull(username);
94 this.password = checkNotNull(password);
95 this.tid = null;
96 channel = null;
97 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hash(ip, port, username, password);
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109 if (obj instanceof DefaultTl1Device) {
110 DefaultTl1Device that = (DefaultTl1Device) obj;
111 return Objects.equals(ip, that.ip) &&
112 Objects.equals(port, that.port) &&
113 Objects.equals(username, that.username) &&
114 Objects.equals(password, that.password) &&
115 Objects.equals(tid, that.tid) &&
116 Objects.equals(channel, that.channel);
117 }
118 return false;
119 }
120
121 @Override
122 public String toString() {
123 return toStringHelper(this)
124 .add("ip", ip)
125 .add("port", port)
126 .add("username", username)
127 .add("password", password)
128 .add("tid", tid == null ? "N/A" : tid)
129 .toString();
130 }
131}