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