Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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 | package org.onosproject.cfg.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.onosproject.cfg.ConfigProperty; |
| 20 | import org.onosproject.cfg.ConfigProperty.Type; |
Ray Milkey | 11ce930 | 2019-02-07 14:41:17 -0800 | [diff] [blame] | 21 | import org.slf4j.Logger; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 22 | |
| 23 | import java.io.BufferedReader; |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
| 26 | import java.io.InputStreamReader; |
| 27 | import java.io.OutputStream; |
| 28 | import java.io.OutputStreamWriter; |
| 29 | import java.io.PrintWriter; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.onosproject.cfg.ConfigProperty.defineProperty; |
Ray Milkey | 11ce930 | 2019-02-07 14:41:17 -0800 | [diff] [blame] | 33 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Utility for writing and reading configuration property definition file. |
| 37 | */ |
| 38 | public 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 Milkey | 11ce930 | 2019-02-07 14:41:17 -0800 | [diff] [blame] | 44 | private static final Logger log = getLogger(ConfigPropertyDefinitions.class); |
| 45 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 46 | 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); |
Ray Milkey | 11ce930 | 2019-02-07 14:41:17 -0800 | [diff] [blame] | 78 | log.info("Line is: {} length of array is: {}", line, f.length); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 79 | builder.add(defineProperty(f[0], Type.valueOf(f[1]), f[2], f[3])); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return builder.build(); |
| 84 | } |
| 85 | |
| 86 | } |