summaryrefslogtreecommitdiffstats
path: root/adaptors/netconf-adaptor/netconf-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/netconf/VNFOperationalStateValidatorImpl.java
blob: 3a6b1428a6ef9c79dfdf659c19e6336da0dd7621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.adaptors.netconf;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class VNFOperationalStateValidatorImpl implements OperationalStateValidator {
    private static final String OPERATIONAL_STATE_ELEMENT_NAME = "operationalState";

    @Override
    public VnfType getVnfType() {
        return VnfType.VNF;
    }

    @Override
    public String getConfigurationFileName() {
        String configFileName = OperationalStateValidatorFactory.configuration
                .getProperty(this.getClass().getCanonicalName() + CONFIG_FILE_PROPERTY_SUFFIX);
        configFileName = configFileName == null ? "VnfGetOperationalStates" : configFileName;
        return configFileName;
    }

    @Override
    public void validateResponse(String response) throws SvcLogicException {
        if(StringUtils.isEmpty(response)) {
            throw new SvcLogicException("empty response");
        }
        try {
            List<Map.Entry> operationalStateList = getOperationalStateList(response).orElseThrow(() ->
                    new SvcLogicException("response without any "+OPERATIONAL_STATE_ELEMENT_NAME+" element"));

            if(operationalStateList.stream().anyMatch(this::isNotEnabled)) {
                throw new SvcLogicException("at least one "+OPERATIONAL_STATE_ELEMENT_NAME+" is not in valid state. "
                        +operationalStateList.toString());
            }

        } catch (Exception e) {
            throw new SvcLogicException(e.toString());
        }
    }

    private boolean isNotEnabled(Map.Entry stateEntry) {
        return !("ENABLED").equalsIgnoreCase((String)stateEntry.getValue());
    }

    private static Optional<List<Map.Entry>> getOperationalStateList(String xmlText) throws IOException, ParserConfigurationException, SAXException {
        List<Map.Entry> entryList = null;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(xmlText.getBytes(StandardCharsets.UTF_8)));

        if(document != null) {
            Element rootElement = document.getDocumentElement();
            NodeList nodeList = rootElement.getElementsByTagName(OPERATIONAL_STATE_ELEMENT_NAME);
            if (nodeList != null && nodeList.getLength() > 0) {
                entryList = new ArrayList<>();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    String text = node.getTextContent();
                    String id = getElementID(node);
                    Map.Entry entry = new AbstractMap.SimpleEntry<>(id, text);
                    entryList.add(entry);
                }
            }
        }
        return Optional.ofNullable(entryList);
    }

    private static String getElementID(Node node) {
        String id = null;
        Node parentNode = node.getParentNode();
        if (parentNode != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                NodeList nodeList = ((Element) parentNode).getElementsByTagName("id");
                if (nodeList != null && nodeList.getLength() > 0) {
                    Node idNode = nodeList.item(0);
                    id = idNode != null ? idNode.getTextContent() : null;
                }
            }else {
                id = parentNode.getNodeValue()+"|"+parentNode.getTextContent();
            }
        }

        id = StringUtils.isEmpty(id) ? null : StringUtils.normalizeSpace(id);
        id = StringUtils.isBlank(id) ? null : id;
        id = id != null ? id : "unknown-id";
        return id;
    }

}