blob: 8b50d33f136612dd10a789227f0399b8f8ba2168 [file] [log] [blame]
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05301/*
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
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 }
85
86 }
87
88 /**
89 * Returns the data root element based on the NETCONF operation parameter.
90 *
91 * @param rootElement root element of document tree to find the root node
92 * @param opType protocol operation being performed
93 * @return the data root node element
94 */
95 public Element getDataRootElement(Element rootElement,
96 YmsOperationType opType) {
97
98 Element retElement = null;
99 String elementName = rootElement.getName();
100 try {
101 validateOpType(elementName, opType);
102 // If config tag name is found then set the root element node.
103 if (ALLOWABLE_NAMES.contains(elementName)) {
104 return rootElement;
105 }
106
107 // If element has child node then traverse through the child node
108 // by recursively calling getDataRootElement method.
109 if (rootElement.hasContent() && !rootElement.isTextOnly()) {
110 for (Iterator i = rootElement.elementIterator();
111 i.hasNext();) {
112 Element childElement = (Element) i.next();
113 retElement = getDataRootElement(childElement, opType);
114 }
115 }
116
117 } catch (Exception e) {
118 // TODO
119 }
120
121 return retElement;
122 }
123}