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