Adding TestVNF netconf server
[demo.git] / vnfs / TestVNF / netconfserver / src / main / java / com / ericsson / testvnf / server / helper / CustomParser.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package com.ericsson.testvnf.server.helper;
22
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;
29
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;
34
35 /*
36  * Parses the xml requests and populates netconf related data to do operations
37  */
38 public class CustomParser extends DefaultHandler2 {
39         
40         private Log log = LogFactory.getLog(CustomParser.class);
41         private RequestQueueHandler requestQueueHandler;
42
43         private Hello hello;
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();
55
56         @Override
57         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
58                 super.startElement(uri, localName, qName, attributes);
59                 String messageId;
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();
68                         }
69                 }
70                 
71                 if (insideOperationTag) {
72                         populateStartElementData(localName, attributes);
73                 }
74                 
75                 if (localName.equalsIgnoreCase("hello")) {
76                         hello = new Hello();
77                 } else if (localName.equalsIgnoreCase("rpc")) {
78                         rpcData = new RpcData();
79
80                         messageId = attributes.getValue("message-id");
81                         if (messageId == null)
82                                 throw new SAXException("Received <rpc> message without a message ID");
83
84                         rpcData.setMessageId(messageId);
85                         nextIsOperationTag = true;
86                 }
87
88         }
89         
90         @Override
91         public void characters(char[] ch, int start, int length) throws SAXException {
92                 super.characters(ch, start, length);
93                 
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);
100                         }
101                 }
102         }
103         @Override
104         public void endElement(String uri, String localName, String qName) throws SAXException {
105                 super.endElement(uri, localName, qName);
106                 
107                 if (insideOperationTag) {
108                         populateEndElementData(localName);
109                 }
110                 if (localName.equalsIgnoreCase("hello")) {
111                         requestQueueHandler.addToQueue(hello);
112                         hello = null;
113                         /* Query tags and operations */
114                 } else if (localName.equalsIgnoreCase("rpc")) {
115                         requestQueueHandler.addToQueue(rpcData);
116                         rpcData = null;
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();
125                         }
126                 }
127         }
128
129         private void populateStartElementData(String localName, Attributes attributes) {
130                 if (insideConfigurationDatastore) {
131                         rpcData.setConfigurationDatastore(localName);
132                 }
133                 if (localName.equalsIgnoreCase("target") || localName.equalsIgnoreCase("source")) {
134                         insideConfigurationDatastore = true;
135                 } else if(localName.equalsIgnoreCase("target-name")) {
136                         insideTargetNameTag = true;
137                 }
138                 operationTagContent.append("<" + localName);
139                 for (int i = 0; i < attributes.getLength(); i++) {
140                         operationTagContent.append(" " + attributes.getQName(i) + "=\"" + attributes.getValue(i) + "\"");
141                 }
142                 operationTagContent.append(">");
143         }
144         
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();
159                         }               
160                 }
161                 operationTagContent.append("</" + localName + ">");
162         }
163         
164         public void setRequestQueueHandler(RequestQueueHandler queue) {
165                 this.requestQueueHandler = queue;
166         }
167         
168         @Override
169         public void error(SAXParseException e) throws SAXException {
170                 log.warn(e.getMessage());
171         }
172
173         @Override
174         public void fatalError(SAXParseException e) throws SAXException {
175                 log.warn(e.getMessage());
176         }
177 }