blob: 74dec9d68242a8aad354ac8e0f1d7db8dc92da2a [file] [log] [blame]
Gaurav Agrawalab7c4bd2016-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
Vidyashree Rama35e989c2016-07-12 20:52:48 +053019import java.util.Collections;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053020import java.util.HashSet;
Vidyashree Rama35e989c2016-07-12 20:52:48 +053021import java.util.LinkedList;
22import java.util.List;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053023import java.util.Set;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053024import org.onosproject.yangutils.datamodel.ResolvableType;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053025import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053026import org.onosproject.yangutils.datamodel.YangReferenceResolver;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053027import org.onosproject.yangutils.datamodel.YangSubModule;
28import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
29import org.onosproject.yangutils.linker.YangLinker;
30import org.onosproject.yangutils.linker.exceptions.LinkerException;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053031
Bharat saraswal039f59c2016-07-14 21:57:13 +053032import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.updateFilePriority;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053033import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
34
35/**
36 * Representation of entity which provides linking service of YANG files.
37 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053038public class YangLinkerManager
39 implements YangLinker {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053040
41 /*
42 * Set of all the YANG nodes, corresponding to the YANG files parsed by
43 * parser.
44 */
45 Set<YangNode> yangNodeSet = new HashSet<>();
46
47 /**
48 * Returns set of YANG node.
49 *
50 * @return set of YANG node
51 */
52 public Set<YangNode> getYangNodeSet() {
53 return yangNodeSet;
54 }
55
56 /**
57 * Creates YANG nodes set.
58 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053059 * @param yangNodeSet YANG node information set
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053060 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053061 public void createYangNodeSet(Set<YangNode> yangNodeSet) {
62 getYangNodeSet().addAll(yangNodeSet);
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053063 }
64
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053065 @Override
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053066 public void resolveDependencies(Set<YangNode> yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053067
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053068 // Create YANG node set.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053069 createYangNodeSet(yangNodeSet);
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053070
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053071 // Carry out linking of sub module with module.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053072 linkSubModulesToParentModule(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053073
74 // Add references to import list.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053075 addRefToYangFilesImportList(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053076
77 // Add reference to include list.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053078 addRefToYangFilesIncludeList(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053079
Vidyashree Rama35e989c2016-07-12 20:52:48 +053080 // Update the priority for all the files.
81 updateFilePriority(yangNodeSet);
82
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053083 // TODO check for circular import/include.
84
85 // Carry out inter-file linking.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053086 processInterFileLinking(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053087 }
88
89 /**
90 * Resolves sub-module linking by linking sub module with parent module.
91 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053092 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053093 * @throws LinkerException fails to link sub-module to parent module
94 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053095 public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053096 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053097 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053098 if (yangNode instanceof YangSubModule) {
99 try {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530100 ((YangSubModule) yangNode).linkWithModule(getYangNodeSet());
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530101 } catch (DataModelException e) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530102 String errorInfo = "YANG file error: " + yangNode.getName() + " at line: "
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530103 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
104 + e.getMessage();
105 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530106 // TODO add file path in exception message in util manager.
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530107 }
108 }
109 }
110 }
111
112 /**
113 * Adds imported node information to the import list.
114 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530115 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530116 * @throws LinkerException fails to find imported module
117 */
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530118 public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet)
119 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530120 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530121 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530122 try {
123 ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet());
124 } catch (DataModelException e) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530125 String errorInfo = "Error in file: " + yangNode.getName() + " at line: "
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530126 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
127 + e.getMessage();
128 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530129 // TODO add file path in exception message in util manager.
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530130 }
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530131 }
132 }
133 }
134
135 /**
136 * Adds included node information to the include list.
137 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530138 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530139 * @throws LinkerException fails to find included sub-module
140 */
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530141 public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet)
142 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530143 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530144 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530145 try {
146 ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet());
147 } catch (DataModelException e) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530148 String errorInfo = "Error in file: " + yangNode.getName() + " at line: "
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530149 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
150 + e.getMessage();
151 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530152 // TODO add file path in exception message in util manager.
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530153 }
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530154 }
155 }
156 }
157
158 /**
159 * Processes inter file linking for type and uses.
160 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530161 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530162 * @throws LinkerException a violation in linker execution
163 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530164 public void processInterFileLinking(Set<YangNode> yangNodeSet)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530165 throws LinkerException {
Vidyashree Rama35e989c2016-07-12 20:52:48 +0530166 List<YangNode> yangNodeSortedList = new LinkedList<>();
167 yangNodeSortedList.addAll(yangNodeSet);
168 Collections.sort(yangNodeSortedList);
169 for (YangNode yangNode : yangNodeSortedList) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530170 try {
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530171 ((YangReferenceResolver) yangNode)
172 .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE);
Shankara-Huawei234cd092016-07-14 11:35:34 +0530173 ((YangReferenceResolver) yangNode)
174 .resolveInterFileLinking(ResolvableType.YANG_USES);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530175 ((YangReferenceResolver) yangNode)
Vidyashree Rama918f1622016-07-28 17:33:15 +0530176 .resolveInterFileLinking(ResolvableType.YANG_AUGMENT);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530177 ((YangReferenceResolver) yangNode)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530178 .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
janani b0e4e8ae2016-07-13 21:06:41 +0530179 ((YangReferenceResolver) yangNode)
Shankara-Huawei234cd092016-07-14 11:35:34 +0530180 .resolveInterFileLinking(ResolvableType.YANG_BASE);
181 ((YangReferenceResolver) yangNode)
182 .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF);
janani bebb143d2016-07-14 19:35:22 +0530183 ((YangReferenceResolver) yangNode)
184 .resolveInterFileLinking(ResolvableType.YANG_LEAFREF);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530185 ((YangReferenceResolver) yangNode)
186 .resolveInterFileLinking(ResolvableType.YANG_COMPILER_ANNOTATION);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530187 } catch (DataModelException e) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530188 String errorInfo = "Error in file: " + yangNode.getName() + " at line: "
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530189 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage();
190 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530191 // TODO add file path in exception message in util manager.
Gaurav Agrawald39a9212016-08-20 18:51:20 +0530192 } catch (LinkerException e) {
193 String errorInfo = "Error in file: " + yangNode.getName() + " at line: "
194 + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage();
195 throw new LinkerException(errorInfo);
196 // TODO add file path in exception message in util manager.
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530197 }
198 }
199 }
Vidyashree Rama35e989c2016-07-12 20:52:48 +0530200
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530201}