blob: 4e8a60a3e7a0352cfb9b942b714596748e298b22 [file] [log] [blame]
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package org.onosproject.yangutils.linker.impl;
18
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053019import java.util.HashSet;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.Set;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053021import org.onosproject.yangutils.datamodel.ResolvableType;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053022import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053023import org.onosproject.yangutils.datamodel.YangReferenceResolver;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053024import org.onosproject.yangutils.datamodel.YangSubModule;
25import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
26import org.onosproject.yangutils.linker.YangLinker;
27import org.onosproject.yangutils.linker.exceptions.LinkerException;
28import org.onosproject.yangutils.plugin.manager.YangFileInfo;
29
30import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
31
32/**
33 * Representation of entity which provides linking service of YANG files.
34 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053035public class YangLinkerManager
36 implements YangLinker {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053037
38 /*
39 * Set of all the YANG nodes, corresponding to the YANG files parsed by
40 * parser.
41 */
42 Set<YangNode> yangNodeSet = new HashSet<>();
43
44 /**
45 * Returns set of YANG node.
46 *
47 * @return set of YANG node
48 */
49 public Set<YangNode> getYangNodeSet() {
50 return yangNodeSet;
51 }
52
53 /**
54 * Creates YANG nodes set.
55 *
56 * @param yangFileInfoSet YANG file information set
57 */
58 public void createYangNodeSet(Set<YangFileInfo> yangFileInfoSet) {
59 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
60 getYangNodeSet().add(yangFileInfo.getRootNode());
61 }
62 }
63
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053064 @Override
65 public void resolveDependencies(Set<YangFileInfo> yangFileInfoSet) {
66
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053067 // Create YANG node set.
68 createYangNodeSet(yangFileInfoSet);
69
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053070 // Carry out linking of sub module with module.
71 linkSubModulesToParentModule(yangFileInfoSet);
72
73 // Add references to import list.
74 addRefToYangFilesImportList(yangFileInfoSet);
75
76 // Add reference to include list.
77 addRefToYangFilesIncludeList(yangFileInfoSet);
78
79 // TODO check for circular import/include.
80
81 // Carry out inter-file linking.
82 processInterFileLinking(yangFileInfoSet);
83 }
84
85 /**
86 * Resolves sub-module linking by linking sub module with parent module.
87 *
88 * @param yangFileInfoSet set of YANG files info
89 * @throws LinkerException fails to link sub-module to parent module
90 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053091 public void linkSubModulesToParentModule(Set<YangFileInfo> yangFileInfoSet)
92 throws LinkerException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053093 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
94 YangNode yangNode = yangFileInfo.getRootNode();
95 if (yangNode instanceof YangSubModule) {
96 try {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053097 ((YangSubModule) yangNode).linkWithModule(getYangNodeSet());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053098 } catch (DataModelException e) {
99 String errorInfo = "YANG file error: " + yangFileInfo.getYangFileName() + " at line: "
100 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
101 + e.getMessage();
102 throw new LinkerException(errorInfo);
103 }
104 }
105 }
106 }
107
108 /**
109 * Adds imported node information to the import list.
110 *
111 * @param yangFileInfoSet set of YANG files info
112 * @throws LinkerException fails to find imported module
113 */
114 public void addRefToYangFilesImportList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
115 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
116 YangNode yangNode = yangFileInfo.getRootNode();
117 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530118 try {
119 ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet());
120 } catch (DataModelException e) {
121 String errorInfo = "Error in file: " + yangFileInfo.getYangFileName() + " at line: "
122 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
123 + e.getMessage();
124 throw new LinkerException(errorInfo);
125 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530126 }
127 }
128 }
129
130 /**
131 * Adds included node information to the include list.
132 *
133 * @param yangFileInfoSet set of YANG files info
134 * @throws LinkerException fails to find included sub-module
135 */
136 public void addRefToYangFilesIncludeList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException {
137 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
138 YangNode yangNode = yangFileInfo.getRootNode();
139 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530140 try {
141 ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet());
142 } catch (DataModelException e) {
143 String errorInfo = "Error in file: " + yangFileInfo.getYangFileName() + " at line: "
144 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
145 + e.getMessage();
146 throw new LinkerException(errorInfo);
147 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530148 }
149 }
150 }
151
152 /**
153 * Processes inter file linking for type and uses.
154 *
155 * @param yangFileInfoSet set of YANG files info
156 * @throws LinkerException a violation in linker execution
157 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530158 public void processInterFileLinking(Set<YangFileInfo> yangFileInfoSet)
159 throws LinkerException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530160 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
161 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 ((YangReferenceResolver) yangFileInfo.getRootNode()).resolveInterFileLinking(ResolvableType.YANG_USES);
163 ((YangReferenceResolver) yangFileInfo.getRootNode())
164 .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530165 } catch (DataModelException e) {
166 String errorInfo = "Error in file: " + yangFileInfo.getYangFileName() + " at line: "
167 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage();
168 throw new LinkerException(errorInfo);
169 }
170 }
171 }
172}