2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Sets;
15 import com.google.common.io.Files;
17 import java.io.IOException;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.apache.maven.project.MavenProject;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
30 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
31 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
32 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
33 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
39 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * This class interfaces with yang-maven-plugin. Gets parsed yang modules in
45 * {@link SchemaContext}, and parameters form the plugin configuration, and
46 * writes service interfaces and/or modules.
48 public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware {
49 private static final class NamespaceMapping {
50 private final String namespace, packageName;
52 public NamespaceMapping(final String namespace, final String packagename) {
53 this.namespace = namespace;
54 this.packageName = packagename;
59 static final String NAMESPACE_TO_PACKAGE_DIVIDER = "==";
61 static final String NAMESPACE_TO_PACKAGE_PREFIX = "namespaceToPackage";
63 static final String MODULE_FACTORY_FILE_BOOLEAN = "moduleFactoryFile";
65 private static final Logger LOG = LoggerFactory.getLogger(JMXGenerator.class);
66 private static final Pattern NAMESPACE_MAPPING_PATTERN = Pattern.compile("(.+)" + NAMESPACE_TO_PACKAGE_DIVIDER + "(.+)");
68 private PackageTranslator packageTranslator;
69 private final CodeWriter codeWriter;
70 private Map<String, String> namespaceToPackageMapping;
71 private File resourceBaseDir;
72 private File projectBaseDir;
73 private boolean generateModuleFactoryFile = true;
75 public JMXGenerator() {
76 this(new CodeWriter());
79 public JMXGenerator(final CodeWriter codeWriter) {
80 this.codeWriter = codeWriter;
84 public Collection<File> generateSources(final SchemaContext context,
85 final File outputBaseDir, final Set<Module> yangModulesInCurrentMavenModule) {
87 Preconditions.checkArgument(context != null, "Null context received");
88 Preconditions.checkArgument(outputBaseDir != null,
89 "Null outputBaseDir received");
92 .checkArgument(namespaceToPackageMapping != null && !namespaceToPackageMapping.isEmpty(),
93 "No namespace to package mapping provided in additionalConfiguration");
95 packageTranslator = new PackageTranslator(namespaceToPackageMapping);
97 if (!outputBaseDir.exists()) {
98 outputBaseDir.mkdirs();
101 GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
102 // create SIE structure qNamesToSIEs
103 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
106 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
107 for (Module module : context.getModules()) {
108 String packageName = packageTranslator.getPackageName(module);
109 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
110 .create(module, packageName, knownSEITracker);
112 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
114 // merge value into qNamesToSIEs
115 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
116 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
118 throw new IllegalStateException(
119 "Cannot add two SIE with same qname "
120 + sieEntry.getValue());
123 if (yangModulesInCurrentMavenModule.contains(module)) {
124 // write this sie to disk
125 for (ServiceInterfaceEntry sie : namesToSIEntries.values()) {
127 generatedFiles.addFile(codeWriter.writeSie(sie,
129 } catch (Exception e) {
130 throw new RuntimeException(
131 "Error occurred during SIE source generate phase",
138 File mainBaseDir = concatFolders(projectBaseDir, "src", "main", "java");
139 Preconditions.checkNotNull(resourceBaseDir,
140 "resource base dir attribute was null");
142 StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
144 for (Module module : yangModulesInCurrentMavenModule) {
145 String packageName = packageTranslator.getPackageName(module);
146 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
147 .create(module, qNamesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context)),
150 for (Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs
152 ModuleMXBeanEntry mbe = mbeEntry.getValue();
154 List<File> files1 = codeWriter.writeMbe(mbe, outputBaseDir,
156 generatedFiles.addFile(files1);
157 } catch (Exception e) {
158 throw new RuntimeException(
159 "Error occurred during MBE source generate phase",
162 fullyQualifiedNamesOfFactories.append(mbe
163 .getFullyQualifiedName(mbe.getStubFactoryName()));
164 fullyQualifiedNamesOfFactories.append("\n");
167 // create ModuleFactory file if needed
168 if (fullyQualifiedNamesOfFactories.length() > 0
169 && generateModuleFactoryFile) {
170 File serviceLoaderFile = JMXGenerator.concatFolders(
171 resourceBaseDir, "META-INF", "services",
172 ModuleFactory.class.getName());
173 // if this file does not exist, create empty file
174 serviceLoaderFile.getParentFile().mkdirs();
176 serviceLoaderFile.createNewFile();
177 Files.write(fullyQualifiedNamesOfFactories.toString(), serviceLoaderFile, StandardCharsets.UTF_8);
178 } catch (IOException e) {
179 String message = "Cannot write to " + serviceLoaderFile;
181 throw new RuntimeException(message, e);
184 return generatedFiles.getFiles();
188 static File concatFolders(final File projectBaseDir, final String... folderNames) {
189 StringBuilder b = new StringBuilder();
190 for (String folder : folderNames) {
192 b.append(File.separator);
194 return new File(projectBaseDir, b.toString());
198 public void setAdditionalConfig(final Map<String, String> additionalCfg) {
199 LOG.debug("{}: Additional configuration received: {}", getClass().getCanonicalName(), additionalCfg);
200 this.namespaceToPackageMapping = extractNamespaceMapping(additionalCfg);
201 this.generateModuleFactoryFile = extractModuleFactoryBoolean(additionalCfg);
204 private boolean extractModuleFactoryBoolean(
205 final Map<String, String> additionalCfg) {
206 String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN);
210 if ("false".equals(bool)) {
216 private static Map<String, String> extractNamespaceMapping(
217 final Map<String, String> additionalCfg) {
218 Map<String, String> namespaceToPackage = Maps.newHashMap();
219 for (String key : additionalCfg.keySet()) {
220 if (key.startsWith(NAMESPACE_TO_PACKAGE_PREFIX)) {
221 String mapping = additionalCfg.get(key);
222 NamespaceMapping mappingResolved = extractNamespaceMapping(mapping);
223 namespaceToPackage.put(mappingResolved.namespace,
224 mappingResolved.packageName);
227 return namespaceToPackage;
230 private static NamespaceMapping extractNamespaceMapping(final String mapping) {
231 Matcher matcher = NAMESPACE_MAPPING_PATTERN.matcher(mapping);
232 Preconditions.checkArgument(matcher.matches(),
233 "Namespace to package mapping:%s is in invalid format, requested format is: %s",
234 mapping, NAMESPACE_MAPPING_PATTERN);
235 return new NamespaceMapping(matcher.group(1), matcher.group(2));
239 public void setResourceBaseDir(final File resourceDir) {
240 this.resourceBaseDir = resourceDir;
244 public void setMavenProject(final MavenProject project) {
245 this.projectBaseDir = project.getBasedir();
246 LOG.debug("{}: project base dir: {}", getClass().getCanonicalName(), projectBaseDir);
250 static class GeneratedFilesTracker {
251 private final Set<File> files = Sets.newHashSet();
253 void addFile(final File file) {
254 if (files.contains(file)) {
255 List<File> undeletedFiles = Lists.newArrayList();
256 for (File presentFile : files) {
257 if (presentFile.delete() == false) {
258 undeletedFiles.add(presentFile);
261 if (undeletedFiles.isEmpty() == false) {
263 "Illegal state occurred: Unable to delete already generated files, undeleted files: {}",
266 throw new IllegalStateException(
267 "Name conflict in generated files, file" + file
273 void addFile(final Collection<File> files) {
274 for (File file : files) {
279 public Set<File> getFiles() {