blob: 452e978a32535b616d5240c44ae621eb9b06de51 [file] [log] [blame]
Yi Tseng2a340f72018-11-02 16:52:47 -07001/*
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
17package org.onosproject.p4runtime.api;
18
19import com.google.common.annotations.Beta;
20import org.onosproject.grpc.api.GrpcClientKey;
21import org.onosproject.net.DeviceId;
22
Carmelo Cascone3977ea42019-02-28 13:43:42 -080023import java.io.UnsupportedEncodingException;
24import java.net.URI;
25import java.net.URLDecoder;
26
27import static java.lang.String.format;
Yi Tseng2a340f72018-11-02 16:52:47 -070028
29/**
30 * Key that uniquely identifies a P4Runtime client.
31 */
32@Beta
33public final class P4RuntimeClientKey extends GrpcClientKey {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080034
35 private static final String DEVICE_ID_PARAM = "device_id=";
36
Carmelo Casconea71b8492018-12-17 17:47:50 -080037 private static final String P4RUNTIME = "P4Runtime";
Yi Tseng2a340f72018-11-02 16:52:47 -070038 private final long p4DeviceId;
39
40 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080041 * 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 Tseng2a340f72018-11-02 16:52:47 -070044 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -080045 * @param deviceId ONOS device ID
46 * @param serverUri P4Runtime server URI
Yi Tseng2a340f72018-11-02 16:52:47 -070047 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080048 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 Tseng2a340f72018-11-02 16:52:47 -070075 }
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 Tseng2a340f72018-11-02 16:52:47 -070085}