blob: bff414f25444b3d305693797dcc99977732cde63 [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 Cascone17fc9e42016-05-31 11:29:21 -070024import java.util.regex.Matcher;
25import java.util.regex.Pattern;
Carmelo Casconee4da9092016-04-26 12:14:08 -070026
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Representation of a BMv2 device.
31 */
32public final class Bmv2Device {
33
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070034 public static final String N_A = "n/a";
35
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070036 public static final String SCHEME = "bmv2";
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070037 public static final String PROTOCOL = "bmv2-thrift";
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070038 public static final String MANUFACTURER = "p4.org";
39 public static final String HW_VERSION = "bmv2";
Carmelo Cascone25f18882016-06-14 19:16:50 -070040 public static final String SW_VERSION = "1.0.0";
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070041 public static final String SERIAL_NUMBER = N_A;
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070042
Carmelo Casconee4da9092016-04-26 12:14:08 -070043 private final String thriftServerHost;
44 private final int thriftServerPort;
45 private final int internalDeviceId;
46
47 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070048 * Creates a new BMv2 device object.
Carmelo Casconee4da9092016-04-26 12:14:08 -070049 *
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070050 * @param thriftServerHost the hostname / IP address of the Thrift runtime server running on the device
51 * @param thriftServerPort the port of the Thrift runtime server running on the device
52 * @param internalDeviceId the internal device ID number
Carmelo Casconee4da9092016-04-26 12:14:08 -070053 */
54 public Bmv2Device(String thriftServerHost, int thriftServerPort, int internalDeviceId) {
55 this.thriftServerHost = checkNotNull(thriftServerHost, "host cannot be null");
56 this.thriftServerPort = checkNotNull(thriftServerPort, "port cannot be null");
57 this.internalDeviceId = internalDeviceId;
58 }
59
60 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070061 * Returns a Bmv2Device representing the given deviceId.
62 *
63 * @param deviceId a deviceId
64 * @return
65 */
66 public static Bmv2Device of(DeviceId deviceId) {
67 return DeviceIdParser.parse(checkNotNull(deviceId, "deviceId cannot be null"));
68 }
69
70 /**
71 * Returns the hostname (or IP address) of the Thrift runtime server running on the device.
Carmelo Casconee4da9092016-04-26 12:14:08 -070072 *
73 * @return a string value
74 */
75 public String thriftServerHost() {
76 return thriftServerHost;
77 }
78
79 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070080 * Returns the port of the Thrift runtime server running on the device.
Carmelo Casconee4da9092016-04-26 12:14:08 -070081 *
82 * @return an integer value
83 */
84 public int thriftServerPort() {
85 return thriftServerPort;
86 }
87
88 /**
89 * Returns the BMv2-internal device ID, which is an integer arbitrary chosen at device boot.
90 * Such an ID must not be confused with the ONOS-internal {@link org.onosproject.net.DeviceId}.
91 *
92 * @return an integer value
93 */
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070094 public int internalDeviceId() {
Carmelo Casconee4da9092016-04-26 12:14:08 -070095 return internalDeviceId;
96 }
97
Carmelo Casconec0fbbee2016-04-27 18:03:36 -070098 /**
99 * Returns a new ONOS device ID for this device.
100 *
101 * @return a new device ID
102 */
103 public DeviceId asDeviceId() {
104 try {
105 // TODO: include internalDeviceId number in the deviceId URI
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700106 return DeviceId.deviceId(new URI(SCHEME, this.thriftServerHost + ":" + this.thriftServerPort,
107 String.valueOf(this.internalDeviceId)));
Carmelo Casconec0fbbee2016-04-27 18:03:36 -0700108 } catch (URISyntaxException e) {
109 throw new IllegalArgumentException("Unable to build deviceID for device " + this.toString(), e);
110 }
111 }
112
Carmelo Casconee4da9092016-04-26 12:14:08 -0700113 @Override
114 public int hashCode() {
115 return Objects.hashCode(thriftServerHost, thriftServerPort, internalDeviceId);
116 }
117
118 @Override
119 public boolean equals(Object obj) {
120 if (this == obj) {
121 return true;
122 }
123 if (obj == null || getClass() != obj.getClass()) {
124 return false;
125 }
126 final Bmv2Device other = (Bmv2Device) obj;
127 return Objects.equal(this.thriftServerHost, other.thriftServerHost)
128 && Objects.equal(this.thriftServerPort, other.thriftServerPort)
129 && Objects.equal(this.internalDeviceId, other.internalDeviceId);
130 }
131
132 @Override
133 public String toString() {
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700134 return asDeviceId().toString();
135 }
136
137 private static class DeviceIdParser {
138
139 private static final Pattern REGEX = Pattern.compile(SCHEME + ":(.+):(\\d+)#(\\d+)");
140
141 public static Bmv2Device parse(DeviceId deviceId) {
142 Matcher matcher = REGEX.matcher(deviceId.toString());
143 if (matcher.find()) {
144 String host = matcher.group(1);
145 int port = Integer.valueOf(matcher.group(2));
146 int internalDeviceId = Integer.valueOf(matcher.group(3));
147 return new Bmv2Device(host, port, internalDeviceId);
148 } else {
149 throw new RuntimeException("Unable to parse bmv2 device id string: " + deviceId.toString());
150 }
151 }
Carmelo Casconee4da9092016-04-26 12:14:08 -0700152 }
153}