4ad8fd79c46fa8ad76088a2ef6ec3ea2289d5e5d
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / JMXGenerator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
9
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;
16 import java.io.File;
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;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
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;
42
43 /**
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.
47  */
48 public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware {
49     private static final class NamespaceMapping {
50         private final String namespace, packageName;
51
52         public NamespaceMapping(final String namespace, final String packagename) {
53             this.namespace = namespace;
54             this.packageName = packagename;
55         }
56     }
57
58     @VisibleForTesting
59     static final String NAMESPACE_TO_PACKAGE_DIVIDER = "==";
60     @VisibleForTesting
61     static final String NAMESPACE_TO_PACKAGE_PREFIX = "namespaceToPackage";
62     @VisibleForTesting
63     static final String MODULE_FACTORY_FILE_BOOLEAN = "moduleFactoryFile";
64
65     private static final Logger LOG = LoggerFactory.getLogger(JMXGenerator.class);
66     private static final Pattern NAMESPACE_MAPPING_PATTERN = Pattern.compile("(.+)" + NAMESPACE_TO_PACKAGE_DIVIDER + "(.+)");
67
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;
74
75     public JMXGenerator() {
76         this(new CodeWriter());
77     }
78
79     public JMXGenerator(final CodeWriter codeWriter) {
80         this.codeWriter = codeWriter;
81     }
82
83     @Override
84     public Collection<File> generateSources(final SchemaContext context,
85                                             final File outputBaseDir, final Set<Module> yangModulesInCurrentMavenModule) {
86
87         Preconditions.checkArgument(context != null, "Null context received");
88         Preconditions.checkArgument(outputBaseDir != null,
89                 "Null outputBaseDir received");
90
91         Preconditions
92                 .checkArgument(namespaceToPackageMapping != null && !namespaceToPackageMapping.isEmpty(),
93                         "No namespace to package mapping provided in additionalConfiguration");
94
95         packageTranslator = new PackageTranslator(namespaceToPackageMapping);
96
97         if (!outputBaseDir.exists()) {
98             outputBaseDir.mkdirs();
99         }
100
101         GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
102         // create SIE structure qNamesToSIEs
103         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
104
105
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);
111
112             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
113                     .entrySet()) {
114                 // merge value into qNamesToSIEs
115                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
116                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
117                 } else {
118                     throw new IllegalStateException(
119                         "Cannot add two SIE  with same qname "
120                                 + sieEntry.getValue());
121                 }
122             }
123             if (yangModulesInCurrentMavenModule.contains(module)) {
124                 // write this sie to disk
125                 for (ServiceInterfaceEntry sie : namesToSIEntries.values()) {
126                     try {
127                         generatedFiles.addFile(codeWriter.writeSie(sie,
128                                 outputBaseDir));
129                     } catch (Exception e) {
130                         throw new RuntimeException(
131                                 "Error occurred during SIE source generate phase",
132                                 e);
133                     }
134                 }
135             }
136         }
137
138         File mainBaseDir = concatFolders(projectBaseDir, "src", "main", "java");
139         Preconditions.checkNotNull(resourceBaseDir,
140                 "resource base dir attribute was null");
141
142         StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
143         // create MBEs
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)),
148                             packageName);
149
150             for (Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs
151                     .entrySet()) {
152                 ModuleMXBeanEntry mbe = mbeEntry.getValue();
153                 try {
154                     List<File> files1 = codeWriter.writeMbe(mbe, outputBaseDir,
155                             mainBaseDir);
156                     generatedFiles.addFile(files1);
157                 } catch (Exception e) {
158                     throw new RuntimeException(
159                             "Error occurred during MBE source generate phase",
160                             e);
161                 }
162                 fullyQualifiedNamesOfFactories.append(mbe
163                         .getFullyQualifiedName(mbe.getStubFactoryName()));
164                 fullyQualifiedNamesOfFactories.append("\n");
165             }
166         }
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();
175             try {
176                 serviceLoaderFile.createNewFile();
177                 Files.write(fullyQualifiedNamesOfFactories.toString(), serviceLoaderFile, StandardCharsets.UTF_8);
178             } catch (IOException e) {
179                 String message = "Cannot write to " + serviceLoaderFile;
180                 LOG.error(message);
181                 throw new RuntimeException(message, e);
182             }
183         }
184         return generatedFiles.getFiles();
185     }
186
187     @VisibleForTesting
188     static File concatFolders(final File projectBaseDir, final String... folderNames) {
189         StringBuilder b = new StringBuilder();
190         for (String folder : folderNames) {
191             b.append(folder);
192             b.append(File.separator);
193         }
194         return new File(projectBaseDir, b.toString());
195     }
196
197     @Override
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);
202     }
203
204     private boolean extractModuleFactoryBoolean(
205             final Map<String, String> additionalCfg) {
206         String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN);
207         if (bool == null) {
208             return true;
209         }
210         if ("false".equals(bool)) {
211             return false;
212         }
213         return true;
214     }
215
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);
225             }
226         }
227         return namespaceToPackage;
228     }
229
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));
236     }
237
238     @Override
239     public void setResourceBaseDir(final File resourceDir) {
240         this.resourceBaseDir = resourceDir;
241     }
242
243     @Override
244     public void setMavenProject(final MavenProject project) {
245         this.projectBaseDir = project.getBasedir();
246         LOG.debug("{}: project base dir: {}", getClass().getCanonicalName(), projectBaseDir);
247     }
248
249     @VisibleForTesting
250     static class GeneratedFilesTracker {
251         private final Set<File> files = Sets.newHashSet();
252
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);
259                     }
260                 }
261                 if (undeletedFiles.isEmpty() == false) {
262                     LOG.error(
263                             "Illegal state occurred: Unable to delete already generated files, undeleted files: {}",
264                             undeletedFiles);
265                 }
266                 throw new IllegalStateException(
267                         "Name conflict in generated files, file" + file
268                                 + " present twice");
269             }
270             files.add(file);
271         }
272
273         void addFile(final Collection<File> files) {
274             for (File file : files) {
275                 addFile(file);
276             }
277         }
278
279         public Set<File> getFiles() {
280             return files;
281         }
282     }
283 }