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