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; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 28 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 29 | import org.onosproject.yangutils.linker.YangLinker; |
| 30 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 31 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.updateFilePriority; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 34 | |
| 35 | /** |
| 36 | * Representation of entity which provides linking service of YANG files. |
| 37 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 38 | public class YangLinkerManager |
| 39 | implements YangLinker { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 40 | |
| 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 Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 59 | * @param yangNodeSet YANG node information set |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 60 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 61 | public void createYangNodeSet(Set<YangNode> yangNodeSet) { |
| 62 | getYangNodeSet().addAll(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 65 | @Override |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 66 | public void resolveDependencies(Set<YangNode> yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 67 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 68 | // Create YANG node set. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 69 | createYangNodeSet(yangNodeSet); |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 70 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 71 | // Carry out linking of sub module with module. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 72 | linkSubModulesToParentModule(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 73 | |
| 74 | // Add references to import list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 75 | addRefToYangFilesImportList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 76 | |
| 77 | // Add reference to include list. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 78 | addRefToYangFilesIncludeList(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 79 | |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame] | 80 | // Update the priority for all the files. |
| 81 | updateFilePriority(yangNodeSet); |
| 82 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 83 | // TODO check for circular import/include. |
| 84 | |
| 85 | // Carry out inter-file linking. |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 86 | processInterFileLinking(yangNodeSet); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Resolves sub-module linking by linking sub module with parent module. |
| 91 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 92 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 93 | * @throws LinkerException fails to link sub-module to parent module |
| 94 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 95 | public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 96 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 97 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 98 | if (yangNode instanceof YangSubModule) { |
| 99 | try { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 100 | ((YangSubModule) yangNode).linkWithModule(getYangNodeSet()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 101 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 102 | String errorInfo = "YANG file error: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 103 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 104 | + e.getMessage(); |
| 105 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 106 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adds imported node information to the import list. |
| 114 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 115 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 116 | * @throws LinkerException fails to find imported module |
| 117 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 118 | public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet) |
| 119 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 120 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 121 | if (yangNode instanceof YangReferenceResolver) { |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 122 | try { |
| 123 | ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet()); |
| 124 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 125 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 126 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 127 | + e.getMessage(); |
| 128 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 129 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 130 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds included node information to the include list. |
| 137 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 138 | * @param yangNodeSet set of YANG files info |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 139 | * @throws LinkerException fails to find included sub-module |
| 140 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 141 | public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet) |
| 142 | throws LinkerException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 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) |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 176 | .resolveInterFileLinking(ResolvableType.YANG_AUGMENT); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 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) |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 180 | .resolveInterFileLinking(ResolvableType.YANG_BASE); |
| 181 | ((YangReferenceResolver) yangNode) |
| 182 | .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 183 | ((YangReferenceResolver) yangNode) |
| 184 | .resolveInterFileLinking(ResolvableType.YANG_LEAFREF); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 185 | ((YangReferenceResolver) yangNode) |
| 186 | .resolveInterFileLinking(ResolvableType.YANG_COMPILER_ANNOTATION); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 187 | } catch (DataModelException e) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 188 | String errorInfo = "Error in file: " + yangNode.getName() + " at line: " |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 189 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage(); |
| 190 | throw new LinkerException(errorInfo); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 191 | // TODO add file path in exception message in util manager. |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame^] | 192 | } 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 Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
Vidyashree Rama | 35e989c | 2016-07-12 20:52:48 +0530 | [diff] [blame] | 200 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 201 | } |