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 | |
| 19 | import gnmi.Gnmi.Path; |
| 20 | |
| 21 | import java.util.List; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
| 24 | /** |
| 25 | * Utilities for gNMI protocol. |
| 26 | */ |
| 27 | public final class GnmiUtils { |
| 28 | |
| 29 | private GnmiUtils() { |
| 30 | // Hide default constructor |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Convert gNMI path to human readable string. |
| 35 | * |
| 36 | * @param path the gNMI path |
| 37 | * @return readable string of the path |
| 38 | */ |
| 39 | public static String pathToString(Path path) { |
| 40 | StringBuilder pathStringBuilder = new StringBuilder(); |
| 41 | |
| 42 | path.getElemList().forEach(elem -> { |
| 43 | pathStringBuilder.append("/").append(elem.getName()); |
| 44 | if (elem.getKeyCount() > 0) { |
| 45 | pathStringBuilder.append("["); |
| 46 | List<String> keys = elem.getKeyMap().entrySet().stream() |
| 47 | .map(entry -> entry.getKey() + "=" + entry.getValue()) |
| 48 | .collect(Collectors.toList()); |
| 49 | pathStringBuilder.append(String.join(", ", keys)); |
| 50 | pathStringBuilder.append("]"); |
| 51 | } |
| 52 | }); |
| 53 | return pathStringBuilder.toString(); |
| 54 | } |
| 55 | } |