blob: 148dd0e30d1d95bb6843e1e2a3c7f789768b552a [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;
22import java.io.InputStream;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053023import java.io.ObjectOutputStream;
24import java.nio.file.Files;
25import java.nio.file.StandardCopyOption;
26import java.util.ArrayList;
27import java.util.Enumeration;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Set;
31import java.util.jar.JarEntry;
32import java.util.jar.JarFile;
Bharat saraswal246a70c2016-06-16 13:24:06 +053033
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053034import org.apache.maven.artifact.repository.ArtifactRepository;
35import org.apache.maven.model.Dependency;
36import org.apache.maven.model.Resource;
37import org.apache.maven.project.MavenProject;
38import org.onosproject.yangutils.datamodel.YangNode;
39import org.slf4j.Logger;
40import org.sonatype.plexus.build.incremental.BuildContext;
41
Bharat saraswalb1170bd2016-07-14 13:26:18 +053042import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.deSerializeDataModel;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053043import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
44import static org.onosproject.yangutils.utils.UtilConstants.JAR;
45import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
46import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
47import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
48import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
49import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
50import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
51import static org.slf4j.LoggerFactory.getLogger;
52
53/**
54 * Represents YANG plugin utilities.
55 */
56public final class YangPluginUtils {
57
58 private static final Logger log = getLogger(YangPluginUtils.class);
59
60 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
61
62 private static final String SERIALIZED_FILE_EXTENSION = ".ser";
63
64 private YangPluginUtils() {
65 }
66
67 /**
68 * Adds generated source directory to the compilation root.
69 *
70 * @param source directory
71 * @param project current maven project
72 * @param context current build context
73 */
74 public static void addToCompilationRoot(String source, MavenProject project, BuildContext context) {
75 project.addCompileSourceRoot(source);
76 context.refresh(project.getBasedir());
77 log.info("Source directory added to compilation root: " + source);
78 }
79
80 /**
81 * Copies YANG files to the current project's output directory.
82 *
83 * @param yangFileInfo list of YANG files
84 * @param outputDir project's output directory
85 * @param project maven project
86 * @throws IOException when fails to copy files to destination resource directory
87 */
88 public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
89 throws IOException {
90
91 List<File> files = getListOfFile(yangFileInfo);
92
93 String path = outputDir + TARGET_RESOURCE_PATH;
94 File targetDir = new File(path);
95 targetDir.mkdirs();
96
97 for (File file : files) {
98 Files.copy(file.toPath(),
99 new File(path + file.getName()).toPath(),
100 StandardCopyOption.REPLACE_EXISTING);
101 }
102 addToProjectResource(outputDir + SLASH + TEMP + SLASH, project);
103 }
104
105 /**
106 * Provides a list of files from list of strings.
107 *
108 * @param yangFileInfo set of yang file information
109 * @return list of files
110 */
111 private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
112 List<File> files = new ArrayList<>();
113 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
114 while (yangFileIterator.hasNext()) {
115 YangFileInfo yangFile = yangFileIterator.next();
116 if (yangFile.isForTranslator()) {
117 files.add(new File(yangFile.getYangFileName()));
118 }
119 }
120 return files;
121 }
122
123 /**
124 * Serializes data-model.
125 *
126 * @param directory base directory for serialized files
127 * @param fileInfoSet YANG file info set
128 * @param project maven project
129 * @param operation true if need to add to resource
130 * @throws IOException when fails to do IO operations
131 */
132 public static void serializeDataModel(String directory, Set<YangFileInfo> fileInfoSet,
133 MavenProject project, boolean operation) throws IOException {
134
135 String serFileDirPath = directory + TARGET_RESOURCE_PATH;
136 File dir = new File(serFileDirPath);
137 dir.mkdirs();
138
139 if (operation) {
140 addToProjectResource(directory + SLASH + TEMP + SLASH, project);
141 }
142
143 for (YangFileInfo fileInfo : fileInfoSet) {
144
145 String serFileName = serFileDirPath + getCamelCase(fileInfo.getRootNode().getName(), null)
146 + SERIALIZED_FILE_EXTENSION;
147 fileInfo.setSerializedFile(serFileName);
148 FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
149 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
150 objectOutputStream.writeObject(fileInfo.getRootNode());
151 objectOutputStream.close();
152 fileOutputStream.close();
153 }
154 }
155
156 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530157 * Returns list of jar path.
158 *
159 * @param project maven project
160 * @param localRepository local repository
161 * @param remoteRepos remote repository
162 * @return list of jar paths
163 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530164 private static List<String> resolveDependencyJarPath(MavenProject project, ArtifactRepository localRepository,
165 List<ArtifactRepository> remoteRepos) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530166
167 StringBuilder path = new StringBuilder();
168 List<String> jarPaths = new ArrayList<>();
Bharat saraswal246a70c2016-06-16 13:24:06 +0530169 for (Object obj : project.getDependencies()) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530170
Bharat saraswal246a70c2016-06-16 13:24:06 +0530171 Dependency dependency = (Dependency) obj;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530172 path.append(localRepository.getBasedir());
173 path.append(SLASH);
174 path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId()));
175 path.append(SLASH);
176 path.append(dependency.getArtifactId());
177 path.append(SLASH);
178 path.append(dependency.getVersion());
179 path.append(SLASH);
180 path.append(dependency.getArtifactId() + HYPHEN + dependency.getVersion() + PERIOD + JAR);
181 File jarFile = new File(path.toString());
182 if (jarFile.exists()) {
183 jarPaths.add(path.toString());
184 }
185 path.delete(0, path.length());
186 }
187
188 for (ArtifactRepository repo : remoteRepos) {
189 // TODO: add resolver for remote repo.
190 }
191 return jarPaths;
192 }
193
194 /**
195 * Resolves inter jar dependencies.
196 *
197 * @param project current maven project
198 * @param localRepository local maven repository
199 * @param remoteRepos list of remote repository
200 * @param directory directory for serialized files
201 * @return list of resolved datamodel nodes
202 * @throws IOException when fails to do IO operations
203 */
204 public static List<YangNode> resolveInterJarDependencies(MavenProject project, ArtifactRepository localRepository,
205 List<ArtifactRepository> remoteRepos, String directory)
206 throws IOException {
207
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530208 List<String> dependeciesJarPaths = resolveDependencyJarPath(project, localRepository, remoteRepos);
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530209 List<YangNode> resolvedDataModelNodes = new ArrayList<>();
210 for (String dependecy : dependeciesJarPaths) {
211 resolvedDataModelNodes.addAll(deSerializeDataModel(parseJarFile(dependecy, directory)));
212 }
213 return resolvedDataModelNodes;
214 }
215
216 /**
217 * Parses jar file and returns list of serialized file names.
218 *
219 * @param jarFile jar file to be parsed
220 * @param directory directory for keeping the searized files
221 * @return list of serialized files
222 * @throws IOException when fails to do IO operations
223 */
224 public static List<String> parseJarFile(String jarFile, String directory)
225 throws IOException {
226
227 List<String> serailizedFiles = new ArrayList<>();
228 JarFile jar = new JarFile(jarFile);
229 Enumeration<?> enumEntries = jar.entries();
230
231 File serializedFileDir = new File(directory);
232 serializedFileDir.mkdirs();
233 while (enumEntries.hasMoreElements()) {
234 JarEntry file = (JarEntry) enumEntries.nextElement();
235 if (file.getName().endsWith(SERIALIZED_FILE_EXTENSION)) {
236 if (file.getName().contains(SLASH)) {
237 String[] strArray = file.getName().split(SLASH);
238 String tempPath = "";
239 for (int i = 0; i < strArray.length - 1; i++) {
240 tempPath = SLASH + tempPath + SLASH + strArray[i];
241 }
242 File dir = new File(directory + tempPath);
243 dir.mkdirs();
244 }
245 File serailizedFile = new File(directory + SLASH + file.getName());
246 if (file.isDirectory()) {
247 serailizedFile.mkdirs();
248 continue;
249 }
250 InputStream inputStream = jar.getInputStream(file);
251
252 FileOutputStream fileOutputStream = new FileOutputStream(serailizedFile);
253 while (inputStream.available() > 0) {
254 fileOutputStream.write(inputStream.read());
255 }
256 fileOutputStream.close();
257 inputStream.close();
258 serailizedFiles.add(serailizedFile.toString());
259 }
260 }
261 jar.close();
262 return serailizedFiles;
263 }
264
265 /* Adds directory to resources of project */
266 private static void addToProjectResource(String dir, MavenProject project) {
267 Resource rsc = new Resource();
268 rsc.setDirectory(dir);
269 project.addResource(rsc);
270 }
271}