Changed to unmaintained
[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
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;
46
47 public class CapabilitiesDataExtractor {
48
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(CapabilitiesDataExtractor.class);
50
51     private final FlowControlDBService dbService;
52     private final ObjectMapper mapper;
53
54     public CapabilitiesDataExtractor() {
55         this(FlowControlDBService.initialise());
56     }
57
58     /**
59      * Ctor for tests, prefer to use default one
60      */
61     public CapabilitiesDataExtractor(FlowControlDBService dbService) {
62         this.dbService = dbService;
63
64         mapper = new ObjectMapper();
65         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66         mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
67     }
68
69     Capabilities getCapabilitiesData(SvcLogicContext ctx) throws SvcLogicException, IOException {
70
71         String fn = "FlowExecutorNode.getCapabilitiesData";
72         String capabilitiesData = dbService.getCapabilitiesData(ctx);
73         log.info(fn + ":capabilitiesDataInput:" + capabilitiesData);
74
75         Capabilities capabilities = new Capabilities();
76         if (StringUtils.isBlank(capabilitiesData)) {
77             return capabilities;
78         }
79
80         JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
81         JsonNode capNode = capabilitiesNode.get("capabilities");
82         log.info("capabilitiesNode:" + capabilitiesNode.toString());
83
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));
88
89         log.info("Capabilities Output:" + capabilities.toString());
90
91         return capabilities;
92     }
93
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<>();
98         }
99         return mapper.readValue(parameterNode.toString(), new TypeReference<List<String>>() {
100         });
101     }
102
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()) {
107             return hm;
108         }
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()));
118                     }
119                     hm.put(currentEntry.getKey(), listOfT);
120                 }
121             }
122         }
123         return hm;
124     }
125 }