blob: e07773b307f732b67be914506ddede344984ef60 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska6d697f12015-03-08 20:59:50 -07003 *
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 */
16package org.onosproject.cfg.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.onosproject.cfg.ConfigProperty;
20import org.onosproject.cfg.ConfigProperty.Type;
Ray Milkey11ce9302019-02-07 14:41:17 -080021import org.slf4j.Logger;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070022
23import java.io.BufferedReader;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.InputStreamReader;
27import java.io.OutputStream;
28import java.io.OutputStreamWriter;
29import java.io.PrintWriter;
30import java.util.Set;
31
32import static org.onosproject.cfg.ConfigProperty.defineProperty;
Ray Milkey11ce9302019-02-07 14:41:17 -080033import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070034
35/**
36 * Utility for writing and reading configuration property definition file.
37 */
38public final class ConfigPropertyDefinitions {
39
40 private static final String FMT = "%s|%s|%s|%s\n";
41 private static final String SEP = "\\|";
42 private static final String COMMENT = "#";
43
Ray Milkey11ce9302019-02-07 14:41:17 -080044 private static final Logger log = getLogger(ConfigPropertyDefinitions.class);
45
Thomas Vachuska6d697f12015-03-08 20:59:50 -070046 private ConfigPropertyDefinitions() {
47 }
48
49 /**
50 * Writes the specified set of property definitions into the given output
51 * stream.
52 *
53 * @param stream output stream
54 * @param props properties whose definitions are to be written
55 * @throws java.io.IOException if unable to write the stream
56 */
57 public static void write(OutputStream stream, Set<ConfigProperty> props) throws IOException {
58 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(stream))) {
59 props.forEach(p -> pw.format(FMT, p.name(), p.type(), p.description(), p.defaultValue()));
60 }
61 }
62
63 /**
64 * Reads the specified input stream and creates from its contents a
65 * set of property definitions.
66 *
67 * @param stream input stream
68 * @return properties whose definitions are contained in the stream
69 * @throws java.io.IOException if unable to read the stream
70 */
71 public static Set<ConfigProperty> read(InputStream stream) throws IOException {
72 ImmutableSet.Builder<ConfigProperty> builder = ImmutableSet.builder();
73 try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
74 String line;
75 while ((line = br.readLine()) != null) {
76 if (!line.isEmpty() && !line.startsWith(COMMENT)) {
77 String[] f = line.split(SEP, 4);
Thomas Vachuska6d697f12015-03-08 20:59:50 -070078 builder.add(defineProperty(f[0], Type.valueOf(f[1]), f[2], f[3]));
79 }
80 }
81 }
82 return builder.build();
83 }
84
85}