blob: deaebb2a99897d48f3b465c1bf511e089a87baaa [file] [log] [blame]
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +05301/*
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
17package org.onosproject.yangutils.linker.impl;
18
19import java.util.Set;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053020
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053021import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangSubModule;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053024import org.onosproject.yangutils.linker.ResolvableType;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053025import org.onosproject.yangutils.linker.YangLinker;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053026import org.onosproject.yangutils.linker.YangReferenceResolver;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053027import org.onosproject.yangutils.linker.exceptions.LinkerException;
28import org.onosproject.yangutils.plugin.manager.YangFileInfo;
29
30import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
31
32/**
33 * Representation of entity which provides linking service of YANG files.
34 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053035public class YangLinkerManager
36 implements YangLinker {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053037 @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-Huawei2ee9e7e2016-06-01 14:30:22 +053061 public void linkSubModulesToParentModule(Set<YangFileInfo> yangFileInfoSet)
62 throws LinkerException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053063 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530114 public void processInterFileLinking(Set<YangFileInfo> yangFileInfoSet)
115 throws LinkerException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530116 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
117 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530118 ((YangReferenceResolver) yangFileInfo.getRootNode()).resolveInterFileLinking(ResolvableType.YANG_USES);
119 ((YangReferenceResolver) yangFileInfo.getRootNode())
120 .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530121 } 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}