Gaurav Agrawal | 0d43bb5 | 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 | |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 19 | import java.util.HashSet; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 20 | import java.util.Set; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 25 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 26 | import org.onosproject.yangutils.linker.YangLinker; |
| 27 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 28 | |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 30 | |
| 31 | /** |
| 32 | * Representation of entity which provides linking service of YANG files. |
| 33 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 34 | public class YangLinkerManager |
| 35 | implements YangLinker { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Set of all the YANG nodes, corresponding to the YANG files parsed by |
| 39 | * parser. |
| 40 | */ |
| 41 | Set<YangNode> yangNodeSet = new HashSet<>(); |
| 42 | |
| 43 | /** |
| 44 | * Returns set of YANG node. |
| 45 | * |
| 46 | * @return set of YANG node |
| 47 | */ |
| 48 | public Set<YangNode> getYangNodeSet() { |
| 49 | return yangNodeSet; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates YANG nodes set. |
| 54 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 55 | * @param yangNodeSet YANG node information set |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 56 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 57 | public void createYangNodeSet(Set<YangNode> yangNodeSet) { |
| 58 | getYangNodeSet().addAll(yangNodeSet); |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 59 | } |
| 60 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 61 | @Override |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 62 | public void resolveDependencies(Set<YangNode> yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 63 | |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 64 | // Create YANG node set. |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 65 | createYangNodeSet(yangNodeSet); |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 66 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 67 | // Carry out linking of sub module with module. |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 68 | linkSubModulesToParentModule(yangNodeSet); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 69 | |
| 70 | // Add references to import list. |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 71 | addRefToYangFilesImportList(yangNodeSet); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 72 | |
| 73 | // Add reference to include list. |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 74 | addRefToYangFilesIncludeList(yangNodeSet); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 75 | |
| 76 | // TODO check for circular import/include. |
| 77 | |
| 78 | // Carry out inter-file linking. |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 79 | processInterFileLinking(yangNodeSet); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Resolves sub-module linking by linking sub module with parent module. |
| 84 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 85 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 86 | * @throws LinkerException fails to link sub-module to parent module |
| 87 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 88 | public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 89 | throws LinkerException { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 90 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 91 | if (yangNode instanceof YangSubModule) { |
| 92 | try { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 93 | ((YangSubModule) yangNode).linkWithModule(getYangNodeSet()); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 94 | } catch (DataModelException e) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 95 | String errorInfo = "YANG file error: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 96 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 97 | + e.getMessage(); |
| 98 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 99 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Adds imported node information to the import list. |
| 107 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 108 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 109 | * @throws LinkerException fails to find imported module |
| 110 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 111 | public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet) throws LinkerException { |
| 112 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 113 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 114 | try { |
| 115 | ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet()); |
| 116 | } catch (DataModelException e) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 117 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 118 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 119 | + e.getMessage(); |
| 120 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 121 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 122 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds included node information to the include list. |
| 129 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 130 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 131 | * @throws LinkerException fails to find included sub-module |
| 132 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 133 | public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet) throws LinkerException { |
| 134 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 135 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 136 | try { |
| 137 | ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet()); |
| 138 | } catch (DataModelException e) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 139 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 140 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 141 | + e.getMessage(); |
| 142 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 143 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 144 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Processes inter file linking for type and uses. |
| 151 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 152 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 153 | * @throws LinkerException a violation in linker execution |
| 154 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 155 | public void processInterFileLinking(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 156 | throws LinkerException { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 157 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 158 | try { |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 159 | ((YangReferenceResolver) yangNode) |
| 160 | .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 161 | ((YangReferenceResolver) yangNode) |
| 162 | .resolveInterFileLinking(ResolvableType.YANG_USES); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 163 | ((YangReferenceResolver) yangNode) |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 164 | .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 165 | ((YangReferenceResolver) yangNode) |
| 166 | .resolveInterFileLinking(ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 167 | ((YangReferenceResolver) yangNode) |
| 168 | .resolveInterFileLinking(ResolvableType.YANG_BASE); |
| 169 | ((YangReferenceResolver) yangNode) |
| 170 | .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 171 | } catch (DataModelException e) { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 172 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 173 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage(); |
| 174 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 175 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |