blob: e289615c1f130007167d5a28aea2fe66cde64b33 [file] [log] [blame]
Carmelo Casconee4da9092016-04-26 12:14:08 -07001/*
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 */
16
17package org.onosproject.bmv2.api.runtime;
18
19import com.google.common.base.Objects;
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070020import org.onosproject.net.DeviceId;
21
22import java.net.URI;
23import java.net.URISyntaxException;
Carmelo Casconee4da9092016-04-26 12:14:08 -070024
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Representation of a BMv2 device.
29 */
30public final class Bmv2Device {
31
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070032 public static final String SCHEME = "bmv2";
33 public static final String MANUFACTURER = "p4.org";
34 public static final String HW_VERSION = "bmv2";
35
Carmelo Casconee4da9092016-04-26 12:14:08 -070036 private final String thriftServerHost;
37 private final int thriftServerPort;
38 private final int internalDeviceId;
39
40 /**
41 * Creates a new Bmv2 device object.
42 *
43 * @param thriftServerHost the host of the Thrift runtime server running inside the device
44 * @param thriftServerPort the port of the Thrift runtime server running inside the device
45 * @param internalDeviceId the internal device id
46 */
47 public Bmv2Device(String thriftServerHost, int thriftServerPort, int internalDeviceId) {
48 this.thriftServerHost = checkNotNull(thriftServerHost, "host cannot be null");
49 this.thriftServerPort = checkNotNull(thriftServerPort, "port cannot be null");
50 this.internalDeviceId = internalDeviceId;
51 }
52
53 /**
54 * Returns the hostname (or IP address) of the Thrift runtime server running inside the device.
55 *
56 * @return a string value
57 */
58 public String thriftServerHost() {
59 return thriftServerHost;
60 }
61
62 /**
63 * Returns the port of the Thrift runtime server running inside the device.
64 *
65 * @return an integer value
66 */
67 public int thriftServerPort() {
68 return thriftServerPort;
69 }
70
71 /**
72 * Returns the BMv2-internal device ID, which is an integer arbitrary chosen at device boot.
73 * Such an ID must not be confused with the ONOS-internal {@link org.onosproject.net.DeviceId}.
74 *
75 * @return an integer value
76 */
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070077 public int internalDeviceId() {
Carmelo Casconee4da9092016-04-26 12:14:08 -070078 return internalDeviceId;
79 }
80
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070081 /**
82 * Returns a new ONOS device ID for this device.
83 *
84 * @return a new device ID
85 */
86 public DeviceId asDeviceId() {
87 try {
88 // TODO: include internalDeviceId number in the deviceId URI
89 return DeviceId.deviceId(new URI(SCHEME, this.thriftServerHost + ":" + this.thriftServerPort, null));
90 } catch (URISyntaxException e) {
91 throw new IllegalArgumentException("Unable to build deviceID for device " + this.toString(), e);
92 }
93 }
94
Carmelo Casconee4da9092016-04-26 12:14:08 -070095 @Override
96 public int hashCode() {
97 return Objects.hashCode(thriftServerHost, thriftServerPort, internalDeviceId);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj == null || getClass() != obj.getClass()) {
106 return false;
107 }
108 final Bmv2Device other = (Bmv2Device) obj;
109 return Objects.equal(this.thriftServerHost, other.thriftServerHost)
110 && Objects.equal(this.thriftServerPort, other.thriftServerPort)
111 && Objects.equal(this.internalDeviceId, other.internalDeviceId);
112 }
113
114 @Override
115 public String toString() {
116 return thriftServerHost + ":" + thriftServerPort + "/" + internalDeviceId;
117 }
118}