Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.linker.impl; |
| 18 | |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 19 | import java.util.Collections; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 20 | import java.util.HashSet; |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 21 | import java.util.Iterator; |
| 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 24 | import java.util.Set; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 26 | import org.onosproject.yangutils.datamodel.YangImport; |
| 27 | import org.onosproject.yangutils.datamodel.YangInclude; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 31 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 32 | import org.onosproject.yangutils.linker.YangLinker; |
| 33 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 34 | |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 36 | |
| 37 | /** |
| 38 | * Representation of entity which provides linking service of YANG files. |
| 39 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 40 | public class YangLinkerManager |
| 41 | implements YangLinker { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Set of all the YANG nodes, corresponding to the YANG files parsed by |
| 45 | * parser. |
| 46 | */ |
| 47 | Set<YangNode> yangNodeSet = new HashSet<>(); |
| 48 | |
| 49 | /** |
| 50 | * Returns set of YANG node. |
| 51 | * |
| 52 | * @return set of YANG node |
| 53 | */ |
| 54 | public Set<YangNode> getYangNodeSet() { |
| 55 | return yangNodeSet; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates YANG nodes set. |
| 60 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 61 | * @param yangNodeSet YANG node information set |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 62 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 63 | public void createYangNodeSet(Set<YangNode> yangNodeSet) { |
| 64 | getYangNodeSet().addAll(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 65 | } |
| 66 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 67 | @Override |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 68 | public void resolveDependencies(Set<YangNode> yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 69 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 70 | // Create YANG node set. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 71 | createYangNodeSet(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 72 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 73 | // Carry out linking of sub module with module. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 74 | linkSubModulesToParentModule(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 75 | |
| 76 | // Add references to import list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 77 | addRefToYangFilesImportList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 78 | |
| 79 | // Add reference to include list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 80 | addRefToYangFilesIncludeList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 81 | |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 82 | // Update the priority for all the files. |
| 83 | updateFilePriority(yangNodeSet); |
| 84 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 85 | // TODO check for circular import/include. |
| 86 | |
| 87 | // Carry out inter-file linking. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 88 | processInterFileLinking(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Resolves sub-module linking by linking sub module with parent module. |
| 93 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 94 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 95 | * @throws LinkerException fails to link sub-module to parent module |
| 96 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 97 | public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 98 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 99 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 100 | if (yangNode instanceof YangSubModule) { |
| 101 | try { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 102 | ((YangSubModule) yangNode).linkWithModule(getYangNodeSet()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 103 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 104 | String errorInfo = "YANG file error: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 105 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 106 | + e.getMessage(); |
| 107 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 108 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Adds imported node information to the import list. |
| 116 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 117 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 118 | * @throws LinkerException fails to find imported module |
| 119 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 120 | public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet) throws LinkerException { |
| 121 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 122 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 123 | try { |
| 124 | ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet()); |
| 125 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 126 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 127 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 128 | + e.getMessage(); |
| 129 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 130 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 131 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds included node information to the include list. |
| 138 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 139 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 140 | * @throws LinkerException fails to find included sub-module |
| 141 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 142 | public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet) throws LinkerException { |
| 143 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 144 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 145 | try { |
| 146 | ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet()); |
| 147 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 148 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 149 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 150 | + e.getMessage(); |
| 151 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 152 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 153 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Processes inter file linking for type and uses. |
| 160 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 161 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 162 | * @throws LinkerException a violation in linker execution |
| 163 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 164 | public void processInterFileLinking(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 165 | throws LinkerException { |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 166 | List<YangNode> yangNodeSortedList = new LinkedList<>(); |
| 167 | yangNodeSortedList.addAll(yangNodeSet); |
| 168 | Collections.sort(yangNodeSortedList); |
| 169 | for (YangNode yangNode : yangNodeSortedList) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 170 | try { |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 171 | ((YangReferenceResolver) yangNode) |
| 172 | .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 173 | ((YangReferenceResolver) yangNode) |
| 174 | .resolveInterFileLinking(ResolvableType.YANG_USES); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 175 | ((YangReferenceResolver) yangNode) |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 176 | .resolveInterFileLinking(ResolvableType.YANG_AUGMENT); |
| 177 | ((YangReferenceResolver) yangNode) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 178 | .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 179 | ((YangReferenceResolver) yangNode) |
| 180 | .resolveInterFileLinking(ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 181 | ((YangReferenceResolver) yangNode) |
| 182 | .resolveInterFileLinking(ResolvableType.YANG_BASE); |
| 183 | ((YangReferenceResolver) yangNode) |
| 184 | .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 185 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 186 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 187 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage(); |
| 188 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 189 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame^] | 193 | |
| 194 | /** |
| 195 | * Updates the priority for all the input files. |
| 196 | * |
| 197 | * @param yangNodeSet set of YANG files info |
| 198 | */ |
| 199 | public void updateFilePriority(Set<YangNode> yangNodeSet) { |
| 200 | for (YangNode yangNode : yangNodeSet) { |
| 201 | updateFilePriorityOfNode(yangNode); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Updates priority of the node. |
| 207 | * |
| 208 | * @param yangNode YANG node information |
| 209 | */ |
| 210 | public void updateFilePriorityOfNode(YangNode yangNode) { |
| 211 | int curNodePriority = yangNode.getPriority(); |
| 212 | if (yangNode instanceof YangReferenceResolver) { |
| 213 | List<YangImport> yangImportList = ((YangReferenceResolver) yangNode).getImportList(); |
| 214 | if (yangImportList != null && !yangImportList.isEmpty()) { |
| 215 | Iterator<YangImport> importInfoIterator = yangImportList.iterator(); |
| 216 | // Run through the imported list to update priority. |
| 217 | while (importInfoIterator.hasNext()) { |
| 218 | YangImport yangImport = importInfoIterator.next(); |
| 219 | YangNode importedNode = yangImport.getImportedNode(); |
| 220 | if (curNodePriority >= importedNode.getPriority()) { |
| 221 | importedNode.setPriority(curNodePriority + 1); |
| 222 | updateFilePriorityOfNode(importedNode); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | List<YangInclude> yangIncludeList = ((YangReferenceResolver) yangNode).getIncludeList(); |
| 228 | if (yangIncludeList != null && !yangIncludeList.isEmpty()) { |
| 229 | Iterator<YangInclude> includeInfoIterator = yangIncludeList.iterator(); |
| 230 | // Run through the imported list to update priority. |
| 231 | while (includeInfoIterator.hasNext()) { |
| 232 | YangInclude yangInclude = includeInfoIterator.next(); |
| 233 | YangNode includedNode = yangInclude.getIncludedNode(); |
| 234 | if (curNodePriority >= includedNode.getPriority()) { |
| 235 | includedNode.setPriority(curNodePriority + 1); |
| 236 | updateFilePriorityOfNode(includedNode); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 242 | } |