Merge "Add get-config commit edit-config to testtool"
[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 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;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Map.Entry;
40 import java.util.Set;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43
44 /**
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.
48  */
49 public class JMXGenerator implements CodeGenerator {
50
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";
54
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;
63
64     public JMXGenerator() {
65         this.codeWriter = new CodeWriter();
66     }
67
68     public JMXGenerator(CodeWriter codeWriter) {
69         this.codeWriter = codeWriter;
70     }
71
72     @Override
73     public Collection<File> generateSources(SchemaContext context,
74                                             File outputBaseDir, Set<Module> yangModulesInCurrentMavenModule) {
75
76         Preconditions.checkArgument(context != null, "Null context received");
77         Preconditions.checkArgument(outputBaseDir != null,
78                 "Null outputBaseDir received");
79
80         Preconditions
81                 .checkArgument(namespaceToPackageMapping != null && !namespaceToPackageMapping.isEmpty(),
82                         "No namespace to package mapping provided in additionalConfiguration");
83
84         packageTranslator = new PackageTranslator(namespaceToPackageMapping);
85
86         if (!outputBaseDir.exists()) {
87             outputBaseDir.mkdirs();
88         }
89
90         GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
91         // create SIE structure qNamesToSIEs
92         Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
93
94
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);
100
101             for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries
102                     .entrySet()) {
103                 // merge value into qNamesToSIEs
104                 if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
105                     qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
106                 } else {
107                     throw new IllegalStateException(
108                         "Cannot add two SIE  with same qname "
109                                 + sieEntry.getValue());
110                 }
111             }
112             if (yangModulesInCurrentMavenModule.contains(module)) {
113                 // write this sie to disk
114                 for (ServiceInterfaceEntry sie : namesToSIEntries.values()) {
115                     try {
116                         generatedFiles.addFile(codeWriter.writeSie(sie,
117                                 outputBaseDir));
118                     } catch (Exception e) {
119                         throw new RuntimeException(
120                                 "Error occurred during SIE source generate phase",
121                                 e);
122                     }
123                 }
124             }
125         }
126
127         File mainBaseDir = concatFolders(projectBaseDir, "src", "main", "java");
128         Preconditions.checkNotNull(resourceBaseDir,
129                 "resource base dir attribute was null");
130
131         StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
132         // create MBEs
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)),
137                             packageName);
138
139             for (Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs
140                     .entrySet()) {
141                 ModuleMXBeanEntry mbe = mbeEntry.getValue();
142                 try {
143                     List<File> files1 = codeWriter.writeMbe(mbe, outputBaseDir,
144                             mainBaseDir);
145                     generatedFiles.addFile(files1);
146                 } catch (Exception e) {
147                     throw new RuntimeException(
148                             "Error occurred during MBE source generate phase",
149                             e);
150                 }
151                 fullyQualifiedNamesOfFactories.append(mbe
152                         .getFullyQualifiedName(mbe.getStubFactoryName()));
153                 fullyQualifiedNamesOfFactories.append("\n");
154             }
155         }
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();
164             try {
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);
172             }
173         }
174         return generatedFiles.getFiles();
175     }
176
177     static File concatFolders(File projectBaseDir, String... folderNames) {
178         StringBuilder b = new StringBuilder();
179         for (String folder : folderNames) {
180             b.append(folder);
181             b.append(File.separator);
182         }
183         return new File(projectBaseDir, b.toString());
184     }
185
186     @Override
187     public void setAdditionalConfig(Map<String, String> additionalCfg) {
188         if (LOGGER != null) {
189             LOGGER.debug(getClass().getCanonicalName(),
190                     ": Additional configuration received: ",
191                     additionalCfg.toString());
192         }
193         this.namespaceToPackageMapping = extractNamespaceMapping(additionalCfg);
194         this.generateModuleFactoryFile = extractModuleFactoryBoolean(additionalCfg);
195     }
196
197     private boolean extractModuleFactoryBoolean(
198             Map<String, String> additionalCfg) {
199         String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN);
200         if (bool == null) {
201             return true;
202         }
203         if ("false".equals(bool)) {
204             return false;
205         }
206         return true;
207     }
208
209     @Override
210     public void setLog(Log log) {
211         StaticLoggerBinder.getSingleton().setMavenLog(log);
212     }
213
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);
223             }
224         }
225         return namespaceToPackage;
226     }
227
228     static Pattern namespaceMappingPattern = Pattern.compile("(.+)"
229             + NAMESPACE_TO_PACKAGE_DIVIDER + "(.+)");
230
231     private static NamespaceMapping extractNamespaceMapping(String mapping) {
232         Matcher matcher = namespaceMappingPattern.matcher(mapping);
233         Preconditions
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));
237     }
238
239     private static class NamespaceMapping {
240         public NamespaceMapping(String namespace, String packagename) {
241             this.namespace = namespace;
242             this.packageName = packagename;
243         }
244
245         private final String namespace, packageName;
246     }
247
248     @Override
249     public void setResourceBaseDir(File resourceDir) {
250         this.resourceBaseDir = resourceDir;
251     }
252
253     @Override
254     public void setMavenProject(MavenProject project) {
255         this.projectBaseDir = project.getBasedir();
256
257         if (LOGGER != null)
258             LOGGER.debug(getClass().getCanonicalName(), " project base dir: ",
259                     projectBaseDir);
260     }
261
262     @VisibleForTesting
263     static class GeneratedFilesTracker {
264         private final Set<File> files = Sets.newHashSet();
265
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);
272                     }
273                 }
274                 if (undeletedFiles.isEmpty() == false) {
275                     LOGGER.error(
276                             "Illegal state occurred: Unable to delete already generated files, undeleted files: {}",
277                             undeletedFiles);
278                 }
279                 throw new IllegalStateException(
280                         "Name conflict in generated files, file" + file
281                                 + " present twice");
282             }
283             files.add(file);
284         }
285
286         void addFile(Collection<File> files) {
287             for (File file : files) {
288                 addFile(file);
289             }
290         }
291
292         public Set<File> getFiles() {
293             return files;
294         }
295     }
296 }