Bharat saraswal
Committed by Gerrit Code Review

[ONOS-4712] inter jar file linker unit test case.

Change-Id: I566b9ce59cb37e664914f99263c597ead7ce5e11
...@@ -32,6 +32,7 @@ import java.util.List; ...@@ -32,6 +32,7 @@ import java.util.List;
32 import java.util.Set; 32 import java.util.Set;
33 import java.util.jar.JarEntry; 33 import java.util.jar.JarEntry;
34 import java.util.jar.JarFile; 34 import java.util.jar.JarFile;
35 +
35 import org.apache.maven.artifact.repository.ArtifactRepository; 36 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.model.Dependency; 37 import org.apache.maven.model.Dependency;
37 import org.apache.maven.model.Resource; 38 import org.apache.maven.model.Resource;
...@@ -192,8 +193,9 @@ public final class YangPluginUtils { ...@@ -192,8 +193,9 @@ public final class YangPluginUtils {
192 193
193 StringBuilder path = new StringBuilder(); 194 StringBuilder path = new StringBuilder();
194 List<String> jarPaths = new ArrayList<>(); 195 List<String> jarPaths = new ArrayList<>();
195 - for (Dependency dependency : project.getDependencies()) { 196 + for (Object obj : project.getDependencies()) {
196 197
198 + Dependency dependency = (Dependency) obj;
197 path.append(localRepository.getBasedir()); 199 path.append(localRepository.getBasedir());
198 path.append(SLASH); 200 path.append(SLASH);
199 path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId())); 201 path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId()));
......
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;
20 +import java.io.FileInputStream;
21 +import java.io.FileOutputStream;
22 +import java.io.IOException;
23 +import java.util.ArrayList;
24 +import java.util.Iterator;
25 +import java.util.List;
26 +import java.util.ListIterator;
27 +import java.util.Set;
28 +import java.util.jar.JarEntry;
29 +import java.util.jar.JarOutputStream;
30 +
31 +import org.apache.maven.plugin.MojoExecutionException;
32 +import org.apache.maven.project.MavenProject;
33 +import org.junit.Test;
34 +import org.onosproject.yangutils.datamodel.YangContainer;
35 +import org.onosproject.yangutils.datamodel.YangDerivedInfo;
36 +import org.onosproject.yangutils.datamodel.YangGrouping;
37 +import org.onosproject.yangutils.datamodel.YangLeaf;
38 +import org.onosproject.yangutils.datamodel.YangNode;
39 +import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
40 +import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
41 +
42 +import static org.hamcrest.MatcherAssert.assertThat;
43 +import static org.hamcrest.core.Is.is;
44 +import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
45 +import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
46 +import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
47 +import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.deSerializeDataModel;
48 +import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.parseJarFile;
49 +import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.serializeDataModel;
50 +import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
51 +import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
52 +import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
53 +import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
54 +
55 +/**
56 + * Unit test case for inter jar linker.
57 + */
58 +public class InterJarLinkerTest {
59 +
60 + private final YangUtilManager utilManager = new YangUtilManager();
61 +
62 + private static final String TARGET = "target/interJarFileLinking/";
63 + private static final String YANG_FILES_DIR = "src/test/resources/interJarFileLinking/yangFiles/";
64 + private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
65 + private static final String JAR_FILE_NAME = "onlab-test-1.7.0-SNAPSHOT.jar";
66 + private static final String SER_FILE_NAME = "portPair.ser";
67 +
68 + private static final String FLOW_CLASSIFIER_FOLDER = "target/interJarFileLinking/org/onosproject"
69 + + "/yang/gen/v1/sfc/flowclassifier/rev20160524";
70 + private static final String PORT_PAIR_FOLDER = "target/interJarFileLinking/org/onosproject"
71 + + "/yang/gen/v1/sfc/portpair/rev20160524";
72 + private static final String FLOW_CLASSIFIER_MANAGER = FLOW_CLASSIFIER_FOLDER + SLASH + "FlowClassifierManager.java";
73 +
74 + /**
75 + * Unit test case for a single jar dependency.
76 + *
77 + * @throws IOException when fails to do IO operations
78 + * @throws MojoExecutionException when fails to do mojo operations
79 + */
80 + @Test
81 + public void processSingleJarLinking()
82 + throws IOException, MojoExecutionException {
83 + utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(YANG_FILES_DIR));
84 +
85 + int size1 = utilManager.getYangFileInfoSet().size();
86 + utilManager.parseYangFileInfoSet();
87 +
88 + provideTestJarFile();
89 + utilManager.setYangFileInfoSet(removeFileInfoFromSet(utilManager.getYangFileInfoSet()));
90 +
91 + for (String file : getListOfTestJar(TARGET)) {
92 + addInterJarRootNodes(file);
93 + }
94 +
95 + utilManager.resolveDependenciesUsingLinker();
96 +
97 + int size2 = utilManager.getYangFileInfoSet().size();
98 + assertThat(true, is(size1 != size2));
99 + assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
100 +
101 + deleteDirectory(TARGET);
102 + deleteTestSerFile();
103 + }
104 +
105 + /**
106 + * Unit test case for a multiple jar dependency.
107 + *
108 + * @throws IOException when fails to do IO operations
109 + * @throws MojoExecutionException when fails to do mojo operations
110 + */
111 + @Test
112 + public void processMultipleJarLinking()
113 + throws IOException, MojoExecutionException {
114 + utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(YANG_FILES_DIR));
115 +
116 + int size1 = utilManager.getYangFileInfoSet().size();
117 + utilManager.parseYangFileInfoSet();
118 +
119 + provideTestJarFile();
120 + utilManager.setYangFileInfoSet(removeFileInfoFromSet(utilManager.getYangFileInfoSet()));
121 + for (String file : getListOfTestJar(TARGET)) {
122 + addInterJarRootNodes(file);
123 + }
124 +
125 + utilManager.resolveDependenciesUsingLinker();
126 + int size2 = utilManager.getYangFileInfoSet().size();
127 + assertThat(true, is(size1 != size2));
128 + assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
129 + assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
130 +
131 + /*
132 + * grouping flow-classifier {
133 + * container flow-classifier {
134 + * leaf id {
135 + * type flow-classifier-id;
136 + * }
137 + *
138 + * leaf tenant-id {
139 + * type port-pair:tenant-id;
140 + * }
141 + * .
142 + * .
143 + * .
144 + *
145 + */
146 +
147 + Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
148 +
149 + YangFileInfo yangFileInfo = yangFileInfoIterator.next();
150 +
151 + while (yangFileInfoIterator.hasNext()) {
152 + if (yangFileInfo.getRootNode().getName().equals("flow-classifier")) {
153 + break;
154 + }
155 + yangFileInfo = yangFileInfoIterator.next();
156 + }
157 +
158 + YangNode node = yangFileInfo.getRootNode();
159 + node = node.getChild();
160 + while (node != null) {
161 + if (node instanceof YangGrouping) {
162 + break;
163 + }
164 + node = node.getNextSibling();
165 + }
166 +
167 + node = node.getChild();
168 + ListIterator<YangLeaf> leafIterator = ((YangContainer) node).getListOfLeaf().listIterator();
169 + YangLeaf leafInfo = leafIterator.next();
170 +
171 + assertThat(leafInfo.getName(), is("id"));
172 + assertThat(leafInfo.getDataType().getDataTypeName(), is("flow-classifier-id"));
173 + assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
174 +
175 + leafInfo = leafIterator.next();
176 +
177 + assertThat(leafInfo.getName(), is("tenant-id"));
178 + assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
179 +
180 + assertThat(true, is(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef()
181 + .getName().equals("tenant-id")));
182 +
183 + assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
184 +
185 + YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
186 +
187 + // Check for the effective built-in type.
188 + assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
189 +
190 + YangPluginConfig yangPluginConfig = new YangPluginConfig();
191 + yangPluginConfig.setCodeGenDir(TARGET);
192 +
193 + utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
194 +
195 + testIfFlowClassifierFilesExists();
196 + testIfPortPairFileDoesNotExist();
197 + deleteDirectory(TARGET);
198 + deleteTestSerFile();
199 + }
200 +
201 + /**
202 + * Test if flow classifier code is generated.
203 + */
204 + private void testIfFlowClassifierFilesExists() {
205 + File folder = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_FOLDER);
206 + File file = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_MANAGER);
207 + assertThat(true, is(folder.exists()));
208 + assertThat(true, is(file.exists()));
209 + }
210 +
211 + /**
212 + * Tests if port pair code is not generated.
213 + */
214 + private void testIfPortPairFileDoesNotExist() {
215 + File folder = new File(System.getProperty("user.dir") + SLASH + PORT_PAIR_FOLDER);
216 + assertThat(false, is(folder.exists()));
217 + }
218 +
219 + /**
220 + * Need to remove port-pair YANG file info from the set so , serialized file info can be
221 + * tested.
222 + *
223 + * @param fileInfoSet YANG file info set
224 + * @return updated file info set
225 + */
226 + private Set<YangFileInfo> removeFileInfoFromSet(Set<YangFileInfo> fileInfoSet) {
227 + String portPairFile = System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + "portpair.yang";
228 + for (YangFileInfo fileInfo : fileInfoSet) {
229 + if (fileInfo.getYangFileName().equals(portPairFile)) {
230 + fileInfoSet.remove(fileInfo);
231 + return fileInfoSet;
232 + }
233 + }
234 + return fileInfoSet;
235 + }
236 +
237 + /**
238 + * Provides test jar files for linker.
239 + *
240 + * @throws IOException when fails to do IO operations
241 + */
242 + private void provideTestJarFile() throws IOException {
243 +
244 + MavenProject project = new MavenProject();
245 + serializeDataModel(TARGET, utilManager.getYangFileInfoSet(), project, false);
246 + createTestJar();
247 + }
248 +
249 + /**
250 + * Deletes serialized file.
251 + */
252 + private void deleteTestSerFile() {
253 + File ser = new File(System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + SER_FILE_NAME);
254 + ser.delete();
255 + }
256 +
257 + /**
258 + * Parses file info list and returns true if file info list contains the serialized file info.
259 + *
260 + * @param yangFileInfoIterator file info list iterator
261 + * @return true if present
262 + */
263 + private boolean parseFileInfoSet(Iterator<YangFileInfo> yangFileInfoIterator) {
264 + YangFileInfo yangFileInfo = yangFileInfoIterator.next();
265 + while (yangFileInfoIterator.hasNext()) {
266 + if (yangFileInfo.getRootNode().getName().equals("port-pair")) {
267 + return true;
268 + } else if (yangFileInfo.getRootNode().getName().equals("flow-classifier")) {
269 + return true;
270 + }
271 + yangFileInfo = yangFileInfoIterator.next();
272 + }
273 + return false;
274 +
275 + }
276 +
277 + /**
278 + * Returns list of test jar files.
279 + *
280 + * @param searchdir search directory
281 + * @return list of test jar files
282 + */
283 + private List<String> getListOfTestJar(String searchdir) {
284 + List<String> jarFiles = new ArrayList<>();
285 +
286 + File directory = new File(searchdir + "/");
287 + File[] files = directory.listFiles();
288 +
289 + for (File file : files) {
290 + if (!file.isDirectory()) {
291 + jarFiles.add(file.toString());
292 + }
293 + }
294 +
295 + return jarFiles;
296 + }
297 +
298 + /**
299 + * Adds data model nodes of jar to file info set.
300 + *
301 + * @param jarFile jar file name
302 + * @throws IOException when fails to do IO operations
303 + */
304 + private void addInterJarRootNodes(String jarFile) throws IOException {
305 + try {
306 + List<YangNode> interJarResolvedNodes = deSerializeDataModel(parseJarFile(jarFile, TARGET));
307 +
308 + for (YangNode node : interJarResolvedNodes) {
309 + YangFileInfo dependentFileInfo = new YangFileInfo();
310 + dependentFileInfo.setRootNode(node);
311 + dependentFileInfo.setForTranslator(false);
312 + dependentFileInfo.setYangFileName(node.getName());
313 + utilManager.getYangFileInfoSet().add(dependentFileInfo);
314 + }
315 + } catch (IOException e) {
316 + throw new IOException("failed to resolve in interjar scenario.");
317 + }
318 + }
319 +
320 + /**
321 + * Creates a temporary test jar files.
322 + */
323 + private void createTestJar() {
324 +
325 + File file = new File(TARGET + TARGET_RESOURCE_PATH);
326 + File[] files = file.listFiles();
327 +
328 + String[] source = new String[files.length];
329 +
330 + for (int i = 0; i < files.length; i++) {
331 + source[i] = files[i].toString();
332 + }
333 + byte[] buf = new byte[1024];
334 +
335 + try {
336 + String target = TARGET + JAR_FILE_NAME;
337 + JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
338 + for (String element : source) {
339 + FileInputStream in = new FileInputStream(element);
340 + out.putNextEntry(new JarEntry(element));
341 + int len;
342 + while ((len = in.read(buf)) > 0) {
343 + out.write(buf, 0, len);
344 + }
345 + out.closeEntry();
346 + in.close();
347 + }
348 + out.close();
349 + } catch (IOException e) {
350 + }
351 + }
352 +
353 +}
1 +module flow-classifier {
2 +
3 + yang-version 1;
4 +
5 + namespace "sfc.flowclassifier";
6 +
7 + prefix "flow-classifier";
8 +
9 + import "port-pair" {
10 + prefix "port-pair";
11 + }
12 +
13 + organization "ON-LAB";
14 +
15 + description "This submodule defines for flow classifier.";
16 +
17 + revision "2016-05-24" {
18 + description "Initial revision.";
19 + }
20 +
21 + typedef flow-classifier-id {
22 + type port-pair:uuid;
23 + }
24 +
25 + typedef IpPrefix {
26 + type string;
27 + }
28 +
29 + typedef VirtualPortId {
30 + type string;
31 + }
32 +
33 + grouping flow-classifier {
34 + container flow-classifier {
35 + leaf id {
36 + type flow-classifier-id;
37 + }
38 +
39 + leaf tenant-id {
40 + type port-pair:tenant-id;
41 + }
42 +
43 + leaf name {
44 + type string;
45 + }
46 +
47 + leaf description {
48 + type string;
49 + }
50 +
51 + leaf etherType {
52 + type string;
53 + }
54 +
55 + leaf protocol {
56 + type string;
57 + }
58 +
59 + leaf priority {
60 + type int32;
61 + }
62 +
63 + leaf minSrcPortRange {
64 + type int32;
65 + }
66 +
67 + leaf maxSrcPortRange {
68 + type int32;
69 + }
70 +
71 + leaf minDstPortRange {
72 + type int32;
73 + }
74 +
75 + leaf maxDstPortRange {
76 + type int32;
77 + }
78 +
79 + leaf srcIpPrefix {
80 + type IpPrefix;
81 + }
82 +
83 + leaf dstIpPrefix {
84 + type IpPrefix;
85 + }
86 +
87 + leaf srcPort {
88 + type VirtualPortId;
89 + }
90 +
91 + leaf dstPort {
92 + type VirtualPortId;
93 + }
94 + }
95 + }
96 + rpc exists {
97 + input {
98 + leaf id {
99 + type flow-classifier-id;
100 + }
101 + }
102 + output {
103 + leaf is-present {
104 + type boolean;
105 + }
106 + }
107 + }
108 +
109 + rpc get-flow-classifier-count {
110 +
111 + output {
112 + leaf count {
113 + type int32;
114 + }
115 + }
116 + }
117 +
118 + rpc get-flow-classifier {
119 + input {
120 + leaf id {
121 + type flow-classifier-id;
122 + }
123 + }
124 + output {
125 + uses flow-classifier;
126 + }
127 + }
128 +
129 + rpc create-flow-classifier {
130 + input {
131 + uses flow-classifier;
132 + }
133 + output {
134 + leaf is-created {
135 + type boolean;
136 + }
137 + }
138 + }
139 +
140 + rpc update-flow-classifier {
141 + input {
142 + uses flow-classifier;
143 + }
144 + output {
145 + leaf is-updated {
146 + type boolean;
147 + }
148 + }
149 + }
150 +
151 + rpc remove-flow-classifier {
152 + input {
153 + leaf id {
154 + type flow-classifier-id;
155 + }
156 + }
157 + output {
158 + leaf is-removed {
159 + type boolean;
160 + }
161 + }
162 + }
163 +
164 + notification Flow-Classifier-Put {
165 + uses flow-classifier;
166 + }
167 +
168 + notification Flow-Classifier-Delete {
169 + uses flow-classifier;
170 + }
171 +
172 + notification Flow-Classifier-Update {
173 + uses flow-classifier;
174 + }
175 +}
1 +module port-pair {
2 +
3 + yang-version 1;
4 +
5 + namespace "sfc.portpair";
6 +
7 + prefix "port-pair";
8 +
9 + organization "Huawei india pvt. ltd..";
10 +
11 + description "This submodule defines for port pair.";
12 +
13 + revision "2016-05-24" {
14 + description "Initial revision.";
15 + }
16 +
17 + typedef uuid {
18 + type string;
19 + }
20 +
21 + typedef port-pair-id {
22 + type uuid;
23 + }
24 +
25 + typedef tenant-id {
26 + type uuid;
27 + }
28 +
29 + grouping port-pair {
30 + container port-pair {
31 +
32 + leaf name {
33 + type string;
34 + }
35 +
36 + leaf id {
37 + type port-pair-id;
38 + }
39 +
40 + leaf tenantIdentifier {
41 + type tenant-id;
42 + }
43 +
44 + leaf description {
45 + type string;
46 + }
47 +
48 + leaf ingress {
49 + type uuid;
50 + }
51 +
52 + leaf egress {
53 + type uuid;
54 + }
55 + }
56 + }
57 + rpc exists {
58 + input {
59 + leaf id {
60 + type port-pair-id;
61 + }
62 + }
63 + output {
64 + leaf is-present {
65 + type boolean;
66 + }
67 + }
68 + }
69 +
70 + rpc get-port-pair-count {
71 +
72 + output {
73 + leaf count {
74 + type int32;
75 + }
76 + }
77 + }
78 +
79 + rpc get-port-pair {
80 + input {
81 + leaf id {
82 + type port-pair-id;
83 + }
84 + }
85 + output {
86 + uses port-pair;
87 + }
88 + }
89 +
90 + rpc create-port-pair {
91 + input {
92 + uses port-pair;
93 + }
94 + output {
95 + leaf is-created {
96 + type boolean;
97 + }
98 + }
99 + }
100 +
101 + rpc update-port-pair {
102 + input {
103 + uses port-pair;
104 + }
105 + output {
106 + leaf is-updated {
107 + type boolean;
108 + }
109 + }
110 + }
111 +
112 + rpc remove-port-pair {
113 + input {
114 + leaf id {
115 + type port-pair-id;
116 + }
117 + }
118 + output {
119 + leaf is-removed {
120 + type boolean;
121 + }
122 + }
123 + }
124 +
125 +
126 + notification port-pair-put {
127 + uses port-pair;
128 + }
129 +
130 + notification port-pair-Delete {
131 + uses port-pair;
132 + }
133 +
134 + notification port-pair-Update {
135 + uses port-pair;
136 + }
137 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + container valid {
6 + leaf invalid-interval {
7 + type "uint16";
8 + units "seconds";
9 + status current;
10 + reference "RFC 6020";
11 + }
12 + }
13 + container invalid {
14 + leaf invalid-interval {
15 + type "uint16";
16 + units "seconds";
17 + status current;
18 + reference "RFC 6020";
19 + }
20 + }
21 + container valid2 {
22 + leaf invalid-interval {
23 + type "uint16";
24 + units "seconds";
25 + status current;
26 + reference "RFC 6020";
27 + }
28 + }
29 +}