2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.appc.flow.controller.node;
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;
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;
40 import java.util.Map.Entry;
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;
48 public class CapabilitiesDataExtractor {
50 private static final EELFLogger log = EELFManager.getInstance().getLogger(CapabilitiesDataExtractor.class);
52 private final FlowControlDBService dbService;
53 private final ObjectMapper mapper;
55 public CapabilitiesDataExtractor() {
56 this(FlowControlDBService.initialise());
60 * Ctor for tests, prefer to use default one
62 public CapabilitiesDataExtractor(FlowControlDBService dbService) {
63 this.dbService = dbService;
65 mapper = new ObjectMapper();
66 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
67 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
70 Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {
72 String fn = "FlowExecutorNode.getCapabilitiesData";
73 String capabilitiesData = dbService.getCapabilitiesData(ctx);
74 log.info(fn + ":capabilitiesDataInput:" + capabilitiesData);
76 Capabilities capabilities = new Capabilities();
77 if (StringUtils.isBlank(capabilitiesData)) {
81 JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
82 JsonNode capNode = capabilitiesNode.get("capabilities");
83 log.info("capabilitiesNode:" + capabilitiesNode.toString());
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));
90 log.info("Capabilities Output:" + capabilities.toString());
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<>();
100 return mapper.readValue(parameterNode.toString(), new TypeReference<List<String>>() {
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()) {
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()));
120 hm.put(currentEntry.getKey(), listOfT);