Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yms.app.ych.defaultcodecs.xml; |
| 18 | |
| 19 | import org.dom4j.Element; |
| 20 | import org.onosproject.yms.app.ych.defaultcodecs.CodecHandlerFactory; |
| 21 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 22 | import org.onosproject.yms.app.ydt.YdtExtendedListener; |
| 23 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 24 | import org.onosproject.yms.ydt.YdtContext; |
| 25 | |
| 26 | import java.util.Objects; |
| 27 | import java.util.Stack; |
| 28 | |
| 29 | import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML; |
| 30 | |
| 31 | /** |
| 32 | * Represents implementation of codec YANG data object listener. |
| 33 | */ |
| 34 | class 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 Rama | 9f34bad | 2016-12-08 18:07:08 +0530 | [diff] [blame] | 53 | * Module YDT node. |
| 54 | */ |
| 55 | private YdtExtendedContext currentModule; |
| 56 | |
| 57 | /** |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 58 | * 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 Rama | 9f34bad | 2016-12-08 18:07:08 +0530 | [diff] [blame] | 67 | currentModule = ((YdtExtendedContext) rootNode.getFirstChild()); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 68 | } |
| 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 Rama | 9f34bad | 2016-12-08 18:07:08 +0530 | [diff] [blame] | 79 | /** |
| 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-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 97 | @Override |
| 98 | public void enterYdtNode(YdtExtendedContext ydtContext) { |
| 99 | |
Vidyashree Rama | 9f34bad | 2016-12-08 18:07:08 +0530 | [diff] [blame] | 100 | if (!Objects.equals(rootYdtNode, ydtContext) && |
| 101 | !isModuleNode(ydtContext, false)) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 102 | |
| 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 Rama | 9f34bad | 2016-12-08 18:07:08 +0530 | [diff] [blame] | 122 | if (!Objects.equals(rootYdtNode, ydtExtendedContext) && |
| 123 | !isModuleNode(ydtExtendedContext, true)) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 124 | 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 | } |