Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | /** |
| 53 | * Creates a new codec listener. |
| 54 | * |
| 55 | * @param format protocol data format |
| 56 | * @param rootNode extended YDT root node |
| 57 | */ |
| 58 | XmlCodecYdtListener(YangProtocolEncodingFormat format, |
| 59 | YdtExtendedContext rootNode) { |
| 60 | dataFormat = format; |
| 61 | rootYdtNode = rootNode; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the stack for the element. |
| 66 | * |
| 67 | * @return the stack for the element |
| 68 | */ |
| 69 | Stack<Element> getElementStack() { |
| 70 | return elementStack; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void enterYdtNode(YdtExtendedContext ydtContext) { |
| 75 | |
| 76 | if (!Objects.equals(rootYdtNode, ydtContext)) { |
| 77 | |
| 78 | CodecHandlerFactory factory = CodecHandlerFactory.instance(); |
| 79 | XmlCodecHandler handler = |
| 80 | factory.getCodecHandlerForContext(ydtContext, dataFormat); |
| 81 | try { |
| 82 | if (dataFormat == XML && handler != null) { |
| 83 | handler.processXmlContext(ydtContext, elementStack); |
| 84 | } |
| 85 | } catch (Exception e) { |
| 86 | // TODO |
| 87 | } |
| 88 | |
| 89 | if (dataFormat == XML && handler != null) { |
| 90 | handler.setXmlValue(ydtContext, elementStack); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void exitYdtNode(YdtExtendedContext ydtExtendedContext) { |
| 97 | if (!Objects.equals(rootYdtNode, ydtExtendedContext)) { |
| 98 | elementStack.pop(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void enterYdtNode(YdtContext ydtContext) { |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void exitYdtNode(YdtContext ydtContext) { |
| 108 | } |
| 109 | |
| 110 | } |