2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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=========================================================
 
  22 package org.openecomp.appc.adapter.netconf;
 
  24 import org.apache.commons.lang3.StringUtils;
 
  25 import org.openecomp.appc.configuration.Configuration;
 
  26 import org.openecomp.appc.configuration.ConfigurationFactory;
 
  27 import org.openecomp.appc.exceptions.APPCException;
 
  28 import org.w3c.dom.Document;
 
  29 import org.w3c.dom.Element;
 
  30 import org.w3c.dom.Node;
 
  31 import org.w3c.dom.NodeList;
 
  32 import org.xml.sax.SAXException;
 
  34 import javax.xml.parsers.DocumentBuilder;
 
  35 import javax.xml.parsers.DocumentBuilderFactory;
 
  36 import javax.xml.parsers.ParserConfigurationException;
 
  37 import java.io.ByteArrayInputStream;
 
  38 import java.io.IOException;
 
  39 import java.io.StringReader;
 
  42 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
 
  43     private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
 
  45     public VnfType getVnfType() {
 
  50     public String getConfigurationFileName() {
 
  51         String configFileName = OperationalStateValidatorFactory.configuration.getProperty(this.getClass().getCanonicalName() + CONFIG_FILE_PROPERTY_SUFFIX);
 
  52         configFileName = configFileName == null?  "VnfGetOperationalStates" : configFileName;
 
  53         return configFileName;
 
  57     public void validateResponse(String response) throws APPCException {
 
  58         if(StringUtils.isEmpty(response)) {
 
  59             throw new APPCException("empty response");
 
  62         boolean isValid = false;
 
  63         String errorMsg = "unexpected response";
 
  65             List<Map.Entry> operationalStateList = getOperationalStateList(response);
 
  66             if(operationalStateList != null && !operationalStateList.isEmpty()) {
 
  67                 for (Map.Entry stateEntry : operationalStateList) {
 
  68                     if(!((String)stateEntry.getValue()).equalsIgnoreCase("ENABLED")){
 
  69                         errorMsg = "at least one "+OPERATIONAL_STATE_ELEMENT_NAME+" is not in valid satae. "+operationalStateList.toString();
 
  77                 errorMsg = "response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element";
 
  79         } catch (Exception e ) {
 
  81             errorMsg = e.toString();
 
  83         if(!isValid) throw new APPCException(errorMsg);
 
  86     private static List<Map.Entry> getOperationalStateList(String xmlText) throws IOException, ParserConfigurationException, SAXException {
 
  87         List<Map.Entry> entryList = null;
 
  88         if(StringUtils.isNotEmpty(xmlText)) {
 
  89             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
  90             DocumentBuilder builder = factory.newDocumentBuilder();
 
  92             Document document = builder.parse(new ByteArrayInputStream(xmlText.getBytes("UTF-8")));
 
  93             if(document != null) {
 
  94                 Element rootElement = document.getDocumentElement();
 
  95                 NodeList nodeList = rootElement.getElementsByTagName(OPERATIONAL_STATE_ELEMENT_NAME);
 
  96                 if (nodeList != null && nodeList.getLength() > 0) {
 
  97                     for (int i = 0; i < nodeList.getLength(); i++) {
 
  98                         Node node = nodeList.item(i);
 
  99                         String text = node.getTextContent();
 
 100                         String id = getElementID(node);
 
 101                         entryList = (entryList == null) ? new ArrayList<Map.Entry>() : entryList;
 
 102                         Map.Entry entry = new AbstractMap.SimpleEntry<String, String>(id, text);
 
 103                         entryList.add(entry);
 
 111     private static String getElementID(Node node) {
 
 113         Node parentNode = node.getParentNode();
 
 114         if (parentNode != null) {
 
 115             if (node.getNodeType() == Node.ELEMENT_NODE) {
 
 116                 NodeList nodeList = ((Element) parentNode).getElementsByTagName("id");
 
 117                 if (nodeList != null && nodeList.getLength() > 0) {
 
 118                     Node idNode = nodeList.item(0);
 
 119                     id = idNode != null ? idNode.getTextContent() : null;
 
 122                 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
 
 126         id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
 
 127         id = StringUtils.isBlank(id) ? null : id;
 
 128         id = id != null ? id : "unknown-id";