blob: e27468bdcf045887b3c44a568f8fd0c5e616618f [file] [log] [blame]
Gaurav Agrawal8a5af142016-06-15 13:58:01 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.plugin.manager;
18
19import java.io.File;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053020import java.io.FileOutputStream;
21import java.io.IOException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053022import java.io.ObjectOutputStream;
23import java.nio.file.Files;
24import java.nio.file.StandardCopyOption;
25import java.util.ArrayList;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import java.util.Iterator;
27import java.util.List;
28import java.util.Set;
Bharat saraswal246a70c2016-06-16 13:24:06 +053029
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053030import org.apache.maven.artifact.repository.ArtifactRepository;
31import org.apache.maven.model.Dependency;
32import org.apache.maven.model.Resource;
33import org.apache.maven.project.MavenProject;
34import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053035import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053036import org.slf4j.Logger;
37import org.sonatype.plexus.build.incremental.BuildContext;
38
39import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
40import static org.onosproject.yangutils.utils.UtilConstants.JAR;
41import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
42import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
43import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
44import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053045import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Represents YANG plugin utilities.
50 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053051final class YangPluginUtils {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053052
53 private static final Logger log = getLogger(YangPluginUtils.class);
54
55 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
56
57 private static final String SERIALIZED_FILE_EXTENSION = ".ser";
58
59 private YangPluginUtils() {
60 }
61
62 /**
63 * Adds generated source directory to the compilation root.
64 *
65 * @param source directory
66 * @param project current maven project
67 * @param context current build context
68 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053069 static void addToCompilationRoot(String source, MavenProject project, BuildContext context) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053070 project.addCompileSourceRoot(source);
71 context.refresh(project.getBasedir());
72 log.info("Source directory added to compilation root: " + source);
73 }
74
75 /**
76 * Copies YANG files to the current project's output directory.
77 *
78 * @param yangFileInfo list of YANG files
79 * @param outputDir project's output directory
80 * @param project maven project
81 * @throws IOException when fails to copy files to destination resource directory
82 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053083 static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053084 throws IOException {
85
86 List<File> files = getListOfFile(yangFileInfo);
87
88 String path = outputDir + TARGET_RESOURCE_PATH;
89 File targetDir = new File(path);
90 targetDir.mkdirs();
91
92 for (File file : files) {
93 Files.copy(file.toPath(),
94 new File(path + file.getName()).toPath(),
95 StandardCopyOption.REPLACE_EXISTING);
96 }
97 addToProjectResource(outputDir + SLASH + TEMP + SLASH, project);
98 }
99
100 /**
101 * Provides a list of files from list of strings.
102 *
103 * @param yangFileInfo set of yang file information
104 * @return list of files
105 */
106 private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
107 List<File> files = new ArrayList<>();
108 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
109 while (yangFileIterator.hasNext()) {
110 YangFileInfo yangFile = yangFileIterator.next();
111 if (yangFile.isForTranslator()) {
112 files.add(new File(yangFile.getYangFileName()));
113 }
114 }
115 return files;
116 }
117
118 /**
119 * Serializes data-model.
120 *
121 * @param directory base directory for serialized files
122 * @param fileInfoSet YANG file info set
123 * @param project maven project
124 * @param operation true if need to add to resource
125 * @throws IOException when fails to do IO operations
126 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530127 static void serializeDataModel(String directory, Set<YangFileInfo> fileInfoSet,
128 MavenProject project, boolean operation) throws IOException {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530129
130 String serFileDirPath = directory + TARGET_RESOURCE_PATH;
131 File dir = new File(serFileDirPath);
132 dir.mkdirs();
133
134 if (operation) {
135 addToProjectResource(directory + SLASH + TEMP + SLASH, project);
136 }
137
138 for (YangFileInfo fileInfo : fileInfoSet) {
139
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530140 String serFileName = serFileDirPath + fileInfo.getRootNode().getName()
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530141 + SERIALIZED_FILE_EXTENSION;
142 fileInfo.setSerializedFile(serFileName);
143 FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
144 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
145 objectOutputStream.writeObject(fileInfo.getRootNode());
146 objectOutputStream.close();
147 fileOutputStream.close();
148 }
149 }
150
151 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530152 * Returns list of jar path.
153 *
154 * @param project maven project
155 * @param localRepository local repository
156 * @param remoteRepos remote repository
157 * @return list of jar paths
158 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530159 private static List<String> resolveDependencyJarPath(MavenProject project, ArtifactRepository localRepository,
160 List<ArtifactRepository> remoteRepos) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530161
162 StringBuilder path = new StringBuilder();
163 List<String> jarPaths = new ArrayList<>();
Bharat saraswal246a70c2016-06-16 13:24:06 +0530164 for (Object obj : project.getDependencies()) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530165
Bharat saraswal246a70c2016-06-16 13:24:06 +0530166 Dependency dependency = (Dependency) obj;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530167 path.append(localRepository.getBasedir());
168 path.append(SLASH);
169 path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId()));
170 path.append(SLASH);
171 path.append(dependency.getArtifactId());
172 path.append(SLASH);
173 path.append(dependency.getVersion());
174 path.append(SLASH);
175 path.append(dependency.getArtifactId() + HYPHEN + dependency.getVersion() + PERIOD + JAR);
176 File jarFile = new File(path.toString());
177 if (jarFile.exists()) {
178 jarPaths.add(path.toString());
179 }
180 path.delete(0, path.length());
181 }
182
183 for (ArtifactRepository repo : remoteRepos) {
184 // TODO: add resolver for remote repo.
185 }
186 return jarPaths;
187 }
188
189 /**
190 * Resolves inter jar dependencies.
191 *
192 * @param project current maven project
193 * @param localRepository local maven repository
194 * @param remoteRepos list of remote repository
195 * @param directory directory for serialized files
196 * @return list of resolved datamodel nodes
197 * @throws IOException when fails to do IO operations
198 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530199 static List<YangNode> resolveInterJarDependencies(MavenProject project, ArtifactRepository localRepository,
200 List<ArtifactRepository> remoteRepos, String directory)
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530201 throws IOException {
202
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530203 List<String> dependenciesJarPaths = resolveDependencyJarPath(project, localRepository, remoteRepos);
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530204 List<YangNode> resolvedDataModelNodes = new ArrayList<>();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530205 for (String dependency : dependenciesJarPaths) {
206 resolvedDataModelNodes.addAll(DataModelUtils.parseJarFile(dependency, directory));
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530207 }
208 return resolvedDataModelNodes;
209 }
210
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530211 /* Adds directory to resources of project */
212 private static void addToProjectResource(String dir, MavenProject project) {
213 Resource rsc = new Resource();
214 rsc.setDirectory(dir);
215 project.addResource(rsc);
216 }
217}