blob: fcd296667f169320cbb530ba8037c9947e4194ee [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;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053047import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.deSerializeDataModel;
Bharat saraswal246a70c2016-06-16 13:24:06 +053048import 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);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530192 yangPluginConfig.setManagerCodeGenDir(TARGET);
Bharat saraswal246a70c2016-06-16 13:24:06 +0530193
194 utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
195
196 testIfFlowClassifierFilesExists();
197 testIfPortPairFileDoesNotExist();
198 deleteDirectory(TARGET);
199 deleteTestSerFile();
200 }
201
202 /**
203 * Test if flow classifier code is generated.
204 */
205 private void testIfFlowClassifierFilesExists() {
206 File folder = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_FOLDER);
207 File file = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_MANAGER);
208 assertThat(true, is(folder.exists()));
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530209 assertThat(false, is(file.exists()));
Bharat saraswal246a70c2016-06-16 13:24:06 +0530210 }
211
212 /**
213 * Tests if port pair code is not generated.
214 */
215 private void testIfPortPairFileDoesNotExist() {
216 File folder = new File(System.getProperty("user.dir") + SLASH + PORT_PAIR_FOLDER);
217 assertThat(false, is(folder.exists()));
218 }
219
220 /**
221 * Need to remove port-pair YANG file info from the set so , serialized file info can be
222 * tested.
223 *
224 * @param fileInfoSet YANG file info set
225 * @return updated file info set
226 */
227 private Set<YangFileInfo> removeFileInfoFromSet(Set<YangFileInfo> fileInfoSet) {
228 String portPairFile = System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + "portpair.yang";
229 for (YangFileInfo fileInfo : fileInfoSet) {
230 if (fileInfo.getYangFileName().equals(portPairFile)) {
231 fileInfoSet.remove(fileInfo);
232 return fileInfoSet;
233 }
234 }
235 return fileInfoSet;
236 }
237
238 /**
239 * Provides test jar files for linker.
240 *
241 * @throws IOException when fails to do IO operations
242 */
243 private void provideTestJarFile() throws IOException {
244
245 MavenProject project = new MavenProject();
246 serializeDataModel(TARGET, utilManager.getYangFileInfoSet(), project, false);
247 createTestJar();
248 }
249
250 /**
251 * Deletes serialized file.
252 */
253 private void deleteTestSerFile() {
254 File ser = new File(System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + SER_FILE_NAME);
255 ser.delete();
256 }
257
258 /**
259 * Parses file info list and returns true if file info list contains the serialized file info.
260 *
261 * @param yangFileInfoIterator file info list iterator
262 * @return true if present
263 */
264 private boolean parseFileInfoSet(Iterator<YangFileInfo> yangFileInfoIterator) {
265 YangFileInfo yangFileInfo = yangFileInfoIterator.next();
266 while (yangFileInfoIterator.hasNext()) {
267 if (yangFileInfo.getRootNode().getName().equals("port-pair")) {
268 return true;
269 } else if (yangFileInfo.getRootNode().getName().equals("flow-classifier")) {
270 return true;
271 }
272 yangFileInfo = yangFileInfoIterator.next();
273 }
274 return false;
275
276 }
277
278 /**
279 * Returns list of test jar files.
280 *
281 * @param searchdir search directory
282 * @return list of test jar files
283 */
284 private List<String> getListOfTestJar(String searchdir) {
285 List<String> jarFiles = new ArrayList<>();
286
287 File directory = new File(searchdir + "/");
288 File[] files = directory.listFiles();
289
290 for (File file : files) {
291 if (!file.isDirectory()) {
292 jarFiles.add(file.toString());
293 }
294 }
295
296 return jarFiles;
297 }
298
299 /**
300 * Adds data model nodes of jar to file info set.
301 *
302 * @param jarFile jar file name
303 * @throws IOException when fails to do IO operations
304 */
305 private void addInterJarRootNodes(String jarFile) throws IOException {
306 try {
307 List<YangNode> interJarResolvedNodes = deSerializeDataModel(parseJarFile(jarFile, TARGET));
308
309 for (YangNode node : interJarResolvedNodes) {
310 YangFileInfo dependentFileInfo = new YangFileInfo();
311 dependentFileInfo.setRootNode(node);
312 dependentFileInfo.setForTranslator(false);
313 dependentFileInfo.setYangFileName(node.getName());
314 utilManager.getYangFileInfoSet().add(dependentFileInfo);
315 }
316 } catch (IOException e) {
317 throw new IOException("failed to resolve in interjar scenario.");
318 }
319 }
320
321 /**
322 * Creates a temporary test jar files.
323 */
324 private void createTestJar() {
325
326 File file = new File(TARGET + TARGET_RESOURCE_PATH);
327 File[] files = file.listFiles();
328
329 String[] source = new String[files.length];
330
331 for (int i = 0; i < files.length; i++) {
332 source[i] = files[i].toString();
333 }
334 byte[] buf = new byte[1024];
335
336 try {
337 String target = TARGET + JAR_FILE_NAME;
338 JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
339 for (String element : source) {
340 FileInputStream in = new FileInputStream(element);
341 out.putNextEntry(new JarEntry(element));
342 int len;
343 while ((len = in.read(buf)) > 0) {
344 out.write(buf, 0, len);
345 }
346 out.closeEntry();
347 in.close();
348 }
349 out.close();
350 } catch (IOException e) {
351 }
352 }
353
354}