aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/CapabilitiesDataExtractor.java
blob: a98d3268edda9d15472225735d809d4e09b55990 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2018 Nokia. All rights reserved.
 * Copyright (C) 2019 AT&T intellectual property. All rights reserved.
 * =============================================================================
 * 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.appc.flow.controller.node;

import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VF_MODULE;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VM;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF;
import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
import org.onap.appc.flow.controller.interfaceData.Capabilities;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;

public class CapabilitiesDataExtractor {

    private static final EELFLogger log = EELFManager.getInstance().getLogger(CapabilitiesDataExtractor.class);

    private final FlowControlDBService dbService;
    private final ObjectMapper mapper;

    public CapabilitiesDataExtractor() {
        this(FlowControlDBService.initialise());
    }

    /**
     * Ctor for tests, prefer to use default one
     */
    public CapabilitiesDataExtractor(FlowControlDBService dbService) {
        this.dbService = dbService;

        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    }

    Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {

        String fn = "FlowExecutorNode.getCapabilitiesData";
        String capabilitiesData = dbService.getCapabilitiesData(ctx);
        log.info(fn + ":capabilitiesDataInput:" + capabilitiesData);

        Capabilities capabilities = new Capabilities();
        if (StringUtils.isBlank(capabilitiesData)) {
            return capabilities;
        }

        JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
        JsonNode capNode = capabilitiesNode.get("capabilities");
        log.info("capabilitiesNode:" + capabilitiesNode.toString());

        capabilities.getVfModule().addAll(extractParameterList(capNode, VF_MODULE));
        capabilities.getVnfc().addAll(extractParameterList(capNode, VNFC));
        capabilities.getVnf().addAll(extractParameterList(capNode, VNF));
        capabilities.getVm().putAll(extractParameterMap(capNode, VM));

        log.info("Capabilities Output:" + capabilities.toString());

        return capabilities;
    }

    private List<String> extractParameterList(JsonNode root, String parameter) throws IOException {
        JsonNode parameterNode = root.get(parameter);
        if (parameterNode == null) {
            return new ArrayList<>();
        }
        return mapper.readValue(parameterNode.toString(), new TypeReference<List<String>>() {
        });
    }

    private HashMap<String, List<String>> extractParameterMap(JsonNode root, String parameter) throws IOException {
        JsonNode parameterNode = root.get(parameter);
        HashMap<String, List<String>> hm = new HashMap<>();
        if (parameterNode == null || !parameterNode.isArray()) {
            return hm;
        }
        for (JsonNode n : parameterNode) {
            Iterator<Map.Entry<String, JsonNode>> fldIter = n.fields();
            while (fldIter.hasNext()) {
                Map.Entry<String, JsonNode> currentEntry = fldIter.next();
                if (currentEntry.getValue().isArray()) {
                    Iterator<JsonNode> nodeIter = currentEntry.getValue().elements();
                    List<String> listOfT = new ArrayList<>();
                    while (nodeIter.hasNext()) {
                        listOfT.add((nodeIter.next().asText()));
                    }
                    hm.put(currentEntry.getKey(), listOfT);
                }
            }
        }
        return hm;
    }
}