Open Kilda Java Documentation
LaunchEnvironment.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm;
17 
18 import static java.util.Collections.singletonList;
19 
26 
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.kohsuke.args4j.CmdLineException;
29 
30 import java.util.Properties;
31 
32 public class LaunchEnvironment {
33  private static String CLI_OVERLAY = "cli";
34 
35  private final CliArguments cli;
36  private Properties properties;
37 
38  public LaunchEnvironment(String[] args) throws CmdLineException, ConfigurationException {
39  this.cli = new CliArguments(args);
40  properties = cli.getProperties();
41  properties = makeCliOverlay();
42  }
43 
44  public String getTopologyName() {
45  return cli.getTopologyName();
46  }
47 
55  public ConfigurationProvider getConfigurationProvider(String... prefixes) {
56  String[] prefixesWithOverlay = ArrayUtils.addAll(ArrayUtils.toArray(CLI_OVERLAY), prefixes);
57 
58  // Apply Kafka naming to Kafka topics and groups in the configuration.
59  return new ConfigurationProvider(getProperties(), prefixesWithOverlay,
61  }
62 
64  return new KafkaNamingStrategy(getEnvironmentPrefix());
65  }
66 
68  return new TopologyNamingStrategy(getEnvironmentPrefix());
69  }
70 
71  private String getEnvironmentPrefix() {
72  ConfigurationProvider configurationProvider = new ConfigurationProvider(getProperties(),
73  ArrayUtils.toArray(CLI_OVERLAY));
74 
75  EnvironmentConfig environmentConfig = configurationProvider.getConfiguration(EnvironmentConfig.class);
76  return environmentConfig.getNamingPrefix() != null ? environmentConfig.getNamingPrefix() : "";
77  }
78 
79  public void setupOverlay(Properties overlay) {
80  Properties newLayer = new Properties(getProperties());
81  for (String name : overlay.stringPropertyNames()) {
82  newLayer.setProperty(name, overlay.getProperty(name));
83  }
84  properties = newLayer;
85  }
86 
87  private Properties makeCliOverlay() {
88  Properties overlay = new Properties(getProperties());
89 
90  overlay.setProperty(CLI_OVERLAY + ".local", cli.getIsLocal() ? "true" : "false");
91  if (cli.getLocalExecutionTime() != null) {
92  overlay.setProperty(CLI_OVERLAY + ".local.execution.time", cli.getLocalExecutionTime().toString());
93  }
94 
95  return overlay;
96  }
97 
98  protected Properties getProperties() {
99  return properties;
100  }
101 }
void setupOverlay(Properties overlay)
ConfigurationProvider getConfigurationProvider(String... prefixes)
name
Definition: setup.py:24
TopologyNamingStrategy getTopologyNamingStrategy()
KafkaNamingStrategy getKafkaNamingStrategy()