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.yangtools.yang2sources.plugin;
10 import com.google.common.io.Files;
12 import java.io.IOException;
13 import java.util.Collection;
14 import org.apache.maven.model.Resource;
15 import org.apache.maven.project.MavenProject;
16 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 abstract class YangProvider {
21 private static final class Default extends YangProvider {
22 private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
25 void addYangsToMetaInf(final MavenProject project, final Collection<YangTextSchemaSource> modelsInProject)
28 final File generatedYangDir = new GeneratedDirectories(project).getYangDir();
29 LOG.debug("Generated dir {}", generatedYangDir);
31 // copy project's src/main/yang/*.yang to ${project.builddir}/generated-sources/yang/META-INF/yang/
32 // This honors setups like a Eclipse-profile derived one
33 final File withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
36 for (YangTextSchemaSource source : modelsInProject) {
37 final String fileName = source.getIdentifier().toYangFilename();
38 final File file = new File(withMetaInf, fileName);
40 source.copyTo(Files.asByteSink(file));
41 LOG.debug("Created file {} for {}", file, source.getIdentifier());
44 setResource(generatedYangDir, project);
45 LOG.debug("{} YANG files marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, generatedYangDir);
49 private static final YangProvider DEFAULT = new Default();
51 static YangProvider getInstance() {
55 abstract void addYangsToMetaInf(MavenProject project, Collection<YangTextSchemaSource> modelsInProject)
58 static void setResource(final File targetYangDir, final MavenProject project) {
59 Resource res = new Resource();
60 res.setDirectory(targetYangDir.getPath());
61 project.addResource(res);