Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [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.gnmi.api; |
| 18 | |
Yi Tseng | 9619d80 | 2020-03-19 23:28:31 +0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import gnmi.Gnmi; |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 21 | import gnmi.Gnmi.Path; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | /** |
| 27 | * Utilities for gNMI protocol. |
| 28 | */ |
| 29 | public final class GnmiUtils { |
| 30 | |
| 31 | private GnmiUtils() { |
| 32 | // Hide default constructor |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Convert gNMI path to human readable string. |
| 37 | * |
| 38 | * @param path the gNMI path |
| 39 | * @return readable string of the path |
| 40 | */ |
| 41 | public static String pathToString(Path path) { |
| 42 | StringBuilder pathStringBuilder = new StringBuilder(); |
| 43 | |
| 44 | path.getElemList().forEach(elem -> { |
| 45 | pathStringBuilder.append("/").append(elem.getName()); |
| 46 | if (elem.getKeyCount() > 0) { |
| 47 | pathStringBuilder.append("["); |
| 48 | List<String> keys = elem.getKeyMap().entrySet().stream() |
| 49 | .map(entry -> entry.getKey() + "=" + entry.getValue()) |
| 50 | .collect(Collectors.toList()); |
| 51 | pathStringBuilder.append(String.join(", ", keys)); |
| 52 | pathStringBuilder.append("]"); |
| 53 | } |
| 54 | }); |
| 55 | return pathStringBuilder.toString(); |
| 56 | } |
Yi Tseng | 9619d80 | 2020-03-19 23:28:31 +0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Helper class which builds gNMI path. |
| 60 | * |
| 61 | * Example usage: |
| 62 | * Path: /interfaces/interface[name=if1]/state/oper-status |
| 63 | * Java code: |
| 64 | * <code> |
| 65 | * Gnmi.Path path = GnmiPathBuilder.newBuilder() |
| 66 | * .addElem("interfaces") |
| 67 | * .addElem("interface").withKeyValue("name", "if1") |
| 68 | * .addElem("state") |
| 69 | * .addElem("oper-status") |
| 70 | * .build(); |
| 71 | * </code> |
| 72 | */ |
| 73 | public static final class GnmiPathBuilder { |
| 74 | List<Gnmi.PathElem> elemList; |
| 75 | private GnmiPathBuilder() { |
| 76 | elemList = Lists.newArrayList(); |
| 77 | } |
| 78 | |
| 79 | public static GnmiPathBuilder newBuilder() { |
| 80 | return new GnmiPathBuilder(); |
| 81 | } |
| 82 | |
| 83 | public GnmiPathBuilder addElem(String elemName) { |
| 84 | Gnmi.PathElem elem = |
| 85 | Gnmi.PathElem.newBuilder() |
| 86 | .setName(elemName) |
| 87 | .build(); |
| 88 | elemList.add(elem); |
| 89 | return this; |
| 90 | } |
| 91 | public GnmiPathBuilder withKeyValue(String key, String value) { |
| 92 | if (elemList.isEmpty()) { |
| 93 | // Invalid case. ignore it |
| 94 | return this; |
| 95 | } |
| 96 | Gnmi.PathElem lastElem = elemList.remove(elemList.size() - 1); |
| 97 | Gnmi.PathElem newElem = |
| 98 | Gnmi.PathElem.newBuilder(lastElem) |
| 99 | .putKey(key, value) |
| 100 | .build(); |
| 101 | elemList.add(newElem); |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | public Gnmi.Path build() { |
| 106 | return Gnmi.Path.newBuilder().addAllElem(elemList).build(); |
| 107 | } |
| 108 | |
| 109 | } |
Yi Tseng | e616d75 | 2018-11-27 10:53:27 -0800 | [diff] [blame] | 110 | } |