1 define([], function () {
4 function YinParserService($http, SyncService, constants, PathUtilsService, YangUiApisService, NodeUtilsService){
5 var augmentType = 'augment',
9 parseYangMP: parseYangMP,
10 Augmentations: Augmentations,
12 yangParser: new YangParser(),
16 yangParser: new YangParser(),
17 Augmentation: Augmentation,
24 // TODO: add service's description
25 function parseYangMP(baseApiPath, name, rev, callback, errorCbk) {
26 var path = baseApiPath + '/' + name + '/' + rev + '/schema';
28 YangUiApisService.getSingleModuleInfo(path)
29 .then(function (data) {
30 if ($.parseXML(data.data) !== null) {
31 parseModule(data.data, callback);
33 loadStaticModule(name, callback, errorCbk);
36 loadStaticModule(name, callback, errorCbk);
40 // TODO: add service's description
41 function parseYang(name, rev, callback, errorCbk) {
42 YangUiApisService.getModuleSchema(name, rev).get()
43 .then(function (data) {
44 if ($.parseXML(data) !== null) {
45 parseModule(data, callback);
47 loadStaticModule(name, callback, errorCbk);
50 loadStaticModule(name, callback, errorCbk);
54 // TODO: add service's description
55 function parentTag(xml) {
56 if (xml.get(0).tagName.toLowerCase() === 'module') {
59 return parentTag(xml.parent());
63 // TODO: add function's description
64 function parseModule(data, callback) {
65 var yangParser = new YangParser();
67 var moduleName = $($.parseXML(data).documentElement).attr('name'),
68 moduleNamespace = $($.parseXML(data)).find('namespace').attr('uri'),
69 moduleoduleRevision = $($.parseXML(data)).find('revision').attr('date'),
70 moduleObj = new Module(moduleName, moduleoduleRevision, moduleNamespace);
72 yangParser.setCurrentModuleObj(moduleObj);
73 yangParser.parse($.parseXML(data).documentElement, moduleObj);
75 yangParser.sync.waitFor(function () {
80 // TODO: add function's description
81 function loadStaticModule(name, callback, errorCbk) {
82 var yinPath = '/yang2xml/' + name + '.yang.xml';
83 $http.get(path + yinPath).success(function (data) {
84 parseModule(data, callback);
85 }).error(function () {
98 function Module(name, revision, namespace) {
100 this._revision = revision;
101 this._namespace = namespace;
102 this._statements = {};
106 this.getRoots = function () {
110 this.getImportByPrefix = function (prefix) {
111 var importNode = null;
113 if (this._statements.hasOwnProperty('import')) {
114 importNode = this._statements.import.filter(function (importItem) {
115 return importItem._prefix === prefix;
122 this.getRawAugments = function () {
123 return this._augments;
126 this.getAugments = function () {
129 return this.getRawAugments().map(function (augNode) {
130 augNode.path = PathUtilsService.translate(augNode.pathString, prefixConverter, self._statements.import, getDefaultModule);
132 return new Augmentation(augNode);
134 // TODO: add function's description
135 function prefixConverter(prefix) {
136 var importNode = self.getImportByPrefix(prefix);
137 return importNode ? importNode.label : null;
140 // TODO: add function's description
141 function getDefaultModule() {
147 this.addChild = function (node) {
148 if (!this._statements.hasOwnProperty(node.type)) {
149 this._statements[node.type] = [];
152 var duplicates = this._statements[node.type].filter(function (item) {
153 return node.label === item.label && node.nodeType === item.nodeType;
156 if (duplicates && duplicates.length > 0) {
158 this._statements[node.type].push(node);
160 if (NodeUtilsService.isRootNode(node.type)) {
161 this._roots.push(node);
164 if (node.type === 'augment') {
165 this._augments.push(node);
170 this.searchNode = function (type, name) {
171 var searchResults = null,
174 if (this._statements[type]) {
175 searchResults = this._statements[type].filter(function (node) {
176 return name === node.label;
180 if (searchResults && searchResults.length === 0) {
181 } else if (searchResults && searchResults.length > 1) {
182 } else if (searchResults && searchResults.length === 1) {
183 searchedNode = searchResults[0];
191 * Base Node element object
199 * @param moduleRevision
202 function Node(id, name, type, module, namespace, parent, nodeType, moduleRevision) {
205 this.localeLabel = constants.LOCALE_PREFIX + name.toUpperCase();
207 this.module = module;
209 this.parent = parent;
210 this.nodeType = nodeType;
211 this.namespace = namespace;
212 this.moduleRevision = moduleRevision;
214 this.appendTo = function (parentNode) {
215 parentNode.addChild(this);
218 this.addChild = function (node) {
219 if (this.children.indexOf(node) === -1) {
220 this.children.push(node);
226 this.deepCopy = function deepCopy(additionalProperties) {
227 var copy = new Node(this.id, this.label, this.type, this.module, this.namespace, null,
228 this.nodeType, this.moduleRevision),
231 additionalProperties = (additionalProperties || []).concat(['pathString']);
233 additionalProperties.forEach(function (prop) {
234 if (prop !== 'children' && self.hasOwnProperty(prop) && copy.hasOwnProperty(prop) === false) {
235 copy[prop] = self[prop];
239 this.children.forEach(function (child) {
240 var childCopy = child.deepCopy(additionalProperties);
241 childCopy.parent = copy;
242 copy.children.push(childCopy);
247 this.getCleanCopy = function (){
248 return new Node(this.id, this.label, this.type, this.module, this.namespace, null,
249 this.nodeType, this.moduleRevision);
252 this.getChildren = function (type, name, nodeType, property) {
253 var filteredChildren = this.children.filter(function (item) {
254 return (name != null ? name === item.label : true) &&
255 (type != null ? type === item.type : true) &&
256 (nodeType != null ? nodeType === item.nodeType : true);
260 return filteredChildren.filter(function (item) {
261 return item.hasOwnProperty(property);
262 }).map(function (item) {
263 return item[property];
266 return filteredChildren;
273 * Base Augment group object
276 function AugmentationsGroup(){
279 this.addAugumentation = function (augumentation){
280 this.obj[augumentation.id] = augumentation;
285 * Base augment's groups object
288 function Augmentations(){
291 this.addGroup = function (groupId){
292 this.groups[groupId] = !this.groups.hasOwnProperty(groupId) ?
293 new AugmentationsGroup() : this.groups[groupId];
296 this.getAugmentation = function (node, augId) {
297 return this.groups[node.module + ':' + node.label] ?
298 this.groups[node.module + ':' + node.label].obj[augId] : null;
303 * Base Augment object
307 function Augmentation(node) {
310 this.path = (node.path ? node.path : []);
311 this.id = node.module + ':' + node.label;
312 this.expanded = true;
314 // node.label = node.module + ':' + node.label;
317 this.toggleExpand = function () {
318 this.expanded = !this.expanded;
321 this.setAugmentationGroup = function (targetNode, augumentations){
322 var targetNodeId = targetNode.module + ':' + targetNode.label;
323 targetNode.augmentionGroups = targetNode.augmentionGroups ? targetNode.augmentionGroups : [];
324 targetNode.augmentionGroups.push(self.id);
326 augumentations.addGroup(targetNodeId);
327 augumentations.groups[targetNodeId].addAugumentation(self);
330 this.apply = function (nodeList, augumentations) {
331 var targetNode = this.getTargetNodeToAugment(nodeList);
334 this.setAugmentationGroup(targetNode, augumentations);
336 this.node.children.forEach(function (child) {
337 child.appendTo(targetNode);
338 child.augmentationId = self.id;
340 // child.children.forEach(function (moduleChild) {
341 // moduleChild.label = moduleChild.module + ':' + moduleChild.label;
348 this.getTargetNodeToAugment = function (nodeList) {
349 return PathUtilsService.search({ children: nodeList }, this.path.slice());
352 this.getPathString = function () {
353 return this.path.map(function (elem) {
354 return elem.module + ':' + elem.name;
361 * Base yang xml parser
364 function YangParser() {
367 this.sync = SyncService.generateObj();
368 this.moduleObj = null;
370 this.setCurrentModuleObj = function (moduleObj) {
371 this.moduleObj = moduleObj;
374 this.createNewNode = function (name, type, parentNode, nodeType) {
375 var node = new Node(this.nodeIndex++, name, type, this.moduleObj._name, this.moduleObj._namespace, parentNode, nodeType, this.moduleObj._revision);
378 parentNode.addChild(node);
384 this.parse = function (xml, parent) {
387 $(xml).children().each(function (_, item) {
388 var prop = item.tagName.toLowerCase();
389 if (self.hasOwnProperty(prop)) {
390 self[prop](item, parent);
392 // self.parse(this, parent);
397 this.config = function (xml, parent) {
398 var type = constants.DATA_STORE_CONFIG,
399 name = $(xml).attr('value'),
400 nodeType = constants.NODE_ALTER;
402 this.createNewNode(name, type, parent, nodeType);
405 this.presence = function (xml, parent) {
406 var type = 'presence',
407 name = $(xml).attr('value'),
408 nodeType = constants.NODE_ALTER;
410 this.createNewNode(name, type, parent, nodeType);
413 this.leaf = function (xml, parent) {
415 name = $(xml).attr('name'),
416 nodeType = constants.NODE_UI_DISPLAY,
417 node = this.createNewNode(name, type, parent, nodeType);
419 this.parse(xml, node);
422 this['leaf-list'] = function (xml, parent) {
423 var type = 'leaf-list',
424 name = $(xml).attr('name'),
425 nodeType = constants.NODE_UI_DISPLAY,
426 node = this.createNewNode(name, type, parent, nodeType);
428 this.parse(xml, node);
431 this.container = function (xml, parent) {
432 var type = 'container',
433 name = $(xml).attr('name'),
434 nodeType = constants.NODE_UI_DISPLAY,
435 node = this.createNewNode(name, type, parent, nodeType);
437 this.parse(xml, node);
440 this.choice = function (xml, parent) {
442 name = $(xml).attr('name'),
443 nodeType = constants.NODE_UI_DISPLAY,
444 node = this.createNewNode(name, type, parent, nodeType);
446 this.parse(xml, node);
449 this.case = function (xml, parent) {
451 name = $(xml).attr('name'),
452 nodeType = constants.NODE_UI_DISPLAY,
453 node = this.createNewNode(name, type, parent, nodeType);
455 this.parse(xml, node);
458 this.list = function (xml, parent) {
460 name = $(xml).attr('name'),
461 nodeType = constants.NODE_UI_DISPLAY,
462 node = this.createNewNode(name, type, parent, nodeType);
464 this.parse(xml, node);
468 this.key = function (xml, parent) {
470 name = $(xml).attr('value'),
471 nodeType = constants.NODE_ALTER,
472 node = this.createNewNode(name, type, parent, nodeType);
474 this.parse(xml, node);
477 this.description = function (xml, parent) {
478 var type = 'description',
479 name = $(xml).attr('text') ? $(xml).attr('text') : $(xml).children('text:first').text(),
480 nodeType = constants.NODE_ALTER,
481 node = this.createNewNode(name, type, parent, nodeType);
483 this.parse(xml, node);
486 this.typedef = function (xml, parent, typedefName) {
487 var type = 'typedef',
488 name = $(xml).attr('name'),
489 nodeType = constants.NODE_LINK_TARGET,
490 node = this.createNewNode(name, type, parent, nodeType);
492 this.parse(xml, node);
495 this.grouping = function (xml, parent, groupingName) {
496 var type = 'grouping',
497 name = $(xml).attr('name'),
498 nodeType = constants.NODE_LINK_TARGET,
499 node = this.createNewNode(name, type, parent, nodeType);
501 this.parse(xml, node);
504 this.uses = function (xml, parent) {
506 name = $(xml).attr('name'),
507 nodeType = constants.NODE_LINK,
508 node = this.createNewNode(name, type, parent, nodeType);
510 this.parse(xml, node);
513 this.import = function (xml, parent) {
515 name = $(xml).attr('module'),
516 nodeType = constants.NODE_ALTER,
517 node = this.createNewNode(name, type, parent, nodeType);
519 node._prefix = $(xml).children('prefix:first').attr('value');
520 node._revisionDate = $(xml).children('revision-date:first').attr('date');
523 this.augment = function (xml, parent) {
524 var type = augmentType,
525 nodeType = constants.NODE_ALTER,
526 augmentIndentifier = $(xml).children('ext\\:augment-identifier:first').attr('ext:identifier'),
527 name = augmentIndentifier ? augmentIndentifier : 'augment' + (this.nodeIndex + 1).toString(),
528 pathString = $(xml).attr('target-node'),
529 augmentRoot = this.createNewNode(name, type, parent, nodeType);
531 augmentRoot.pathString = pathString;
532 this.parse(xml, augmentRoot);
536 this.rpc = function (xml, parent) {
537 var type = constants.NODE_RPC,
538 name = $(xml).attr('name'),
539 nodeType = constants.NODE_UI_DISPLAY,
540 node = this.createNewNode(name, type, parent, nodeType);
542 this.parse(xml, node);
545 this.input = function (xml, parent) {
548 nodeType = constants.NODE_UI_DISPLAY,
549 node = this.createNewNode(name, type, parent, nodeType);
551 this.parse(xml, node);
554 this.output = function (xml, parent) {
557 nodeType = constants.NODE_UI_DISPLAY,
558 node = this.createNewNode(name, type, parent, nodeType);
560 this.parse(xml, node);
563 this.pattern = function (xml, parent) {
564 var type = 'pattern',
565 name = $(xml).attr('value'),
566 nodeType = constants.NODE_RESTRICTIONS;
568 this.createNewNode(name, type, parent, nodeType);
571 this.range = function (xml, parent) {
573 name = $(xml).attr('value'),
574 nodeType = constants.NODE_RESTRICTIONS;
576 this.createNewNode(name, type, parent, nodeType);
579 this.length = function (xml, parent) {
581 name = $(xml).attr('value'),
582 nodeType = constants.NODE_RESTRICTIONS;
584 this.createNewNode(name, type, parent, nodeType);
587 this.enum = function (xml, parent) {
589 name = $(xml).attr('name'),
590 nodeType = constants.NODE_ALTER;
592 this.createNewNode(name, type, parent, nodeType);
595 this.bit = function (xml, parent) {
597 name = $(xml).attr('name'),
598 nodeType = constants.NODE_ALTER,
599 node = this.createNewNode(name, type, parent, nodeType);
601 this.parse(xml, node);
604 this.position = function (xml, parent) {
605 var type = 'position',
606 name = $(xml).attr('value'),
607 nodeType = constants.NODE_ALTER;
609 this.createNewNode(name, type, parent, nodeType);
612 this.type = function (xml, parent) {
614 name = $(xml).attr('name'),
615 nodeType = constants.NODE_ALTER,
616 node = this.createNewNode(name, type, parent, nodeType);
618 this.parse(xml, node);
623 YinParserService.$inject = ['$http', 'SyncService', 'constants', 'PathUtilsService', 'YangUiApisService',
626 return YinParserService;