2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.netconf;
25 import org.apache.commons.lang3.StringUtils;
26 import org.openecomp.appc.configuration.Configuration;
27 import org.openecomp.appc.configuration.ConfigurationFactory;
28 import org.openecomp.appc.exceptions.APPCException;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33 import org.xml.sax.SAXException;
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import javax.xml.parsers.ParserConfigurationException;
38 import java.io.ByteArrayInputStream;
39 import java.io.IOException;
40 import java.io.StringReader;
43 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
44 private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
46 public VnfType getVnfType() {
51 public String getConfigurationFileName() {
52 String configFileName = OperationalStateValidatorFactory.configuration.getProperty(this.getClass().getCanonicalName() + CONFIG_FILE_PROPERTY_SUFFIX);
53 configFileName = configFileName == null? "VnfGetOperationalStates" : configFileName;
54 return configFileName;
58 public void validateResponse(String response) throws APPCException {
59 if(StringUtils.isEmpty(response)) {
60 throw new APPCException("empty response");
63 boolean isValid = false;
64 String errorMsg = "unexpected response";
66 List<Map.Entry> operationalStateList = getOperationalStateList(response);
67 if(operationalStateList != null && !operationalStateList.isEmpty()) {
68 for (Map.Entry stateEntry : operationalStateList) {
69 if(!((String)stateEntry.getValue()).equalsIgnoreCase("ENABLED")){
70 errorMsg = "at least one "+OPERATIONAL_STATE_ELEMENT_NAME+" is not in valid satae. "+operationalStateList.toString();
78 errorMsg = "response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element";
80 } catch (Exception e ) {
82 errorMsg = e.toString();
84 if(!isValid) throw new APPCException(errorMsg);
87 private static List<Map.Entry> getOperationalStateList(String xmlText) throws IOException, ParserConfigurationException, SAXException {
88 List<Map.Entry> entryList = null;
89 if(StringUtils.isNotEmpty(xmlText)) {
90 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
91 DocumentBuilder builder = factory.newDocumentBuilder();
93 Document document = builder.parse(new ByteArrayInputStream(xmlText.getBytes("UTF-8")));
94 if(document != null) {
95 Element rootElement = document.getDocumentElement();
96 NodeList nodeList = rootElement.getElementsByTagName(OPERATIONAL_STATE_ELEMENT_NAME);
97 if (nodeList != null && nodeList.getLength() > 0) {
98 for (int i = 0; i < nodeList.getLength(); i++) {
99 Node node = nodeList.item(i);
100 String text = node.getTextContent();
101 String id = getElementID(node);
102 entryList = (entryList == null) ? new ArrayList<Map.Entry>() : entryList;
103 Map.Entry entry = new AbstractMap.SimpleEntry<String, String>(id, text);
104 entryList.add(entry);
112 private static String getElementID(Node node) {
114 Node parentNode = node.getParentNode();
115 if (parentNode != null) {
116 if (node.getNodeType() == Node.ELEMENT_NODE) {
117 NodeList nodeList = ((Element) parentNode).getElementsByTagName("id");
118 if (nodeList != null && nodeList.getLength() > 0) {
119 Node idNode = nodeList.item(0);
120 id = idNode != null ? idNode.getTextContent() : null;
123 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
127 id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
128 id = StringUtils.isBlank(id) ? null : id;
129 id = id != null ? id : "unknown-id";