Applying license changes to all files
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.netconf;
26
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;
36
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;
43 import java.util.*;
44
45 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
46     private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
47     @Override
48     public VnfType getVnfType() {
49         return VnfType.VNF;
50     }
51
52     @Override
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;
57     }
58
59     @Override
60     public void validateResponse(String response) throws APPCException {
61         if(StringUtils.isEmpty(response)) {
62             throw new APPCException("empty response");
63         }
64
65         boolean isValid = false;
66         String errorMsg = "unexpected response";
67         try {
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();
73                         isValid = false;
74                         break;
75                     }else{
76                         isValid =true;
77                     }
78                 }
79             }else {
80                 errorMsg = "response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element";
81             }
82         } catch (Exception e ) {
83             isValid = false;
84             errorMsg = e.toString();
85         }
86         if(!isValid) throw new APPCException(errorMsg);
87     }
88
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();
94
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);
107                     }
108                 }
109             }
110         }
111         return entryList;
112     }
113
114     private static String getElementID(Node node) {
115         String id = null;
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;
123                 }
124             }else {
125                 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
126             }
127         }
128
129         id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
130         id = StringUtils.isBlank(id) ? null : id;
131         id = id != null ? id : "unknown-id";
132         return id;
133     }
134
135 }