2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package com.ericsson.testvnf.server.helper;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.SAXParseException;
28 import org.xml.sax.ext.DefaultHandler2;
30 import com.ericsson.testvnf.server.models.Hello;
31 import com.ericsson.testvnf.server.models.RpcData;
32 import com.ericsson.testvnf.server.models.SchemaDetails;
33 import com.ericsson.testvnf.server.requestqueue.RequestQueueHandler;
36 * Parses the xml requests and populates netconf related data to do operations
38 public class CustomParser extends DefaultHandler2 {
40 private Log log = LogFactory.getLog(CustomParser.class);
41 private RequestQueueHandler requestQueueHandler;
44 private RpcData rpcData;
45 private boolean nextIsOperationTag = false;
46 private boolean insideOperationTag = false;
47 private String operationTagValue = "";
48 private StringBuilder operationTagContent = new StringBuilder();
49 private boolean insideConfigurationDatastore = false;
50 private boolean insideSchemaDetailsTag = false;
51 private StringBuilder individualSchemaDetailsTagContent = new StringBuilder();
52 private SchemaDetails schemaDetails;
53 private boolean insideTargetNameTag = false;
54 private StringBuilder targetNameContent = new StringBuilder();
57 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
58 super.startElement(uri, localName, qName, attributes);
60 if (nextIsOperationTag) {
61 insideOperationTag = true;
62 rpcData.setOperation(localName);
63 operationTagValue = localName;
64 nextIsOperationTag = false;
65 if (localName.equalsIgnoreCase("get-schema")) {
66 insideSchemaDetailsTag = true;
67 schemaDetails = new SchemaDetails();
71 if (insideOperationTag) {
72 populateStartElementData(localName, attributes);
75 if (localName.equalsIgnoreCase("hello")) {
77 } else if (localName.equalsIgnoreCase("rpc")) {
78 rpcData = new RpcData();
80 messageId = attributes.getValue("message-id");
81 if (messageId == null)
82 throw new SAXException("Received <rpc> message without a message ID");
84 rpcData.setMessageId(messageId);
85 nextIsOperationTag = true;
91 public void characters(char[] ch, int start, int length) throws SAXException {
92 super.characters(ch, start, length);
94 if (insideOperationTag) {
95 operationTagContent.append(ch, start, length);
96 if (insideSchemaDetailsTag) {
97 individualSchemaDetailsTagContent.append(ch, start, length);
98 }else if (insideTargetNameTag) {
99 targetNameContent.append(ch, start, length);
104 public void endElement(String uri, String localName, String qName) throws SAXException {
105 super.endElement(uri, localName, qName);
107 if (insideOperationTag) {
108 populateEndElementData(localName);
110 if (localName.equalsIgnoreCase("hello")) {
111 requestQueueHandler.addToQueue(hello);
113 /* Query tags and operations */
114 } else if (localName.equalsIgnoreCase("rpc")) {
115 requestQueueHandler.addToQueue(rpcData);
117 }else if(operationTagValue.equals(localName)) {
118 rpcData.setOperationTagContent(operationTagContent.toString());
119 insideOperationTag = false;
120 operationTagContent = new StringBuilder();
121 if (localName.equalsIgnoreCase("get-schema")) {
122 insideSchemaDetailsTag = false;
123 rpcData.setSchemaDetails(schemaDetails);
124 schemaDetails = new SchemaDetails();
129 private void populateStartElementData(String localName, Attributes attributes) {
130 if (insideConfigurationDatastore) {
131 rpcData.setConfigurationDatastore(localName);
133 if (localName.equalsIgnoreCase("target") || localName.equalsIgnoreCase("source")) {
134 insideConfigurationDatastore = true;
135 } else if(localName.equalsIgnoreCase("target-name")) {
136 insideTargetNameTag = true;
138 operationTagContent.append("<" + localName);
139 for (int i = 0; i < attributes.getLength(); i++) {
140 operationTagContent.append(" " + attributes.getQName(i) + "=\"" + attributes.getValue(i) + "\"");
142 operationTagContent.append(">");
145 private void populateEndElementData(String localName) {
146 if (localName.equalsIgnoreCase("target") || localName.equalsIgnoreCase("source")) {
147 insideConfigurationDatastore = false;
148 } else if(localName.equalsIgnoreCase("target-name")) {
149 insideTargetNameTag = false;
150 rpcData.setTargetName(targetNameContent.toString());
151 targetNameContent = new StringBuilder();
152 } else if (insideSchemaDetailsTag) {
153 if(localName.equalsIgnoreCase("identifier")) {
154 schemaDetails.setIdentifier(individualSchemaDetailsTagContent.toString().trim());
155 individualSchemaDetailsTagContent = new StringBuilder();
156 } else if(localName.equalsIgnoreCase("version")) {
157 schemaDetails.setVersion(individualSchemaDetailsTagContent.toString().trim());
158 individualSchemaDetailsTagContent = new StringBuilder();
161 operationTagContent.append("</" + localName + ">");
164 public void setRequestQueueHandler(RequestQueueHandler queue) {
165 this.requestQueueHandler = queue;
169 public void error(SAXParseException e) throws SAXException {
170 log.warn(e.getMessage());
174 public void fatalError(SAXParseException e) throws SAXException {
175 log.warn(e.getMessage());