Sonar fixes in "appc-netconf-adapter-bundle"
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / onap / 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.onap.appc.adapter.netconf;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.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.util.*;
41
42 public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
43     private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";
44
45     @Override
46     public VnfType getVnfType() {
47         return VnfType.VNF;
48     }
49
50     @Override
51     public String getConfigurationFileName() {
52         String configFileName = OperationalStateValidatorFactory.configuration
53                 .getProperty(this.getClass().getCanonicalName() + CONFIG_FILE_PROPERTY_SUFFIX);
54         configFileName = configFileName == null ? "VnfGetOperationalStates" : configFileName;
55         return configFileName;
56     }
57
58     @Override
59     public void validateResponse(String response) throws APPCException {
60         if(StringUtils.isEmpty(response)) {
61             throw new APPCException("empty response");
62         }
63         try {
64             List<Map.Entry> operationalStateList = getOperationalStateList(response).orElseThrow(() ->
65                     new APPCException("response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element"));
66
67             if(operationalStateList.stream().anyMatch(this::isNotEnabled)) {
68                 throw new APPCException("at least one "+OPERATIONAL_STATE_ELEMENT_NAME+" is not in valid state. "
69                         +operationalStateList.toString());
70             }
71
72         } catch (Exception e) {
73             throw new APPCException(e);
74         }
75     }
76
77     private boolean isNotEnabled(Map.Entry stateEntry) {
78         return !("ENABLED").equalsIgnoreCase((String)stateEntry.getValue());
79     }
80
81     private static Optional<List<Map.Entry>> getOperationalStateList(String xmlText) throws IOException, ParserConfigurationException, SAXException {
82         List<Map.Entry> entryList = null;
83
84         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85         DocumentBuilder builder = factory.newDocumentBuilder();
86         Document document = builder.parse(new ByteArrayInputStream(xmlText.getBytes("UTF-8")));
87
88         if(document != null) {
89             Element rootElement = document.getDocumentElement();
90             NodeList nodeList = rootElement.getElementsByTagName(OPERATIONAL_STATE_ELEMENT_NAME);
91             if (nodeList != null && nodeList.getLength() > 0) {
92                 entryList = new ArrayList<>();
93                 for (int i = 0; i < nodeList.getLength(); i++) {
94                     Node node = nodeList.item(i);
95                     String text = node.getTextContent();
96                     String id = getElementID(node);
97                     Map.Entry entry = new AbstractMap.SimpleEntry<>(id, text);
98                     entryList.add(entry);
99                 }
100             }
101         }
102         return Optional.ofNullable(entryList);
103     }
104
105     private static String getElementID(Node node) {
106         String id = null;
107         Node parentNode = node.getParentNode();
108         if (parentNode != null) {
109             if (node.getNodeType() == Node.ELEMENT_NODE) {
110                 NodeList nodeList = ((Element) parentNode).getElementsByTagName("id");
111                 if (nodeList != null && nodeList.getLength() > 0) {
112                     Node idNode = nodeList.item(0);
113                     id = idNode != null ? idNode.getTextContent() : null;
114                 }
115             }else {
116                 id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
117             }
118         }
119
120         id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
121         id = StringUtils.isBlank(id) ? null : id;
122         id = id != null ? id : "unknown-id";
123         return id;
124     }
125
126 }