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