blob: 900aca92ff5b188e73441570d14e669a9f3d25f6 [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053040import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal246a70c2016-06-16 13:24:06 +053041
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.core.Is.is;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053044import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053045import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
46import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053047import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.parseJarFile;
Bharat saraswal246a70c2016-06-16 13:24:06 +053048import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.serializeDataModel;
49import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
50import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
51import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
52import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
53
54/**
55 * Unit test case for inter jar linker.
56 */
57public class InterJarLinkerTest {
58
59 private final YangUtilManager utilManager = new YangUtilManager();
60
61 private static final String TARGET = "target/interJarFileLinking/";
62 private static final String YANG_FILES_DIR = "src/test/resources/interJarFileLinking/yangFiles/";
63 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
64 private static final String JAR_FILE_NAME = "onlab-test-1.7.0-SNAPSHOT.jar";
65 private static final String SER_FILE_NAME = "portPair.ser";
66
67 private static final String FLOW_CLASSIFIER_FOLDER = "target/interJarFileLinking/org/onosproject"
68 + "/yang/gen/v1/sfc/flowclassifier/rev20160524";
69 private static final String PORT_PAIR_FOLDER = "target/interJarFileLinking/org/onosproject"
70 + "/yang/gen/v1/sfc/portpair/rev20160524";
71 private static final String FLOW_CLASSIFIER_MANAGER = FLOW_CLASSIFIER_FOLDER + SLASH + "FlowClassifierManager.java";
72
73 /**
74 * Unit test case for a single jar dependency.
75 *
76 * @throws IOException when fails to do IO operations
77 * @throws MojoExecutionException when fails to do mojo operations
78 */
79 @Test
80 public void processSingleJarLinking()
81 throws IOException, MojoExecutionException {
82 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(YANG_FILES_DIR));
83
84 int size1 = utilManager.getYangFileInfoSet().size();
85 utilManager.parseYangFileInfoSet();
86
87 provideTestJarFile();
88 utilManager.setYangFileInfoSet(removeFileInfoFromSet(utilManager.getYangFileInfoSet()));
89
90 for (String file : getListOfTestJar(TARGET)) {
91 addInterJarRootNodes(file);
92 }
93
94 utilManager.resolveDependenciesUsingLinker();
95
96 int size2 = utilManager.getYangFileInfoSet().size();
97 assertThat(true, is(size1 != size2));
98 assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
99
100 deleteDirectory(TARGET);
101 deleteTestSerFile();
102 }
103
104 /**
105 * Unit test case for a multiple jar dependency.
106 *
107 * @throws IOException when fails to do IO operations
108 * @throws MojoExecutionException when fails to do mojo operations
109 */
110 @Test
111 public void processMultipleJarLinking()
112 throws IOException, MojoExecutionException {
113 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(YANG_FILES_DIR));
114
115 int size1 = utilManager.getYangFileInfoSet().size();
116 utilManager.parseYangFileInfoSet();
117
118 provideTestJarFile();
119 utilManager.setYangFileInfoSet(removeFileInfoFromSet(utilManager.getYangFileInfoSet()));
120 for (String file : getListOfTestJar(TARGET)) {
121 addInterJarRootNodes(file);
122 }
123
124 utilManager.resolveDependenciesUsingLinker();
125 int size2 = utilManager.getYangFileInfoSet().size();
126 assertThat(true, is(size1 != size2));
127 assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
128 assertThat(true, is(parseFileInfoSet(utilManager.getYangFileInfoSet().iterator())));
129
130 /*
131 * grouping flow-classifier {
132 * container flow-classifier {
133 * leaf id {
134 * type flow-classifier-id;
135 * }
136 *
137 * leaf tenant-id {
138 * type port-pair:tenant-id;
139 * }
140 * .
141 * .
142 * .
143 *
144 */
145
146 Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
147
148 YangFileInfo yangFileInfo = yangFileInfoIterator.next();
149
150 while (yangFileInfoIterator.hasNext()) {
151 if (yangFileInfo.getRootNode().getName().equals("flow-classifier")) {
152 break;
153 }
154 yangFileInfo = yangFileInfoIterator.next();
155 }
156
157 YangNode node = yangFileInfo.getRootNode();
158 node = node.getChild();
159 while (node != null) {
160 if (node instanceof YangGrouping) {
161 break;
162 }
163 node = node.getNextSibling();
164 }
165
166 node = node.getChild();
167 ListIterator<YangLeaf> leafIterator = ((YangContainer) node).getListOfLeaf().listIterator();
168 YangLeaf leafInfo = leafIterator.next();
169
170 assertThat(leafInfo.getName(), is("id"));
171 assertThat(leafInfo.getDataType().getDataTypeName(), is("flow-classifier-id"));
172 assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
173
174 leafInfo = leafIterator.next();
175
176 assertThat(leafInfo.getName(), is("tenant-id"));
177 assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
178
179 assertThat(true, is(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef()
180 .getName().equals("tenant-id")));
181
182 assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
183
184 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
185
186 // Check for the effective built-in type.
187 assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
188
189 YangPluginConfig yangPluginConfig = new YangPluginConfig();
190 yangPluginConfig.setCodeGenDir(TARGET);
191
Bharat saraswald50c6382016-07-14 21:57:13 +0530192 utilManager.translateToJava(yangPluginConfig);
Bharat saraswal246a70c2016-06-16 13:24:06 +0530193
194 testIfFlowClassifierFilesExists();
195 testIfPortPairFileDoesNotExist();
196 deleteDirectory(TARGET);
197 deleteTestSerFile();
198 }
199
200 /**
201 * Test if flow classifier code is generated.
202 */
203 private void testIfFlowClassifierFilesExists() {
204 File folder = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_FOLDER);
205 File file = new File(System.getProperty("user.dir") + SLASH + FLOW_CLASSIFIER_MANAGER);
206 assertThat(true, is(folder.exists()));
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530207 assertThat(false, is(file.exists()));
Bharat saraswal246a70c2016-06-16 13:24:06 +0530208 }
209
210 /**
211 * Tests if port pair code is not generated.
212 */
213 private void testIfPortPairFileDoesNotExist() {
214 File folder = new File(System.getProperty("user.dir") + SLASH + PORT_PAIR_FOLDER);
215 assertThat(false, is(folder.exists()));
216 }
217
218 /**
219 * Need to remove port-pair YANG file info from the set so , serialized file info can be
220 * tested.
221 *
222 * @param fileInfoSet YANG file info set
223 * @return updated file info set
224 */
225 private Set<YangFileInfo> removeFileInfoFromSet(Set<YangFileInfo> fileInfoSet) {
226 String portPairFile = System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + "portpair.yang";
227 for (YangFileInfo fileInfo : fileInfoSet) {
228 if (fileInfo.getYangFileName().equals(portPairFile)) {
229 fileInfoSet.remove(fileInfo);
230 return fileInfoSet;
231 }
232 }
233 return fileInfoSet;
234 }
235
236 /**
237 * Provides test jar files for linker.
238 *
239 * @throws IOException when fails to do IO operations
240 */
241 private void provideTestJarFile() throws IOException {
242
243 MavenProject project = new MavenProject();
244 serializeDataModel(TARGET, utilManager.getYangFileInfoSet(), project, false);
245 createTestJar();
246 }
247
248 /**
249 * Deletes serialized file.
250 */
251 private void deleteTestSerFile() {
252 File ser = new File(System.getProperty("user.dir") + SLASH + YANG_FILES_DIR + SER_FILE_NAME);
253 ser.delete();
254 }
255
256 /**
257 * Parses file info list and returns true if file info list contains the serialized file info.
258 *
259 * @param yangFileInfoIterator file info list iterator
260 * @return true if present
261 */
262 private boolean parseFileInfoSet(Iterator<YangFileInfo> yangFileInfoIterator) {
263 YangFileInfo yangFileInfo = yangFileInfoIterator.next();
264 while (yangFileInfoIterator.hasNext()) {
265 if (yangFileInfo.getRootNode().getName().equals("port-pair")) {
266 return true;
267 } else if (yangFileInfo.getRootNode().getName().equals("flow-classifier")) {
268 return true;
269 }
270 yangFileInfo = yangFileInfoIterator.next();
271 }
272 return false;
273
274 }
275
276 /**
277 * Returns list of test jar files.
278 *
279 * @param searchdir search directory
280 * @return list of test jar files
281 */
282 private List<String> getListOfTestJar(String searchdir) {
283 List<String> jarFiles = new ArrayList<>();
284
285 File directory = new File(searchdir + "/");
286 File[] files = directory.listFiles();
287
288 for (File file : files) {
289 if (!file.isDirectory()) {
290 jarFiles.add(file.toString());
291 }
292 }
293
294 return jarFiles;
295 }
296
297 /**
298 * Adds data model nodes of jar to file info set.
299 *
300 * @param jarFile jar file name
301 * @throws IOException when fails to do IO operations
302 */
303 private void addInterJarRootNodes(String jarFile) throws IOException {
304 try {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530305 List<YangNode> interJarResolvedNodes = parseJarFile(jarFile, TARGET);
Bharat saraswal246a70c2016-06-16 13:24:06 +0530306
307 for (YangNode node : interJarResolvedNodes) {
308 YangFileInfo dependentFileInfo = new YangFileInfo();
Bharat saraswald50c6382016-07-14 21:57:13 +0530309 node.setToTranslate(false);
Bharat saraswal246a70c2016-06-16 13:24:06 +0530310 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();
Bharat saraswal246a70c2016-06-16 13:24:06 +0530327 String[] source = new String[files.length];
328
329 for (int i = 0; i < files.length; i++) {
330 source[i] = files[i].toString();
331 }
332 byte[] buf = new byte[1024];
333
334 try {
335 String target = TARGET + JAR_FILE_NAME;
336 JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
337 for (String element : source) {
338 FileInputStream in = new FileInputStream(element);
339 out.putNextEntry(new JarEntry(element));
340 int len;
341 while ((len = in.read(buf)) > 0) {
342 out.write(buf, 0, len);
343 }
344 out.closeEntry();
345 in.close();
346 }
347 out.close();
348 } catch (IOException e) {
349 }
350 }
351
352}