a98d3268edda9d15472225735d809d4e09b55990
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / CapabilitiesDataExtractor.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2019 AT&T intellectual property. All rights reserved.
7  * =============================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.flow.controller.node;
22
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VF_MODULE;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VM;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.core.type.TypeReference;
31 import com.fasterxml.jackson.databind.DeserializationFeature;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 import org.apache.commons.lang3.StringUtils;
43 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
44 import org.onap.appc.flow.controller.interfaceData.Capabilities;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47
48 public class CapabilitiesDataExtractor {
49
50     private static final EELFLogger log = EELFManager.getInstance().getLogger(CapabilitiesDataExtractor.class);
51
52     private final FlowControlDBService dbService;
53     private final ObjectMapper mapper;
54
55     public CapabilitiesDataExtractor() {
56         this(FlowControlDBService.initialise());
57     }
58
59     /**
60      * Ctor for tests, prefer to use default one
61      */
62     public CapabilitiesDataExtractor(FlowControlDBService dbService) {
63         this.dbService = dbService;
64
65         mapper = new ObjectMapper();
66         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
67         mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
68     }
69
70     Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {
71
72         String fn = "FlowExecutorNode.getCapabilitiesData";
73         String capabilitiesData = dbService.getCapabilitiesData(ctx);
74         log.info(fn + ":capabilitiesDataInput:" + capabilitiesData);
75
76         Capabilities capabilities = new Capabilities();
77         if (StringUtils.isBlank(capabilitiesData)) {
78             return capabilities;
79         }
80
81         JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
82         JsonNode capNode = capabilitiesNode.get("capabilities");
83         log.info("capabilitiesNode:" + capabilitiesNode.toString());
84
85         capabilities.getVfModule().addAll(extractParameterList(capNode, VF_MODULE));
86         capabilities.getVnfc().addAll(extractParameterList(capNode, VNFC));
87         capabilities.getVnf().addAll(extractParameterList(capNode, VNF));
88         capabilities.getVm().putAll(extractParameterMap(capNode, VM));
89
90         log.info("Capabilities Output:" + capabilities.toString());
91
92         return capabilities;
93     }
94
95     private List<String> extractParameterList(JsonNode root, String parameter) throws IOException {
96         JsonNode parameterNode = root.get(parameter);
97         if (parameterNode == null) {
98             return new ArrayList<>();
99         }
100         return mapper.readValue(parameterNode.toString(), new TypeReference<List<String>>() {
101         });
102     }
103
104     private HashMap<String, List<String>> extractParameterMap(JsonNode root, String parameter) throws IOException {
105         JsonNode parameterNode = root.get(parameter);
106         HashMap<String, List<String>> hm = new HashMap<>();
107         if (parameterNode == null || !parameterNode.isArray()) {
108             return hm;
109         }
110         for (JsonNode n : parameterNode) {
111             Iterator<Map.Entry<String, JsonNode>> fldIter = n.fields();
112             while (fldIter.hasNext()) {
113                 Map.Entry<String, JsonNode> currentEntry = fldIter.next();
114                 if (currentEntry.getValue().isArray()) {
115                     Iterator<JsonNode> nodeIter = currentEntry.getValue().elements();
116                     List<String> listOfT = new ArrayList<>();
117                     while (nodeIter.hasNext()) {
118                         listOfT.add((nodeIter.next().asText()));
119                     }
120                     hm.put(currentEntry.getKey(), listOfT);
121                 }
122             }
123         }
124         return hm;
125     }
126 }