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.netconf; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.dom4j.Element; |
| 21 | import org.onosproject.yms.app.ych.YchException; |
| 22 | import org.onosproject.yms.ydt.YmsOperationType; |
| 23 | |
| 24 | import java.util.Iterator; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.CONFIG; |
| 28 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.DATA; |
| 29 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.EDIT_CONFIG; |
| 30 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.FILTER; |
| 31 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.GET; |
| 32 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.GET_CONFIG; |
| 33 | import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST; |
| 34 | import static org.onosproject.yms.ydt.YmsOperationType.QUERY_CONFIG_REQUEST; |
| 35 | import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REQUEST; |
| 36 | |
| 37 | /** |
| 38 | * Represents an YCH netconf codec to find the root element in the xml string. |
| 39 | */ |
| 40 | public class NetconfCodec { |
| 41 | |
| 42 | private static final String PROTO_OPER_ERROR = "Received protocol " + |
| 43 | "operation is not same as in the XML string: "; |
| 44 | private static final Set<String> ALLOWABLE_NAMES = |
| 45 | ImmutableSet.of(CONFIG, DATA, FILTER); |
| 46 | |
| 47 | /** |
| 48 | * Validate the operation type. |
| 49 | * |
| 50 | * @param elementName tag name in the xml string |
| 51 | * @param opType operation type |
| 52 | */ |
| 53 | private void validateOpType(String elementName, YmsOperationType opType) { |
| 54 | switch (elementName) { |
| 55 | // edit-config tag name is found in xml then check the |
| 56 | // interaction type. |
| 57 | case EDIT_CONFIG: { |
| 58 | if (opType != EDIT_CONFIG_REQUEST) { |
| 59 | throw new YchException(PROTO_OPER_ERROR + opType); |
| 60 | } |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | // get-config tag name is found in xml then check the |
| 65 | // interaction type. |
| 66 | case GET_CONFIG: { |
| 67 | if (opType != QUERY_CONFIG_REQUEST) { |
| 68 | throw new YchException(PROTO_OPER_ERROR + opType); |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | // get tag name is found in xml then check the interaction type. |
| 74 | case GET: { |
| 75 | if (opType != QUERY_REQUEST) { |
| 76 | throw new YchException(PROTO_OPER_ERROR + opType); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | default: { |
| 82 | //TODO |
| 83 | } |
| 84 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the data root element based on the NETCONF operation parameter. |
| 89 | * |
| 90 | * @param rootElement root element of document tree to find the root node |
| 91 | * @param opType protocol operation being performed |
| 92 | * @return the data root node element |
| 93 | */ |
| 94 | public Element getDataRootElement(Element rootElement, |
| 95 | YmsOperationType opType) { |
| 96 | |
| 97 | Element retElement = null; |
| 98 | String elementName = rootElement.getName(); |
| 99 | try { |
| 100 | validateOpType(elementName, opType); |
| 101 | // If config tag name is found then set the root element node. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 102 | if (DATA.equals(elementName) |
| 103 | || CONFIG.equals(elementName) |
| 104 | || FILTER.equals(elementName)) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 105 | return rootElement; |
| 106 | } |
| 107 | |
| 108 | // If element has child node then traverse through the child node |
| 109 | // by recursively calling getDataRootElement method. |
| 110 | if (rootElement.hasContent() && !rootElement.isTextOnly()) { |
| 111 | for (Iterator i = rootElement.elementIterator(); |
| 112 | i.hasNext();) { |
| 113 | Element childElement = (Element) i.next(); |
| 114 | retElement = getDataRootElement(childElement, opType); |
| 115 | } |
| 116 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 117 | } catch (Exception e) { |
| 118 | // TODO |
| 119 | } |
| 120 | |
| 121 | return retElement; |
| 122 | } |
| 123 | } |