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