Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
| 17 | package org.onosproject.p4runtime.api; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onosproject.grpc.api.GrpcClientKey; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 23 | import java.io.UnsupportedEncodingException; |
| 24 | import java.net.URI; |
| 25 | import java.net.URLDecoder; |
| 26 | |
| 27 | import static java.lang.String.format; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Key that uniquely identifies a P4Runtime client. |
| 31 | */ |
| 32 | @Beta |
| 33 | public final class P4RuntimeClientKey extends GrpcClientKey { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 34 | |
| 35 | private static final String DEVICE_ID_PARAM = "device_id="; |
| 36 | |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 37 | private static final String P4RUNTIME = "P4Runtime"; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 38 | private final long p4DeviceId; |
| 39 | |
| 40 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 41 | * Creates a new client key. The server URI is expected to carry the |
| 42 | * P4runtime server-internal 'device_id' as a param in the query string. For |
| 43 | * example, grpc://10.0.0.1:5001?device_id=1 |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 44 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 45 | * @param deviceId ONOS device ID |
| 46 | * @param serverUri P4Runtime server URI |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 47 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 48 | public P4RuntimeClientKey(DeviceId deviceId, URI serverUri) { |
| 49 | super(P4RUNTIME, deviceId, serverUri); |
| 50 | this.p4DeviceId = extractP4DeviceId(serverUri); |
| 51 | } |
| 52 | |
| 53 | private static Long extractP4DeviceId(URI uri) { |
| 54 | String[] segments = uri.getRawQuery().split("&"); |
| 55 | try { |
| 56 | for (String s : segments) { |
| 57 | if (s.startsWith(DEVICE_ID_PARAM)) { |
| 58 | return Long.parseUnsignedLong( |
| 59 | URLDecoder.decode( |
| 60 | s.substring(DEVICE_ID_PARAM.length()), "utf-8")); |
| 61 | } |
| 62 | } |
| 63 | } catch (UnsupportedEncodingException e) { |
| 64 | throw new IllegalArgumentException(format( |
| 65 | "Unable to decode P4Runtime-interval device_id from URI %s: %s", |
| 66 | uri, e.toString())); |
| 67 | } catch (NumberFormatException e) { |
| 68 | throw new IllegalArgumentException(format( |
| 69 | "Invalid P4Runtime-interval device_id in URI %s: %s", |
| 70 | uri, e.toString())); |
| 71 | } |
| 72 | throw new IllegalArgumentException(format( |
| 73 | "Missing P4Runtime-interval device_id in URI %s", |
| 74 | uri)); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the P4Runtime server-internal device ID. |
| 79 | * |
| 80 | * @return P4Runtime server-internal device ID |
| 81 | */ |
| 82 | public long p4DeviceId() { |
| 83 | return p4DeviceId; |
| 84 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 85 | } |