Change nexus values to properties
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / openecomp / appc / adapter / netconf / VNFOperationalStateValidatorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.adapter.netconf;
23
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;
33
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;
40 import java.util.*;
41
42 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
43     private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
44     @Override
45     public VnfType getVnfType() {
46         return VnfType.VNF;
47     }
48
49     @Override
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;
54     }
55
56     @Override
57     public void validateResponse(String response) throws APPCException {
58         if(StringUtils.isEmpty(response)) {
59             throw new APPCException("empty response");
60         }
61
62         boolean isValid = false;
63         String errorMsg = "unexpected response";
64         try {
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();
70                         isValid = false;
71                         break;
72                     }else{
73                         isValid =true;
74                     }
75                 }
76             }else {
77                 errorMsg = "response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element";
78             }
79         } catch (Exception e ) {
80             isValid = false;
81             errorMsg = e.toString();
82         }
83         if(!isValid) throw new APPCException(errorMsg);
84     }
85
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();
91
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);
104                     }
105                 }
106             }
107         }
108         return entryList;
109     }
110
111     private static String getElementID(Node node) {
112         String id = null;
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;
120                 }
121             }else {
122                 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
123             }
124         }
125
126         id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
127         id = StringUtils.isBlank(id) ? null : id;
128         id = id != null ? id : "unknown-id";
129         return id;
130     }
131
132 }