Updating licenses in 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  * APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.netconf;
24
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;
34
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;
41 import java.util.*;
42
43 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
44     private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
45     @Override
46     public VnfType getVnfType() {
47         return VnfType.VNF;
48     }
49
50     @Override
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;
55     }
56
57     @Override
58     public void validateResponse(String response) throws APPCException {
59         if(StringUtils.isEmpty(response)) {
60             throw new APPCException("empty response");
61         }
62
63         boolean isValid = false;
64         String errorMsg = "unexpected response";
65         try {
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();
71                         isValid = false;
72                         break;
73                     }else{
74                         isValid =true;
75                     }
76                 }
77             }else {
78                 errorMsg = "response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element";
79             }
80         } catch (Exception e ) {
81             isValid = false;
82             errorMsg = e.toString();
83         }
84         if(!isValid) throw new APPCException(errorMsg);
85     }
86
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();
92
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);
105                     }
106                 }
107             }
108         }
109         return entryList;
110     }
111
112     private static String getElementID(Node node) {
113         String id = null;
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;
121                 }
122             }else {
123                 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
124             }
125         }
126
127         id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
128         id = StringUtils.isBlank(id) ? null : id;
129         id = id != null ? id : "unknown-id";
130         return id;
131     }
132
133 }