summaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/ConfigDefinitionBuilder.java
blob: 0121e47b9aee6dc853f1999ebb066cb00cb0f7ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.config.codegen.CNode;
import com.yahoo.config.codegen.LeafCNode;

import java.util.Arrays;

/**
 * Builds a ConfigDefinition from a tree of CNodes.
 *
 * @author hmusum
 */
public class ConfigDefinitionBuilder {

    /**
     * Creates a ConfigDefinition based on a tree generated from parsing a config
     * definition file.
     *
     * @param root the root node in a tree generated from parsing a config definition file.
     * @return a ConfigDefinition object
     */
    public static ConfigDefinition createConfigDefinition(CNode root) {
        ConfigDefinition def = new ConfigDefinition(root.getName(), root.getVersion(), root.getNamespace());

        for (CNode node : root.getChildren()) {
            addNode(def, node);
        }
        return def;
    }

    /**
     *
     * @param def a ConfigDefinition object
     * @param node the node to be added to the config definition
     */
    private static void addNode(ConfigDefinition def, CNode node) {
        String name = node.getName();
        if (node instanceof LeafCNode) {
            if (node.isArray) {
                //System.out.println("Adding array node " + name);
                String enumValues = null;
                String type = ((LeafCNode) node).getType();
                if ("enum".equals(type)) {
                    enumValues = convertToEnumValueCommaSeparated(((LeafCNode.EnumLeaf) node).getLegalValues());
                }
                def.arrayDef(name).setTypeSpec(
                        new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, enumValues, null, null));

            } else if (node.isMap) {
                //System.out.println("Adding leaf map node " + name);
                def.leafMapDef(name).setTypeSpec(new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, null, null, null));
            } else {
                //System.out.println("Adding basic node " + name);
                if (node instanceof LeafCNode.IntegerLeaf) {
                    addNode(def, (LeafCNode.IntegerLeaf) node);
                } else if (node instanceof LeafCNode.LongLeaf) {
                    addNode(def, (LeafCNode.LongLeaf) node);
                } else if (node instanceof LeafCNode.BooleanLeaf) {
                    addNode(def, (LeafCNode.BooleanLeaf) node);
                } else if (node instanceof LeafCNode.DoubleLeaf) {
                    addNode(def, (LeafCNode.DoubleLeaf) node);
                    // Need to come before StringLeaf, since it is a subclass of StringLeaf
                } else if (node instanceof LeafCNode.ReferenceLeaf) {
                    addNode(def, (LeafCNode.ReferenceLeaf) node);
                } else if (node instanceof LeafCNode.FileLeaf) {
                    addNode(def, (LeafCNode.FileLeaf) node);
                } else if (node instanceof LeafCNode.PathLeaf) {
                    addNode(def, (LeafCNode.PathLeaf) node);
                } else if (node instanceof LeafCNode.UrlLeaf) {
                    addNode(def, (LeafCNode.UrlLeaf) node);
                } else if (node instanceof LeafCNode.StringLeaf) {
                    addNode(def, (LeafCNode.StringLeaf) node);
                } else if (node instanceof LeafCNode.EnumLeaf) {
                    addNode(def, (LeafCNode.EnumLeaf) node);
                } else {
                    System.err.println("Unknown node type for node with name " + name);
                }
            }
        } else {
            ConfigDefinition newDef;
            if (node.isArray) {
                if (node.getChildren() != null && node.getChildren().length > 0) {
                    //System.out.println("\tAdding inner array node " + name);
                    newDef = def.innerArrayDef(name);
                    for (CNode childNode : node.getChildren()) {
                        //System.out.println("\tChild node " + childNode.getName());
                        addNode(newDef, childNode);
                    }
                }
            } else if (node.isMap) {
                //System.out.println("Adding struct map node " + name);
                newDef = def.structMapDef(name);
                if (node.getChildren() != null && node.getChildren().length > 0) {
                    for (CNode childNode : node.getChildren()) {
                        //System.out.println("\tChild node " + childNode.getName());
                        addNode(newDef, childNode);
                    }
                }

            } else {
                //System.out.println("Adding struct node " + name);
                newDef = def.structDef(name);
                if (node.getChildren() != null && node.getChildren().length > 0) {
                    for (CNode childNode : node.getChildren()) {
                        //System.out.println("\tChild node " + childNode.getName());
                        addNode(newDef, childNode);
                    }
                }
            }
        }
    }


    static void addNode(ConfigDefinition def, LeafCNode.IntegerLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addIntDef(leaf.getName(), Integer.valueOf(leaf.getDefaultValue().getValue()));
        } else {
            def.addIntDef(leaf.getName());
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.LongLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addLongDef(leaf.getName(), Long.valueOf(leaf.getDefaultValue().getValue()));
        } else {
            def.addLongDef(leaf.getName());
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.BooleanLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addBoolDef(leaf.getName(), Boolean.valueOf(leaf.getDefaultValue().getValue()));
        } else {
            def.addBoolDef(leaf.getName());
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.DoubleLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addDoubleDef(leaf.getName(), Double.valueOf(leaf.getDefaultValue().getValue()));
        } else {
            def.addDoubleDef(leaf.getName());
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.StringLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addStringDef(leaf.getName(), leaf.getDefaultValue().getValue());
        } else {
            def.addStringDef(leaf.getName());
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.ReferenceLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addReferenceDef(leaf.getName(), leaf.getDefaultValue().getValue());
        } else {
            def.addReferenceDef(leaf.getName(), null);
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.FileLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addFileDef(leaf.getName(), leaf.getDefaultValue().getValue());
        } else {
            def.addFileDef(leaf.getName(), null);
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.PathLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addPathDef(leaf.getName(), leaf.getDefaultValue().getValue());
        } else {
            def.addPathDef(leaf.getName(), null);
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.UrlLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addUrlDef(leaf.getName(), leaf.getDefaultValue().getValue());
        } else {
            def.addUrlDef(leaf.getName(), null);
        }
    }

    static void addNode(ConfigDefinition def, LeafCNode.EnumLeaf leaf) {
        if (leaf.getDefaultValue() != null) {
            def.addEnumDef(leaf.getName(), Arrays.asList(leaf.getLegalValues()), leaf.getDefaultValue().getValue());
        } else {
            def.addEnumDef(leaf.getName(), Arrays.asList(leaf.getLegalValues()), null);
        }
    }

    static String convertToEnumValueCommaSeparated(String[] enumValues) {
        StringBuilder sb = new StringBuilder();
        for (String s : enumValues) {
            sb.append(s);
            sb.append(", ");
        }
        int length = sb.length();
        sb.delete(length - 2, length);
        return sb.toString();
    }
}