blob: 6a26a6ec45f9f230548364bdfa149c5ab6983a04 [file] [log] [blame]
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.ych.defaultcodecs.xml;
18
19import org.dom4j.Element;
20import org.onosproject.yms.app.ych.defaultcodecs.CodecHandlerFactory;
21import org.onosproject.yms.app.ydt.YdtExtendedContext;
22import org.onosproject.yms.app.ydt.YdtExtendedListener;
23import org.onosproject.yms.ych.YangProtocolEncodingFormat;
24import org.onosproject.yms.ydt.YdtContext;
25
26import java.util.Objects;
27import java.util.Stack;
28
29import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML;
30
31/**
32 * Represents implementation of codec YANG data object listener.
33 */
34class XmlCodecYdtListener implements YdtExtendedListener {
35
36 /**
37 * Data format type requested from driver.
38 */
39 private YangProtocolEncodingFormat dataFormat;
40
41 /**
42 * Stack for element is maintained for hierarchical references, this is
43 * used during YDT walker and preparation of xml/json.
44 */
45 private final Stack<Element> elementStack = new Stack<>();
46
47 /**
48 * Root name received from driver.
49 */
50 private YdtExtendedContext rootYdtNode;
51
52 /**
Vidyashree Rama9f34bad2016-12-08 18:07:08 +053053 * Module YDT node.
54 */
55 private YdtExtendedContext currentModule;
56
57 /**
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053058 * Creates a new codec listener.
59 *
60 * @param format protocol data format
61 * @param rootNode extended YDT root node
62 */
63 XmlCodecYdtListener(YangProtocolEncodingFormat format,
64 YdtExtendedContext rootNode) {
65 dataFormat = format;
66 rootYdtNode = rootNode;
Vidyashree Rama9f34bad2016-12-08 18:07:08 +053067 currentModule = ((YdtExtendedContext) rootNode.getFirstChild());
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053068 }
69
70 /**
71 * Returns the stack for the element.
72 *
73 * @return the stack for the element
74 */
75 Stack<Element> getElementStack() {
76 return elementStack;
77 }
78
Vidyashree Rama9f34bad2016-12-08 18:07:08 +053079 /**
80 * Returns true, if YDT node is module node; false otherwise.
81 *
82 * @param ydtContext YDT node
83 * @return true if YDT node is module; false otherwise
84 */
85 private boolean isModuleNode(YdtExtendedContext ydtContext,
86 boolean isExit) {
87 if (Objects.equals(currentModule, ydtContext)) {
88 if (isExit) {
89 currentModule = (YdtExtendedContext) currentModule
90 .getNextSibling();
91 }
92 return true;
93 }
94 return false;
95 }
96
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053097 @Override
98 public void enterYdtNode(YdtExtendedContext ydtContext) {
99
Vidyashree Rama9f34bad2016-12-08 18:07:08 +0530100 if (!Objects.equals(rootYdtNode, ydtContext) &&
101 !isModuleNode(ydtContext, false)) {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530102
103 CodecHandlerFactory factory = CodecHandlerFactory.instance();
104 XmlCodecHandler handler =
105 factory.getCodecHandlerForContext(ydtContext, dataFormat);
106 try {
107 if (dataFormat == XML && handler != null) {
108 handler.processXmlContext(ydtContext, elementStack);
109 }
110 } catch (Exception e) {
111 // TODO
112 }
113
114 if (dataFormat == XML && handler != null) {
115 handler.setXmlValue(ydtContext, elementStack);
116 }
117 }
118 }
119
120 @Override
121 public void exitYdtNode(YdtExtendedContext ydtExtendedContext) {
Vidyashree Rama9f34bad2016-12-08 18:07:08 +0530122 if (!Objects.equals(rootYdtNode, ydtExtendedContext) &&
123 !isModuleNode(ydtExtendedContext, true)) {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530124 elementStack.pop();
125 }
126 }
127
128 @Override
129 public void enterYdtNode(YdtContext ydtContext) {
130 }
131
132 @Override
133 public void exitYdtNode(YdtContext ydtContext) {
134 }
135
136}