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 | |
| 19 | import java.util.Set; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 20 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.linker.ResolvableType; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.linker.YangLinker; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.linker.YangReferenceResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 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 | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 35 | public class YangLinkerManager |
| 36 | implements YangLinker { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 37 | @Override |
| 38 | public void resolveDependencies(Set<YangFileInfo> yangFileInfoSet) { |
| 39 | |
| 40 | // Carry out linking of sub module with module. |
| 41 | linkSubModulesToParentModule(yangFileInfoSet); |
| 42 | |
| 43 | // Add references to import list. |
| 44 | addRefToYangFilesImportList(yangFileInfoSet); |
| 45 | |
| 46 | // Add reference to include list. |
| 47 | addRefToYangFilesIncludeList(yangFileInfoSet); |
| 48 | |
| 49 | // TODO check for circular import/include. |
| 50 | |
| 51 | // Carry out inter-file linking. |
| 52 | processInterFileLinking(yangFileInfoSet); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Resolves sub-module linking by linking sub module with parent module. |
| 57 | * |
| 58 | * @param yangFileInfoSet set of YANG files info |
| 59 | * @throws LinkerException fails to link sub-module to parent module |
| 60 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 61 | public void linkSubModulesToParentModule(Set<YangFileInfo> yangFileInfoSet) |
| 62 | throws LinkerException { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 63 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 64 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 65 | if (yangNode instanceof YangSubModule) { |
| 66 | try { |
| 67 | ((YangSubModule) yangNode).linkWithModule(yangFileInfoSet); |
| 68 | } catch (DataModelException e) { |
| 69 | String errorInfo = "YANG file error: " + yangFileInfo.getYangFileName() + " at line: " |
| 70 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE |
| 71 | + e.getMessage(); |
| 72 | throw new LinkerException(errorInfo); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds imported node information to the import list. |
| 80 | * |
| 81 | * @param yangFileInfoSet set of YANG files info |
| 82 | * @throws LinkerException fails to find imported module |
| 83 | */ |
| 84 | public void addRefToYangFilesImportList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException { |
| 85 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 86 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 87 | if (yangNode instanceof YangReferenceResolver) { |
| 88 | ((YangReferenceResolver) yangNode).addReferencesToImportList(yangFileInfoSet); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Adds included node information to the include list. |
| 95 | * |
| 96 | * @param yangFileInfoSet set of YANG files info |
| 97 | * @throws LinkerException fails to find included sub-module |
| 98 | */ |
| 99 | public void addRefToYangFilesIncludeList(Set<YangFileInfo> yangFileInfoSet) throws LinkerException { |
| 100 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 101 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 102 | if (yangNode instanceof YangReferenceResolver) { |
| 103 | ((YangReferenceResolver) yangNode).addReferencesToIncludeList(yangFileInfoSet); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Processes inter file linking for type and uses. |
| 110 | * |
| 111 | * @param yangFileInfoSet set of YANG files info |
| 112 | * @throws LinkerException a violation in linker execution |
| 113 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 114 | public void processInterFileLinking(Set<YangFileInfo> yangFileInfoSet) |
| 115 | throws LinkerException { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 116 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 117 | try { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 118 | ((YangReferenceResolver) yangFileInfo.getRootNode()).resolveInterFileLinking(ResolvableType.YANG_USES); |
| 119 | ((YangReferenceResolver) yangFileInfo.getRootNode()) |
| 120 | .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 121 | } catch (DataModelException e) { |
| 122 | String errorInfo = "Error in file: " + yangFileInfo.getYangFileName() + " at line: " |
| 123 | + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE + e.getMessage(); |
| 124 | throw new LinkerException(errorInfo); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |