blob: b43c2ec1401b7662e1f78c3ec184515ac98b456e [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.drivers.server.impl.devices;
18
19import org.onosproject.drivers.server.devices.CpuDevice;
Georgios Katsikas2ebd8a02018-06-27 18:32:50 +020020import org.onosproject.drivers.server.devices.nic.NicDevice;
Georgios Katsikas83600982017-05-28 20:41:45 +020021import org.onosproject.drivers.server.devices.RestServerSBDevice;
22
23import org.onosproject.protocol.rest.DefaultRestSBDevice;
24import org.onosproject.protocol.rest.RestSBDevice.AuthenticationScheme;
25import org.onlab.packet.IpAddress;
26
27import com.google.common.base.MoreObjects;
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020028import com.google.common.base.Strings;
Georgios Katsikas83600982017-05-28 20:41:45 +020029import com.google.common.collect.Lists;
30
31import java.util.Objects;
32import java.util.Collection;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Default implementation for REST server devices.
38 */
39public class DefaultRestServerSBDevice
40 extends DefaultRestSBDevice implements RestServerSBDevice {
41
42 private Collection<CpuDevice> cpus = Lists.newArrayList();
43 private Collection<NicDevice> nics = Lists.newArrayList();
44
45 public DefaultRestServerSBDevice(
46 IpAddress ip, int port, String name, String password,
47 String protocol, String url, boolean isActive,
48 Collection<CpuDevice> cpus, Collection<NicDevice> nics) {
49 this(
50 ip, port, name, password, protocol, url, isActive,
51 "", "", "", "", AuthenticationScheme.BASIC, "", cpus, nics
52 );
53 }
54
55 public DefaultRestServerSBDevice(
56 IpAddress ip, int port, String name, String password,
57 String protocol, String url, boolean isActive, String testUrl,
58 String manufacturer, String hwVersion, String swVersion,
59 AuthenticationScheme authenticationScheme, String token,
60 Collection<CpuDevice> cpus, Collection<NicDevice> nics) {
61 super(
62 ip, port, name, password, protocol, url, isActive,
63 testUrl, manufacturer, hwVersion, swVersion,
64 authenticationScheme, token
65 );
66
67 checkNotNull(cpus, "Device's set of CPUs cannot be null");
68 checkNotNull(nics, "Device's set of NICs cannot be null");
69
70 this.cpus = cpus;
71 this.nics = nics;
72 }
73
74 @Override
75 public Collection<CpuDevice> cpus() {
76 return this.cpus;
77 }
78
79 @Override
80 public int numberOfCpus() {
81 return this.cpus.size();
82 }
83
84 @Override
85 public Collection<NicDevice> nics() {
86 return this.nics;
87 }
88
89 @Override
90 public int numberOfNics() {
91 return this.nics.size();
92 }
93
94 @Override
Georgios Katsikas80e0b9f2018-07-21 20:29:18 +020095 public long portNumberFromName(String portName) {
96 if (Strings.isNullOrEmpty(portName)) {
97 return -1;
98 }
99
100 for (NicDevice nic : this.nics) {
101 if (nic.name().equals(portName)) {
102 return nic.portNumber();
103 }
104 }
105
106 return -1;
107 }
108
109 @Override
110 public String portNameFromNumber(long portNumber) {
111 if (portNumber < 0) {
112 return "";
113 }
114
115 for (NicDevice nic : this.nics) {
116 if (nic.portNumber() == portNumber) {
117 return nic.name();
118 }
119 }
120
121 return "";
122 }
123
124 @Override
Georgios Katsikas83600982017-05-28 20:41:45 +0200125 public String toString() {
126 return MoreObjects.toStringHelper(this)
127 .omitNullValues()
128 .add("url", url())
Georgios Katsikas83600982017-05-28 20:41:45 +0200129 .add("protocol", protocol())
130 .add("username", username())
Georgios Katsikas83600982017-05-28 20:41:45 +0200131 .add("ip", ip())
Georgios Katsikasdb148472018-10-27 20:48:13 +0200132 .add("port", port())
133 .add("testUrl", testUrl().orElse(null))
Georgios Katsikas83600982017-05-28 20:41:45 +0200134 .add("manufacturer", manufacturer().orElse(null))
135 .add("hwVersion", hwVersion().orElse(null))
136 .add("swVersion", swVersion().orElse(null))
137 .add("cpus", cpus())
138 .add("nics", nics())
139 .toString();
140
141 }
142
143 @Override
144 public boolean equals(Object obj) {
145 if (obj == this) {
146 return true;
147 }
148 if (!(obj instanceof RestServerSBDevice)) {
149 return false;
150 }
151 RestServerSBDevice device = (RestServerSBDevice) obj;
152
153 return this.username().equals(device.username()) &&
154 this.ip().equals(device.ip()) &&
155 this.port() == device.port();
156 }
157
158 @Override
159 public int hashCode() {
160 return Objects.hash(ip(), port(), cpus(), nics());
161 }
162
163}