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 org.apache.commons.io.FileUtils;
16 import org.apache.maven.plugin.logging.Log;
17 import org.apache.maven.project.MavenProject;
18 import org.opendaylight.controller.config.spi.ModuleFactory;
19 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
20 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
21 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
22 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
23 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.slf4j.impl.StaticLoggerBinder;
34 import java.io.IOException;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Map.Entry;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
45 * This class interfaces with yang-maven-plugin. Gets parsed yang modules in
46 * {@link SchemaContext}, and parameters form the plugin configuration, and
47 * writes service interfaces and/or modules.
49 public class JMXGenerator implements CodeGenerator {
51 static final String NAMESPACE_TO_PACKAGE_DIVIDER = "==";
52 static final String NAMESPACE_TO_PACKAGE_PREFIX = "namespaceToPackage";
53 static final String MODULE_FACTORY_FILE_BOOLEAN = "moduleFactoryFile";
55 private PackageTranslator packageTranslator;
56 private final CodeWriter codeWriter;
57 private static final Logger LOGGER = LoggerFactory
58 .getLogger(JMXGenerator.class);
59 private Map<String, String> namespaceToPackageMapping;
60 private File resourceBaseDir;
61 private File projectBaseDir;
62 private boolean generateModuleFactoryFile = true;
64 public JMXGenerator() {
65 this.codeWriter = new CodeWriter();
68 public JMXGenerator(CodeWriter codeWriter) {
69 this.codeWriter = codeWriter;
73 public Collection<File> generateSources(SchemaContext context,
74 File outputBaseDir, Set<Module> yangModulesInCurrentMavenModule) {
76 Preconditions.checkArgument(context != null, "Null context received");
77 Preconditions.checkArgument(outputBaseDir != null,
78 "Null outputBaseDir received");
81 .checkArgument(namespaceToPackageMapping != null && !namespaceToPackageMapping.isEmpty(),
82 "No namespace to package mapping provided in additionalConfiguration");
84 packageTranslator = new PackageTranslator(namespaceToPackageMapping);
86 if (!outputBaseDir.exists()) {
87 outputBaseDir.mkdirs();
90 GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
91 // create SIE structure qNamesToSIEs
92 Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
95 Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
96 for (Module module : context.getModules()) {
97 String packageName = packageTranslator.getPackageName(module);
98 Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
99 .create(module, packageName, knownSEITracker);
101 for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
103 // merge value into qNamesToSIEs
104 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
105 qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
107 throw new IllegalStateException(
108 "Cannot add two SIE with same qname "
109 + sieEntry.getValue());
112 if (yangModulesInCurrentMavenModule.contains(module)) {
113 // write this sie to disk
114 for (ServiceInterfaceEntry sie : namesToSIEntries.values()) {
116 generatedFiles.addFile(codeWriter.writeSie(sie,
118 } catch (Exception e) {
119 throw new RuntimeException(
120 "Error occurred during SIE source generate phase",
127 File mainBaseDir = concatFolders(projectBaseDir, "src", "main", "java");
128 Preconditions.checkNotNull(resourceBaseDir,
129 "resource base dir attribute was null");
131 StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
133 for (Module module : yangModulesInCurrentMavenModule) {
134 String packageName = packageTranslator.getPackageName(module);
135 Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
136 .create(module, qNamesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context)),
139 for (Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs
141 ModuleMXBeanEntry mbe = mbeEntry.getValue();
143 List<File> files1 = codeWriter.writeMbe(mbe, outputBaseDir,
145 generatedFiles.addFile(files1);
146 } catch (Exception e) {
147 throw new RuntimeException(
148 "Error occurred during MBE source generate phase",
151 fullyQualifiedNamesOfFactories.append(mbe
152 .getFullyQualifiedName(mbe.getStubFactoryName()));
153 fullyQualifiedNamesOfFactories.append("\n");
156 // create ModuleFactory file if needed
157 if (fullyQualifiedNamesOfFactories.length() > 0
158 && generateModuleFactoryFile) {
159 File serviceLoaderFile = JMXGenerator.concatFolders(
160 resourceBaseDir, "META-INF", "services",
161 ModuleFactory.class.getName());
162 // if this file does not exist, create empty file
163 serviceLoaderFile.getParentFile().mkdirs();
165 serviceLoaderFile.createNewFile();
166 FileUtils.write(serviceLoaderFile,
167 fullyQualifiedNamesOfFactories.toString());
168 } catch (IOException e) {
169 String message = "Cannot write to " + serviceLoaderFile;
170 LOGGER.error(message);
171 throw new RuntimeException(message, e);
174 return generatedFiles.getFiles();
177 static File concatFolders(File projectBaseDir, String... folderNames) {
178 StringBuilder b = new StringBuilder();
179 for (String folder : folderNames) {
181 b.append(File.separator);
183 return new File(projectBaseDir, b.toString());
187 public void setAdditionalConfig(Map<String, String> additionalCfg) {
188 if (LOGGER != null) {
189 LOGGER.debug(getClass().getCanonicalName(),
190 ": Additional configuration received: ",
191 additionalCfg.toString());
193 this.namespaceToPackageMapping = extractNamespaceMapping(additionalCfg);
194 this.generateModuleFactoryFile = extractModuleFactoryBoolean(additionalCfg);
197 private boolean extractModuleFactoryBoolean(
198 Map<String, String> additionalCfg) {
199 String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN);
203 if ("false".equals(bool)) {
210 public void setLog(Log log) {
211 StaticLoggerBinder.getSingleton().setMavenLog(log);
214 private static Map<String, String> extractNamespaceMapping(
215 Map<String, String> additionalCfg) {
216 Map<String, String> namespaceToPackage = Maps.newHashMap();
217 for (String key : additionalCfg.keySet()) {
218 if (key.startsWith(NAMESPACE_TO_PACKAGE_PREFIX)) {
219 String mapping = additionalCfg.get(key);
220 NamespaceMapping mappingResolved = extractNamespaceMapping(mapping);
221 namespaceToPackage.put(mappingResolved.namespace,
222 mappingResolved.packageName);
225 return namespaceToPackage;
228 static Pattern namespaceMappingPattern = Pattern.compile("(.+)"
229 + NAMESPACE_TO_PACKAGE_DIVIDER + "(.+)");
231 private static NamespaceMapping extractNamespaceMapping(String mapping) {
232 Matcher matcher = namespaceMappingPattern.matcher(mapping);
234 .checkArgument(matcher.matches(), String.format("Namespace to package mapping:%s is in invalid " +
235 "format, requested format is: %s", mapping, namespaceMappingPattern));
236 return new NamespaceMapping(matcher.group(1), matcher.group(2));
239 private static class NamespaceMapping {
240 public NamespaceMapping(String namespace, String packagename) {
241 this.namespace = namespace;
242 this.packageName = packagename;
245 private final String namespace, packageName;
249 public void setResourceBaseDir(File resourceDir) {
250 this.resourceBaseDir = resourceDir;
254 public void setMavenProject(MavenProject project) {
255 this.projectBaseDir = project.getBasedir();
258 LOGGER.debug(getClass().getCanonicalName(), " project base dir: ",
263 static class GeneratedFilesTracker {
264 private final Set<File> files = Sets.newHashSet();
266 void addFile(File file) {
267 if (files.contains(file)) {
268 List<File> undeletedFiles = Lists.newArrayList();
269 for (File presentFile : files) {
270 if (presentFile.delete() == false) {
271 undeletedFiles.add(presentFile);
274 if (undeletedFiles.isEmpty() == false) {
276 "Illegal state occurred: Unable to delete already generated files, undeleted files: {}",
279 throw new IllegalStateException(
280 "Name conflict in generated files, file" + file
286 void addFile(Collection<File> files) {
287 for (File file : files) {
292 public Set<File> getFiles() {