blob: a1138ce0d86a59ebaec3e2ddc1b9aae3a4087a48 [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.netconf;
18
19import com.google.common.collect.ImmutableSet;
20import org.dom4j.Element;
21import org.onosproject.yms.app.ych.YchException;
22import org.onosproject.yms.ydt.YmsOperationType;
23
24import java.util.Iterator;
25import java.util.Set;
26
27import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.CONFIG;
28import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.DATA;
29import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.EDIT_CONFIG;
30import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.FILTER;
31import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.GET;
32import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.GET_CONFIG;
33import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST;
34import static org.onosproject.yms.ydt.YmsOperationType.QUERY_CONFIG_REQUEST;
35import 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 */
40public 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-Huaweid5823ab2016-11-22 10:14:52 +053085 }
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 guptaeff184b2016-11-24 12:43:49 +0530102 if (DATA.equals(elementName)
103 || CONFIG.equals(elementName)
104 || FILTER.equals(elementName)) {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530105 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-Huaweid5823ab2016-11-22 10:14:52 +0530117 } catch (Exception e) {
118 // TODO
119 }
120
121 return retElement;
122 }
123}