Remove yang-test
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / CodeWriter.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.base.Optional;
11 import com.google.common.collect.Lists;
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
23 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
24 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
25 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate;
26 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate;
27 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate;
28 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.RuntimeRegistratorFtlTemplate;
29 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.StubFactoryTemplate;
30 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory;
31 import org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory.AbsFactoryGeneratedObjectFactory;
32 import org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory.AbsModuleGeneratedObjectFactory;
33 import org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory.ConcreteModuleGeneratedObjectFactory;
34 import org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory.GenericGeneratedObjectFactory;
35 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.FullyQualifiedName;
36 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.GeneratedObject;
37 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 final class CodeWriter {
42
43     private static final Logger LOG = LoggerFactory.getLogger(CodeWriter.class);
44     private static final Optional<String> COPYRIGHT = StringUtil.loadCopyright();
45
46     public File writeSie(final ServiceInterfaceEntry sie, final File outputBaseDir) {
47         try {
48             GeneralInterfaceTemplate generalInterfaceTemplate = TemplateFactory.serviceInterfaceFromSie(sie);
49             GeneratedObject go = new GenericGeneratedObjectFactory().toGeneratedObject(generalInterfaceTemplate, COPYRIGHT);
50             return go.persist(outputBaseDir).get().getValue();
51         } catch (Exception e) {
52             String message = "An error occurred during Service interface generating, sie:"
53                     + sie.getTypeName() + ", " + sie.getFullyQualifiedName();
54             LOG.error(message, e);
55             throw new RuntimeException(message, e);
56         }
57     }
58
59     public List<File> writeMbe(final ModuleMXBeanEntry mbe, final File targetBaseDir,
60                                final File mainBaseDir) {
61         try {
62             List<File> generatedFiles = Lists.newArrayList();
63
64
65             Map<GeneratedObject, Boolean /*overwrite*/> gos = new HashMap<>();
66
67             // generate mx interface and abstract factory
68
69             // TOs
70             Map<String,GeneralClassTemplate> tosFromMbe = TemplateFactory.tOsFromMbe(mbe);
71             for(GeneralClassTemplate template: tosFromMbe.values()) {
72                 gos.put(new GenericGeneratedObjectFactory().toGeneratedObject(template, COPYRIGHT), true);
73             }
74
75             // MXBean interface
76             GeneralInterfaceTemplate ifcTemplate = TemplateFactory.mXBeanInterfaceTemplateFromMbe(mbe);
77             gos.put(new GenericGeneratedObjectFactory().toGeneratedObject(ifcTemplate, COPYRIGHT), true);
78
79
80             // generate abstract factory
81             gos.put(new AbsFactoryGeneratedObjectFactory().toGeneratedObject(mbe, COPYRIGHT), true);
82
83             // generate abstract module
84             gos.put(new AbsModuleGeneratedObjectFactory().toGeneratedObject(mbe, COPYRIGHT), true);
85
86             // generate concrete factory
87             StubFactoryTemplate concreteFactory = TemplateFactory.stubFactoryTemplateFromMbe(mbe);
88             gos.put(new GenericGeneratedObjectFactory().toGeneratedObject(concreteFactory, COPYRIGHT), false);
89
90
91             // generate concrete module
92
93             gos.put(new ConcreteModuleGeneratedObjectFactory().toGeneratedObject(mbe, COPYRIGHT, Optional.<String>absent()), false);
94
95             // write runtime bean MXBeans and registrators
96             List<FtlTemplate> allFtlFiles = getRuntimeBeanFtlTemplates(mbe.getRuntimeBeans());
97             for(FtlTemplate template: allFtlFiles) {
98                 gos.put(new GenericGeneratedObjectFactory().toGeneratedObject(template, COPYRIGHT), true);
99             }
100
101             generatedFiles.addAll(persistGeneratedObjects(targetBaseDir, mainBaseDir, gos));
102
103             // purge nulls
104             for (Iterator<File> it = generatedFiles.iterator(); it.hasNext(); ) {
105                 if (it.next() == null) {
106                     it.remove();
107                 }
108             }
109
110             return generatedFiles;
111
112         } catch (Exception e) {
113             String message = "An error occurred during Module generating, mbe:"
114                     + mbe.getJavaNamePrefix();
115             LOG.error(message, e);
116             throw new RuntimeException(message, e);
117         }
118     }
119
120     private static List<File> persistGeneratedObjects(final File targetBaseDir, final File mainBaseDir,
121             final Map<GeneratedObject, Boolean> gos) throws IOException {
122         List<File> generatedFiles = new ArrayList<>();
123         for (Entry<GeneratedObject, Boolean> entry : gos.entrySet()) {
124             boolean overwrite = entry.getValue();
125             File dst;
126             if (overwrite) {
127                 dst = targetBaseDir;
128             } else {
129                 dst = mainBaseDir;
130             }
131             Optional<Entry<FullyQualifiedName, File>> maybePersistEntry = entry.getKey().persist(dst, overwrite);
132
133             if (maybePersistEntry.isPresent()) {
134                 generatedFiles.add(maybePersistEntry.get().getValue());
135             }
136         }
137         return generatedFiles;
138     }
139
140     private static List<FtlTemplate> getRuntimeBeanFtlTemplates(final Collection<RuntimeBeanEntry> runtimeBeans) {
141         if (runtimeBeans.isEmpty()) {
142             return Collections.emptyList();
143         }
144         List<FtlTemplate> allFtlFiles = new ArrayList<>();
145         { // registrators
146             Map<String, FtlTemplate> registratorNamesToFtls = RuntimeRegistratorFtlTemplate
147                     .create(RuntimeRegistratorFtlTemplate.findRoot(runtimeBeans));
148
149             allFtlFiles.addAll(registratorNamesToFtls.values());
150         }
151         { // TOs, MXBean interfaces
152             for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeans) {
153                 Collection<FtlTemplate> ftlFiles = TemplateFactory
154                         .getTOAndMXInterfaceFtlFiles(runtimeBeanEntry)
155                         .values();
156                 allFtlFiles.addAll(ftlFiles);
157             }
158         }
159         return allFtlFiles;
160     }
161 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.