blob: 6a71a196f6cd5fc48ea8268e3bf6febd638527c2 [file] [log] [blame]
Yi Tsenge616d752018-11-27 10:53:27 -08001/*
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.gnmi.api;
18
19import gnmi.Gnmi.Path;
20
21import java.util.List;
22import java.util.stream.Collectors;
23
24/**
25 * Utilities for gNMI protocol.
26 */
27public 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}