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; |
| 28 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
| 29 | |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 31 | |
| 32 | /** |
| 33 | * Representation of entity which provides linking service of YANG files. |
| 34 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 35 | public class YangLinkerManager |
| 36 | implements YangLinker { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 37 | |
| 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 Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 64 | @Override |
| 65 | public void resolveDependencies(Set<YangFileInfo> yangFileInfoSet) { |
| 66 | |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 67 | // Create YANG node set. |
| 68 | createYangNodeSet(yangFileInfoSet); |
| 69 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 70 | // 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-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 91 | public void linkSubModulesToParentModule(Set<YangFileInfo> yangFileInfoSet) |
| 92 | throws LinkerException { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 93 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 94 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 95 | if (yangNode instanceof YangSubModule) { |
| 96 | try { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 97 | ((YangSubModule) yangNode).linkWithModule(getYangNodeSet()); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 98 | } 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 Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 118 | 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 Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 126 | } |
| 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 Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 140 | 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 Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 148 | } |
| 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-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 158 | public void processInterFileLinking(Set<YangFileInfo> yangFileInfoSet) |
| 159 | throws LinkerException { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 160 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 161 | try { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 162 | ((YangReferenceResolver) yangFileInfo.getRootNode()).resolveInterFileLinking(ResolvableType.YANG_USES); |
| 163 | ((YangReferenceResolver) yangFileInfo.getRootNode()) |
| 164 | .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 165 | } 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 | } |