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