Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bmv2.api.runtime; |
| 18 | |
| 19 | import com.google.common.base.Objects; |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import java.net.URI; |
| 23 | import java.net.URISyntaxException; |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 24 | import java.util.regex.Matcher; |
| 25 | import java.util.regex.Pattern; |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Representation of a BMv2 device. |
| 31 | */ |
| 32 | public final class Bmv2Device { |
| 33 | |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 34 | public static final String N_A = "n/a"; |
| 35 | |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 36 | public static final String SCHEME = "bmv2"; |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 37 | public static final String PROTOCOL = "bmv2-thrift"; |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 38 | public static final String MANUFACTURER = "p4.org"; |
| 39 | public static final String HW_VERSION = "bmv2"; |
Carmelo Cascone | 25f1888 | 2016-06-14 19:16:50 -0700 | [diff] [blame^] | 40 | public static final String SW_VERSION = "1.0.0"; |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 41 | public static final String SERIAL_NUMBER = N_A; |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 42 | |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 43 | private final String thriftServerHost; |
| 44 | private final int thriftServerPort; |
| 45 | private final int internalDeviceId; |
| 46 | |
| 47 | /** |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 48 | * Creates a new BMv2 device object. |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 49 | * |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 50 | * @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 Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 53 | */ |
| 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 Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 61 | * 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 Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 72 | * |
| 73 | * @return a string value |
| 74 | */ |
| 75 | public String thriftServerHost() { |
| 76 | return thriftServerHost; |
| 77 | } |
| 78 | |
| 79 | /** |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 80 | * Returns the port of the Thrift runtime server running on the device. |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 81 | * |
| 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 Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 94 | public int internalDeviceId() { |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 95 | return internalDeviceId; |
| 96 | } |
| 97 | |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 98 | /** |
| 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 Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 106 | return DeviceId.deviceId(new URI(SCHEME, this.thriftServerHost + ":" + this.thriftServerPort, |
| 107 | String.valueOf(this.internalDeviceId))); |
Carmelo Cascone | c0fbbee | 2016-04-27 18:03:36 -0700 | [diff] [blame] | 108 | } catch (URISyntaxException e) { |
| 109 | throw new IllegalArgumentException("Unable to build deviceID for device " + this.toString(), e); |
| 110 | } |
| 111 | } |
| 112 | |
Carmelo Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 113 | @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 Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 134 | 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 Cascone | e4da909 | 2016-04-26 12:14:08 -0700 | [diff] [blame] | 152 | } |
| 153 | } |