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;
41 import org.apache.commons.lang3.StringUtils;
42 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
43 import org.onap.appc.flow.controller.interfaceData.Capabilities;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 public class CapabilitiesDataExtractor {
49 private static final EELFLogger log = EELFManager.getInstance().getLogger(CapabilitiesDataExtractor.class);
51 private final FlowControlDBService dbService;
52 private final ObjectMapper mapper;
54 public CapabilitiesDataExtractor() {
55 this(FlowControlDBService.initialise());
59 * Ctor for tests, prefer to use default one
61 public CapabilitiesDataExtractor(FlowControlDBService dbService) {
62 this.dbService = dbService;
64 mapper = new ObjectMapper();
65 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
69 Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {
71 String fn = "FlowExecutorNode.getCapabilitiesData";
72 String capabilitiesData = dbService.getCapabilitiesData(ctx);
73 log.info(fn + ":capabilitiesDataInput:" + capabilitiesData);
75 Capabilities capabilities = new Capabilities();
76 if (StringUtils.isBlank(capabilitiesData)) {
80 JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
81 JsonNode capNode = capabilitiesNode.get("capabilities");
82 log.info("capabilitiesNode:" + capabilitiesNode.toString());
84 capabilities.getVfModule().addAll(extractParameterList(capNode, VF_MODULE));
85 capabilities.getVnfc().addAll(extractParameterList(capNode, VNFC));
86 capabilities.getVnf().addAll(extractParameterList(capNode, VNF));
87 capabilities.getVm().putAll(extractParameterMap(capNode, VM));
89 log.info("Capabilities Output:" + capabilities.toString());
94 private List<String> extractParameterList(JsonNode root, String parameter) throws IOException {
95 JsonNode parameterNode = root.get(parameter);
96 if (parameterNode == null) {
97 return new ArrayList<>();
99 return mapper.readValue(parameterNode.toString(), new TypeReference<List<String>>() {
103 private HashMap<String, List<String>> extractParameterMap(JsonNode root, String parameter) throws IOException {
104 JsonNode parameterNode = root.get(parameter);
105 HashMap<String, List<String>> hm = new HashMap<>();
106 if (parameterNode == null || !parameterNode.isArray()) {
109 for (JsonNode n : parameterNode) {
110 Iterator<Map.Entry<String, JsonNode>> fldIter = n.fields();
111 while (fldIter.hasNext()) {
112 Map.Entry<String, JsonNode> currentEntry = fldIter.next();
113 if (currentEntry.getValue().isArray()) {
114 Iterator<JsonNode> nodeIter = currentEntry.getValue().elements();
115 List<String> listOfT = new ArrayList<>();
116 while (nodeIter.hasNext()) {
117 listOfT.add((nodeIter.next().asText()));
119 hm.put(currentEntry.getKey(), listOfT);