blob: daf035e6786a3de59c7be79c708cf16105896b7a [file] [log] [blame]
Gaurav Agrawalab7c4bd2016-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
Vidyashree Rama35e989c2016-07-12 20:52:48 +053019import java.util.Collections;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053020import java.util.HashSet;
Vidyashree Rama35e989c2016-07-12 20:52:48 +053021import java.util.LinkedList;
22import java.util.List;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053023import java.util.Set;
VinodKumarS-Huawei3b4cce02016-08-20 21:32:17 +053024
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053025import org.onosproject.yangutils.datamodel.ResolvableType;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053026import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053027import org.onosproject.yangutils.datamodel.YangReferenceResolver;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053028import org.onosproject.yangutils.datamodel.YangSubModule;
29import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
30import org.onosproject.yangutils.linker.YangLinker;
31import org.onosproject.yangutils.linker.exceptions.LinkerException;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053032
Bharat saraswal039f59c2016-07-14 21:57:13 +053033import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.updateFilePriority;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053034import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
35
36/**
37 * Representation of entity which provides linking service of YANG files.
38 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053039public class YangLinkerManager
40 implements YangLinker {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053041
42 /*
43 * Set of all the YANG nodes, corresponding to the YANG files parsed by
44 * parser.
45 */
Bharat saraswal2da23bf2016-08-25 15:28:39 +053046 private Set<YangNode> yangNodeSet = new HashSet<>();
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053047
48 /**
49 * Returns set of YANG node.
50 *
51 * @return set of YANG node
52 */
53 public Set<YangNode> getYangNodeSet() {
54 return yangNodeSet;
55 }
56
57 /**
58 * Creates YANG nodes set.
59 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053060 * @param yangNodeSet YANG node information set
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053061 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053062 public void createYangNodeSet(Set<YangNode> yangNodeSet) {
63 getYangNodeSet().addAll(yangNodeSet);
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053064 }
65
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053066 @Override
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053067 public void resolveDependencies(Set<YangNode> yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053068
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053069 // Create YANG node set.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053070 createYangNodeSet(yangNodeSet);
Gaurav Agrawal58b348e2016-06-07 14:00:26 +053071
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053072 // Carry out linking of sub module with module.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053073 linkSubModulesToParentModule(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053074
75 // Add references to import list.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053076 addRefToYangFilesImportList(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053077
78 // Add reference to include list.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053079 addRefToYangFilesIncludeList(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053080
Vidyashree Rama35e989c2016-07-12 20:52:48 +053081 // Update the priority for all the files.
82 updateFilePriority(yangNodeSet);
83
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053084 // TODO check for circular import/include.
85
86 // Carry out inter-file linking.
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053087 processInterFileLinking(yangNodeSet);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053088 }
89
90 /**
91 * Resolves sub-module linking by linking sub module with parent module.
92 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053093 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053094 * @throws LinkerException fails to link sub-module to parent module
95 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053096 public void linkSubModulesToParentModule(Set<YangNode> yangNodeSet)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053097 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053098 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053099 if (yangNode instanceof YangSubModule) {
100 try {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530101 ((YangSubModule) yangNode).linkWithModule(getYangNodeSet());
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530102 } catch (DataModelException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530103 String errorInfo = "Error in file: " + yangNode.getName() + " in " +
104 yangNode.getFileName() + " at " +
105 "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
106 + e.getLocalizedMessage();
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530107 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530108 // TODO add file path in exception message in util manager.
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530109 }
110 }
111 }
112 }
113
114 /**
115 * Adds imported node information to the import list.
116 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530117 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530118 * @throws LinkerException fails to find imported module
119 */
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530120 public void addRefToYangFilesImportList(Set<YangNode> yangNodeSet)
121 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530122 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530123 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530124 try {
125 ((YangReferenceResolver) yangNode).addReferencesToImportList(getYangNodeSet());
126 } catch (DataModelException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530127 String errorInfo = "Error in file: " + yangNode.getName() + " in " +
128 yangNode.getFileName() + " at " +
129 "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
130 + e.getLocalizedMessage();
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530131 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530132 // TODO add file path in exception message in util manager.
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530133 }
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530134 }
135 }
136 }
137
138 /**
139 * Adds included node information to the include list.
140 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530141 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530142 * @throws LinkerException fails to find included sub-module
143 */
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530144 public void addRefToYangFilesIncludeList(Set<YangNode> yangNodeSet)
145 throws LinkerException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530146 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530147 if (yangNode instanceof YangReferenceResolver) {
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530148 try {
149 ((YangReferenceResolver) yangNode).addReferencesToIncludeList(getYangNodeSet());
150 } catch (DataModelException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530151 String errorInfo = "Error in file: " + yangNode.getName() + " in " +
152 yangNode.getFileName() + " at " +
153 "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
154 + e.getLocalizedMessage();
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530155 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530156 // TODO add file path in exception message in util manager.
Gaurav Agrawal58b348e2016-06-07 14:00:26 +0530157 }
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530158 }
159 }
160 }
161
162 /**
163 * Processes inter file linking for type and uses.
164 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530165 * @param yangNodeSet set of YANG files info
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530166 * @throws LinkerException a violation in linker execution
167 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530168 public void processInterFileLinking(Set<YangNode> yangNodeSet)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530169 throws LinkerException {
Vidyashree Rama35e989c2016-07-12 20:52:48 +0530170 List<YangNode> yangNodeSortedList = new LinkedList<>();
171 yangNodeSortedList.addAll(yangNodeSet);
172 Collections.sort(yangNodeSortedList);
173 for (YangNode yangNode : yangNodeSortedList) {
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530174 try {
Vidyashree Rama13b4c552016-06-20 15:12:43 +0530175 ((YangReferenceResolver) yangNode)
176 .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE);
Shankara-Huawei234cd092016-07-14 11:35:34 +0530177 ((YangReferenceResolver) yangNode)
178 .resolveInterFileLinking(ResolvableType.YANG_USES);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530179 ((YangReferenceResolver) yangNode)
Vidyashree Rama918f1622016-07-28 17:33:15 +0530180 .resolveInterFileLinking(ResolvableType.YANG_AUGMENT);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530181 ((YangReferenceResolver) yangNode)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530182 .resolveInterFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE);
janani b0e4e8ae2016-07-13 21:06:41 +0530183 ((YangReferenceResolver) yangNode)
Shankara-Huawei234cd092016-07-14 11:35:34 +0530184 .resolveInterFileLinking(ResolvableType.YANG_BASE);
185 ((YangReferenceResolver) yangNode)
186 .resolveInterFileLinking(ResolvableType.YANG_IDENTITYREF);
janani bebb143d2016-07-14 19:35:22 +0530187 ((YangReferenceResolver) yangNode)
188 .resolveInterFileLinking(ResolvableType.YANG_LEAFREF);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530189 ((YangReferenceResolver) yangNode)
190 .resolveInterFileLinking(ResolvableType.YANG_COMPILER_ANNOTATION);
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530191 } catch (DataModelException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530192 String errorInfo = "Error in file: " + yangNode.getName() + " in " +
193 yangNode.getFileName() + " at " +
194 "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
195 + e.getLocalizedMessage();
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530196 throw new LinkerException(errorInfo);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530197 // TODO add file path in exception message in util manager.
Gaurav Agrawald39a9212016-08-20 18:51:20 +0530198 } catch (LinkerException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530199 String errorInfo = "Error in file: " + yangNode.getName() + " in " +
200 yangNode.getFileName() + " at " +
201 "line: " + e.getLineNumber() + " at position: " + e.getCharPositionInLine() + NEW_LINE
202 + e.getLocalizedMessage();
Gaurav Agrawald39a9212016-08-20 18:51:20 +0530203 throw new LinkerException(errorInfo);
204 // TODO add file path in exception message in util manager.
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530205 }
206 }
207 }
Vidyashree Rama35e989c2016-07-12 20:52:48 +0530208
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530209}