Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.plugin.manager; |
| 18 | |
| 19 | import java.io.File; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 20 | import java.io.FileOutputStream; |
| 21 | import java.io.IOException; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 22 | import java.io.ObjectOutputStream; |
| 23 | import java.nio.file.Files; |
| 24 | import java.nio.file.StandardCopyOption; |
| 25 | import java.util.ArrayList; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.Set; |
Bharat saraswal | 246a70c | 2016-06-16 13:24:06 +0530 | [diff] [blame] | 29 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 30 | import org.apache.maven.artifact.repository.ArtifactRepository; |
| 31 | import org.apache.maven.model.Dependency; |
| 32 | import org.apache.maven.model.Resource; |
| 33 | import org.apache.maven.project.MavenProject; |
| 34 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 35 | import org.onosproject.yangutils.datamodel.utils.DataModelUtils; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.sonatype.plexus.build.incremental.BuildContext; |
| 38 | |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.JAR; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Represents YANG plugin utilities. |
| 50 | */ |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 51 | final class YangPluginUtils { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 52 | |
| 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 saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 69 | static void addToCompilationRoot(String source, MavenProject project, BuildContext context) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 70 | 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 saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 83 | static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project) |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 84 | 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 saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 127 | static void serializeDataModel(String directory, Set<YangFileInfo> fileInfoSet, |
| 128 | MavenProject project, boolean operation) throws IOException { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 129 | |
| 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 saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 140 | String serFileName = serFileDirPath + fileInfo.getRootNode().getName() |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 141 | + 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 Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 152 | * 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 saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 159 | private static List<String> resolveDependencyJarPath(MavenProject project, ArtifactRepository localRepository, |
| 160 | List<ArtifactRepository> remoteRepos) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 161 | |
| 162 | StringBuilder path = new StringBuilder(); |
| 163 | List<String> jarPaths = new ArrayList<>(); |
Bharat saraswal | 246a70c | 2016-06-16 13:24:06 +0530 | [diff] [blame] | 164 | for (Object obj : project.getDependencies()) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 165 | |
Bharat saraswal | 246a70c | 2016-06-16 13:24:06 +0530 | [diff] [blame] | 166 | Dependency dependency = (Dependency) obj; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 167 | 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 saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 199 | static List<YangNode> resolveInterJarDependencies(MavenProject project, ArtifactRepository localRepository, |
| 200 | List<ArtifactRepository> remoteRepos, String directory) |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 201 | throws IOException { |
| 202 | |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 203 | List<String> dependenciesJarPaths = resolveDependencyJarPath(project, localRepository, remoteRepos); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 204 | List<YangNode> resolvedDataModelNodes = new ArrayList<>(); |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 205 | for (String dependency : dependenciesJarPaths) { |
| 206 | resolvedDataModelNodes.addAll(DataModelUtils.parseJarFile(dependency, directory)); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 207 | } |
| 208 | return resolvedDataModelNodes; |
| 209 | } |
| 210 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 211 | /* 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 | } |