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.LinkedList; |
| 22 | import java.util.List; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 23 | import java.util.Set; |
VinodKumarS-Huawei | 3b4cce0 | 2016-08-20 21:32:17 +0530 | [diff] [blame] | 24 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 29 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 30 | import org.onosproject.yangutils.linker.YangLinker; |
| 31 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 32 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.updateFilePriority; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 35 | |
| 36 | /** |
| 37 | * Representation of entity which provides linking service of YANG files. |
| 38 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 39 | public class YangLinkerManager |
| 40 | implements YangLinker { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Set of all the YANG nodes, corresponding to the YANG files parsed by |
| 44 | * parser. |
| 45 | */ |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 46 | private Set<YangNode> yangNodeSet = new HashSet<>(); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Returns set of YANG node. |
| 50 | * |
| 51 | * @return set of YANG node |
| 52 | */ |
| 53 | public Set<YangNode> getYangNodeSet() { |
| 54 | return yangNodeSet; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates YANG nodes set. |
| 59 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 60 | * @param yangNodeSet YANG node information set |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 61 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 62 | public void createYangNodeSet(Set<YangNode> yangNodeSet) { |
| 63 | getYangNodeSet().addAll(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 64 | } |
| 65 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 66 | @Override |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 67 | public void resolveDependencies(Set<YangNode> yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 68 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 69 | // Create YANG node set. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 70 | createYangNodeSet(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 71 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 72 | // Carry out linking of sub module with module. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 73 | linkSubModulesToParentModule(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 74 | |
| 75 | // Add references to import list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 76 | addRefToYangFilesImportList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 77 | |
| 78 | // Add reference to include list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 79 | addRefToYangFilesIncludeList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 80 | |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame] | 81 | // Update the priority for all the files. |
| 82 | updateFilePriority(yangNodeSet); |
| 83 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 84 | // TODO check for circular import/include. |
| 85 | |
| 86 | // Carry out inter-file linking. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 87 | processInterFileLinking(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Resolves sub-module linking by linking sub module with parent module. |
| 92 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 93 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 94 | * @throws LinkerException fails to link sub-module to parent module |
| 95 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 96 | public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 97 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 98 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 99 | if (yangNode instanceof YangSubModule) { |
| 100 | try { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 101 | ((YangSubModule) yangNode).linkWithModule(getYangNodeSet()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 102 | } catch (DataModelException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 103 | String errorInfo = "Error in file: " + yangNode.getName() + " in " + |
| 104 | yangNode.getFileName() + " at " + |
| 105 | "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 106 | + e.getLocalizedMessage(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 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 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 120 | public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet) |
| 121 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 122 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 123 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 124 | try { |
| 125 | ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet()); |
| 126 | } catch (DataModelException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 127 | String errorInfo = "Error in file: " + yangNode.getName() + " in " + |
| 128 | yangNode.getFileName() + " at " + |
| 129 | "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 130 | + e.getLocalizedMessage(); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 131 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 132 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 133 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adds included node information to the include list. |
| 140 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 141 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 142 | * @throws LinkerException fails to find included sub-module |
| 143 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 144 | public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet) |
| 145 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 146 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 147 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 148 | try { |
| 149 | ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet()); |
| 150 | } catch (DataModelException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 151 | String errorInfo = "Error in file: " + yangNode.getName() + " in " + |
| 152 | yangNode.getFileName() + " at " + |
| 153 | "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 154 | + e.getLocalizedMessage(); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 155 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 156 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 157 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Processes inter file linking for type and uses. |
| 164 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 165 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 166 | * @throws LinkerException a violation in linker execution |
| 167 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 168 | public void processInterFileLinking(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 169 | throws LinkerException { |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame] | 170 | List<YangNode> yangNodeSortedList = new LinkedList<>(); |
| 171 | yangNodeSortedList.addAll(yangNodeSet); |
| 172 | Collections.sort(yangNodeSortedList); |
| 173 | for (YangNode yangNode : yangNodeSortedList) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 174 | try { |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 175 | ((YangReferenceResolver) yangNode) |
| 176 | .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 177 | ((YangReferenceResolver) yangNode) |
| 178 | .resolveInterFileLinking(ResolvableType.YANG_USES); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 179 | ((YangReferenceResolver) yangNode) |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 180 | .resolveInterFileLinking(ResolvableType.YANG_AUGMENT); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 181 | ((YangReferenceResolver) yangNode) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 182 | .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 183 | ((YangReferenceResolver) yangNode) |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 184 | .resolveInterFileLinking(ResolvableType.YANG_BASE); |
| 185 | ((YangReferenceResolver) yangNode) |
| 186 | .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 187 | ((YangReferenceResolver) yangNode) |
| 188 | .resolveInterFileLinking(ResolvableType.YANG_LEAFREF); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 189 | ((YangReferenceResolver) yangNode) |
| 190 | .resolveInterFileLinking(ResolvableType.YANG_COMPILER_ANNOTATION); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 191 | } catch (DataModelException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 192 | String errorInfo = "Error in file: " + yangNode.getName() + " in " + |
| 193 | yangNode.getFileName() + " at " + |
| 194 | "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 195 | + e.getLocalizedMessage(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 196 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 197 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame] | 198 | } catch (LinkerException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 199 | String errorInfo = "Error in file: " + yangNode.getName() + " in " + |
| 200 | yangNode.getFileName() + " at " + |
| 201 | "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 202 | + e.getLocalizedMessage(); |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame] | 203 | throw new LinkerException(errorInfo); |
| 204 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame] | 208 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 209 | } |