blob: 40fed6ed5bd6292583ce245040f1a0ef2f9d334d [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
2 * Copyright 2016 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.translator.tojava;
18
19import java.io.IOException;
20
21import org.onosproject.yangutils.datamodel.YangNode;
22
23/**
24 * Implementation of Java code generator based on application schema.
25 */
26public final class JavaCodeGeneratorUtil {
27
28 /**
29 * Default constructor.
30 */
31 private JavaCodeGeneratorUtil() {
32 }
33
34 /**
35 * Generate Java code files corresponding to the YANG schema.
36 *
37 * @param rootNode root node of the data model tree
38 * @param codeGenDir code generation directory
39 * @throws IOException when fails to generate java code file the current
40 * node
41 */
42 public static void generateJavaCode(YangNode rootNode, String codeGenDir) throws IOException {
43 YangNode curNode = rootNode;
44 TraversalType curTraversal = TraversalType.ROOT;
45
46 while (!(curNode == null)) {
47 if (curTraversal != TraversalType.PARENT) {
48 generateCodeEntry(curNode, codeGenDir);
49 }
50 if (curTraversal != TraversalType.PARENT && curNode.getChild() != null) {
51 curTraversal = TraversalType.CHILD;
52 curNode = curNode.getChild();
53 } else if (curNode.getNextSibling() != null) {
54 generateCodeExit(curNode);
55 curTraversal = TraversalType.SIBILING;
56 curNode = curNode.getNextSibling();
57 } else {
58 generateCodeExit(curNode);
59 curTraversal = TraversalType.PARENT;
60 curNode = curNode.getParent();
61 }
62 }
63 }
64
65 /**
66 * Generates the current nodes code snippet.
67 *
68 * @param curNode current data model node for which the code needs to be
69 * generated
70 * @param codeGenDir the base directory where the code needs to be generated
71 * @throws IOException IO operation exception
72 */
73 private static void generateCodeEntry(YangNode curNode,
74 String codeGenDir) throws IOException {
75
76 if (curNode instanceof JavaCodeGenerator) {
77 ((JavaCodeGenerator) curNode).generateCodeEntry(codeGenDir);
78 } else {
79 throw new RuntimeException(
80 "Gnenerated data model node cannot be translated to target language code");
81 }
82 }
83
84 /**
85 * Generates the current nodes code target code from the snippet.
86 *
87 * @param curNode current data model node for which the code needs to be
88 * generated
89 * @throws IOException IO operation exception
90 */
91 private static void generateCodeExit(YangNode curNode) throws IOException {
92
93 if (curNode instanceof JavaCodeGenerator) {
94 ((JavaCodeGenerator) curNode).generateCodeExit();
95 } else {
96 throw new RuntimeException(
97 "Gnenerated data model node cannot be translated to target language code");
98 }
99 }
100}