blob: cb9eff91311b2a5c8613c35d31005e7855591f6d [file] [log] [blame]
Bharat saraswal246a70c2016-06-16 13:24:06 +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;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.Iterator;
25import java.util.List;
26import java.util.ListIterator;
27import java.util.Set;
28import java.util.jar.JarEntry;
29import java.util.jar.JarOutputStream;
30
31import org.apache.maven.plugin.MojoExecutionException;
32import org.apache.maven.project.MavenProject;
33import org.junit.Test;
34import org.onosproject.yangutils.datamodel.YangContainer;
35import org.onosproject.yangutils.datamodel.YangDerivedInfo;
36import org.onosproject.yangutils.datamodel.YangGrouping;
37import org.onosproject.yangutils.datamodel.YangLeaf;
38import org.onosproject.yangutils.datamodel.YangNode;
39import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
40import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
41
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.core.Is.is;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053044import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
45import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
Bharat saraswal246a70c2016-06-16 13:24:06 +053046import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
47import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.deSerializeDataModel;
48import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.parseJarFile;
49import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.serializeDataModel;
50import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
51import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
52import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
53import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
54
55/**
56 * Unit test case for inter jar linker.
57 */
58public 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}