Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit afdcd1c

Browse files
y-higuchigauravagrawal07
authored andcommitted
Fix for dealing with multiple yang deps
Change-Id: I77fd4d1118db0b238549f50f4dac24c0616b5035
1 parent d43416b commit afdcd1c

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@
6767
import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
6868
import org.onosproject.yang.model.LeafObjectType;
6969
import org.onosproject.yang.model.SchemaId;
70+
import org.slf4j.Logger;
7071

7172
import java.io.File;
7273
import java.io.FileOutputStream;
7374
import java.io.IOException;
7475
import java.io.InputStream;
76+
import java.nio.file.Files;
77+
import java.nio.file.Path;
78+
import java.nio.file.Paths;
7579
import java.text.SimpleDateFormat;
7680
import java.util.ArrayList;
7781
import java.util.Collection;
@@ -111,11 +115,14 @@
111115
import static org.onosproject.yang.model.LeafObjectType.LONG;
112116
import static org.onosproject.yang.model.LeafObjectType.SHORT;
113117
import static org.onosproject.yang.model.LeafObjectType.STRING;
118+
import static org.slf4j.LoggerFactory.getLogger;
114119

115120
/**
116121
* Represents utilities for data model tree.
117122
*/
118123
public final class DataModelUtils {
124+
private static final Logger log = getLogger(DataModelUtils.class);
125+
119126
public static final String TRUE = "true";
120127
public static final String FALSE = "false";
121128
public static final String FMT_NOT_EXIST =
@@ -865,49 +872,56 @@ private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leav
865872
}
866873

867874
/**
868-
* Parses jar file and returns list of serialized file names.
875+
* Extracts contents from jar file and returns path to YangMetaData.
869876
*
870877
* @param jarFile jar file to be parsed
871-
* @param directory directory where to search
872-
* @return list of serialized files
878+
* @param directory directory where to output
879+
* @return path to serialized YangMetaData file copied in {@code directory}
873880
* @throws IOException when fails to do IO operations
874881
*/
875882
public static File parseDepSchemaPath(String jarFile, String directory)
876883
throws IOException {
877-
JarFile jar = new JarFile(jarFile);
878-
Enumeration<?> enumEntries = jar.entries();
879-
File serializedFile = null;
880-
while (enumEntries.hasMoreElements()) {
881-
JarEntry file = (JarEntry) enumEntries.nextElement();
882-
if (file.getName().endsWith(".ser")) {
883-
884-
if (file.getName().contains(SLASH)) {
885-
String[] strArray = file.getName().split(SLASH);
886-
String tempPath = "";
887-
for (int i = 0; i < strArray.length - 1; i++) {
888-
tempPath = SLASH + tempPath + SLASH + strArray[i];
884+
log.trace("From jarfile: {}", jarFile);
885+
try (JarFile jar = new JarFile(jarFile)) {
886+
Enumeration<?> enumEntries = jar.entries();
887+
File serializedFile = null;
888+
while (enumEntries.hasMoreElements()) {
889+
JarEntry file = (JarEntry) enumEntries.nextElement();
890+
if (file.getName().endsWith("YangMetaData.ser")) {
891+
892+
Path jarRelPath = Paths.get(file.getName());
893+
Path outBase = Paths.get(directory);
894+
String inFilename = Paths.get(jarFile).getFileName().toString();
895+
String inBasename = inFilename.substring(0, inFilename.length() - ".jar".length());
896+
// inject input jar basename right before the filename.
897+
Path serializedPath = outBase
898+
.resolve(jarRelPath.getParent())
899+
.resolve(inBasename)
900+
.resolve(jarRelPath.getFileName());
901+
902+
if (Files.isDirectory(serializedPath)) {
903+
Files.createDirectories(serializedPath.getParent());
904+
continue;
905+
} else {
906+
Files.createDirectories(serializedPath.getParent());
889907
}
890-
File dir = new File(directory + tempPath);
891-
dir.mkdirs();
892-
}
893-
serializedFile = new File(directory + SLASH + file.getName());
894-
if (file.isDirectory()) {
895-
serializedFile.mkdirs();
896-
continue;
908+
serializedFile = serializedPath.toFile();
909+
log.trace(" writing {} to {}", file.getName(), serializedFile);
910+
InputStream inputStream = jar.getInputStream(file);
911+
912+
FileOutputStream fileOutputStream = new FileOutputStream(serializedFile);
913+
IOUtils.copy(inputStream, fileOutputStream);
914+
fileOutputStream.close();
915+
inputStream.close();
916+
917+
//As of now only one metadata files will be there so if we
918+
// found one then we should break the loop.
919+
return serializedFile;
897920
}
898-
InputStream inputStream = jar.getInputStream(file);
899-
900-
FileOutputStream fileOutputStream = new FileOutputStream(serializedFile);
901-
IOUtils.copy(inputStream, fileOutputStream);
902-
fileOutputStream.close();
903-
inputStream.close();
904-
//As of now only one metadata files will be there so if we
905-
// found one then we should break the loop.
906-
break;
907921
}
908922
}
909-
jar.close();
910-
return serializedFile;
923+
// Not found
924+
return null;
911925
}
912926

913927
/**

compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/YangCompilerManager.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,18 @@ public static void setNodeInfo(YangModel model, List<YangNodeInfo> infos) {
607607
}
608608

609609
/**
610-
* Parses jar file and returns YANG model.
610+
* Extracts .yang files and YangMetaData from jar file to
611+
* target directory and returns YangModel found.
611612
*
612613
* @param jarFile jar file to be parsed
613-
* @param directory directory where to search
614-
* @return YANG model
614+
* @param directory directory where to extract files to
615+
* @return YangModel found
615616
* @throws IOException when fails to do IO operations
616617
*/
617618
public static YangModel parseJarFile(String jarFile, String directory)
618619
throws IOException {
619620

621+
log.trace("Searching YangModel in {}", jarFile);
620622
YangModel model = null;
621623
try (JarFile jar = new JarFile(jarFile)) {
622624
Enumeration<?> enumEntries = jar.entries();
@@ -643,8 +645,13 @@ public static YangModel parseJarFile(String jarFile, String directory)
643645

644646
IOUtils.copy(inputStream, fileOutputStream);
645647
fileOutputStream.close();
646-
if (serializedFile.getName().endsWith(YANG_META_DATA)) {
648+
// FIXME hack to return first model found
649+
if (model == null &&
650+
serializedFile.getName().endsWith(YANG_META_DATA)) {
647651
model = deSerializeDataModel(serializedFile.toString());
652+
log.trace(" found {} at {}",
653+
model.getYangModelId(),
654+
serializedFile.getName());
648655
}
649656
}
650657
}

compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public void execute() throws YangParsingException {
7777
//Need to get dependent schema paths to give inter jar dependencies.
7878
for (String jar : depJar) {
7979
try {
80+
// Note: when there's multiple deps, it all gets copied to
81+
// same directory.
8082
File path = parseDepSchemaPath(jar, outputDirectory);
8183
if (path != null) {
8284
bldr.addDependentSchema(Paths.get(path.getAbsolutePath()));

compiler/plugin/maven/src/main/java/org/onosproject/yang/compiler/plugin/maven/YangPluginUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ static List<Path> resolveInterJarDependencies(MavenProject project,
120120
project, localRepository, remoteRepos);
121121
List<Path> serFilePaths = new ArrayList<>();
122122
for (String dependency : depJars) {
123+
// Note: when there's multiple deps, it all gets copied to
124+
// same directory.
123125
File path = parseDepSchemaPath(dependency, directory);
124126
if (path != null) {
125127
serFilePaths.add(Paths.get(path.getAbsolutePath()));

0 commit comments

Comments
 (0)