aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodeBuilder.java
blob: 41f0144c9c8339287ecbe1ccea1c84b33cb829a6 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package org.onap.vid.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.aai.AaiClientInterface;
import org.onap.vid.aai.ExceptionWithRequestInfo;
import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship;
import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList;
import org.onap.vid.model.aaiTree.AAITreeNode;
import org.onap.vid.model.aaiTree.FailureAAITreeNode;
import org.onap.vid.utils.Streams;
import org.onap.vid.utils.Tree;
import org.onap.vid.utils.Unchecked;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;
import static org.onap.vid.utils.Streams.not;


@Component
public class AAITreeNodeBuilder {

    private AaiClientInterface aaiClient;

    private final ObjectMapper mapper = new ObjectMapper();

    private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAITreeNodeBuilder.class);

    //List of all the node types the tree should include
    public static final String SERVICE_INSTANCE = "service-instance";
    public static final String GENERIC_VNF = "generic-vnf";
    public static final String NETWORK = "l3-network";
    public static final String FAILURE = "failure_node";
    public static final String COLLECTION_RESOURCE = "collection";
    public static final String CONFIGURATION = "configuration";
    public static final String PNF = "pnf";
    public static final String VF_MODULE = "vf-module";
    public static final String INSTANCE_GROUP = "instance-group";
    public static final String PORT = "l-interface";
    public static final String VG = "volume-group";
    public static final String VLAN_TAG = "vlan-tag";

    //Hashmap that defines the node-type and the tag that should be used to find it's ID key in the JSON.
    private static HashMap<String, String> nodeTypeToIdKeyMap = generateTypeToIdKeyMap();

    //Hashmap that defines the node-type and the tag that should be used to find it's NAMR key in the JSON.
    private static HashMap<String, String> nodeTypeToNameKeyMap = generateTypeToNameKeyMap();

    public enum AAIBaseProperties {
        ORCHESTRATION_STATUS("orchestration-status"),
        PROV_STATUS("prov-status"),
        IN_MAINT("in-maint"),
        MODEL_VERSION_ID("model-version-id"),
        MODEL_CUSTOMIZATION_ID("model-customization-id"),
        MODEL_INVARIANT_ID("model-invariant-id"),
        RELATIONSHIP_LIST("relationship-list");

        private final String aaiKey;

        AAIBaseProperties(String aaiKey) {
            this.aaiKey = aaiKey;
        }

        public String getAaiKey() {
            return aaiKey;
        }
    }

    public static List<AAIServiceTree.AaiRelationship> toAaiRelationshipList(String... types) {
        return Stream.of(types).map(AAIServiceTree.AaiRelationship::new).collect(Collectors.toList());
    }

    @Inject
    public AAITreeNodeBuilder(AaiClientInterface aaiClient) {
        this.aaiClient = aaiClient;
    }

    public List<AAITreeNode> buildNode(String nodeType,
                                 String requestURL,
                                 ConcurrentSkipListSet<AAITreeNode> nodesAccumulator, ExecutorService threadPool,
                                 ConcurrentLinkedQueue<String> visitedNodes,
                                 AtomicInteger nodesCounter,
                                 Tree<AAIServiceTree.AaiRelationship> pathsTree) {

        JsonNode topLevelJson = aaiClient.typedAaiGet(Unchecked.toURI(requestURL), JsonNode.class);

        if (topLevelJson.has(nodeType) && topLevelJson.get(nodeType).isArray()) {
            return Streams.fromIterable(topLevelJson.get(nodeType))
                    .map(item -> parseNodeAndGetChildren(nodeType, requestURL, item,
                            nodesAccumulator, threadPool, visitedNodes, nodesCounter, pathsTree))
                    .collect(toList());
        } else {
            return ImmutableList.of(parseNodeAndGetChildren(nodeType, requestURL, topLevelJson,
                    nodesAccumulator, threadPool, visitedNodes, nodesCounter, pathsTree));
        }
    }

    private AAITreeNode parseNodeAndGetChildren(String nodeType,
                                                String requestURL,
                                                JsonNode topLevelJson,
                                                ConcurrentSkipListSet<AAITreeNode> nodesAccumulator, ExecutorService threadPool,
                                                ConcurrentLinkedQueue<String> visitedNodes,
                                                AtomicInteger nodesCounter,
                                                Tree<AAIServiceTree.AaiRelationship> pathsTree) {
        AAITreeNode node = jsonToAaiNode(nodeType, topLevelJson, nodesAccumulator, nodesCounter);

        RelationshipList relationships = mapper.convertValue(topLevelJson.get(AAIBaseProperties.RELATIONSHIP_LIST.getAaiKey()), RelationshipList.class);
        if (relationships != null) {
            getChildren(threadPool, nodesAccumulator, relationships.getRelationship(), visitedNodes, node, nodesCounter, pathsTree);
        }
        if (StringUtils.equals(node.getType(), GENERIC_VNF)) {
            getRelatedVfModules(threadPool, nodesAccumulator, requestURL, node, nodesCounter);
        }
        return node;
    }

    private AAITreeNode jsonToAaiNode(String nodeType, JsonNode topLevelJson, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator, AtomicInteger nodesCounter) {
        AAITreeNode node = fillNodeMetaData(nodeType, topLevelJson, nodesCounter);

        nodesAccumulator.add(node);

        return node;
    }

    private void getRelatedVfModules(ExecutorService threadPool, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator, String parentURL, AAITreeNode parentNode, AtomicInteger nodesCounter) {
        /*
        VNFs do not report their direct related-to vf-modules, so try
        directly fetching a resource URI.
         */

        threadPool.execute(() -> {
            // the response is an array of vf-modules
            final JsonNode topLevelJson;
            try {
                topLevelJson = aaiClient.typedAaiGet(Unchecked.toURI(parentURL + "/vf-modules"), JsonNode.class);
            } catch (ExceptionWithRequestInfo e) {
                if (e.getHttpCode().equals(404)) {
                    // it's ok, as we're just optimistically fetching
                    // the /vf-modules uri; 404 says this time it was
                    // a bad guess
                    return;
                } else {
                    throw e;
                }
            }

            if (topLevelJson != null) {
                parentNode.getChildren().addAll(
                        Streams.fromIterable(topLevelJson.get(VF_MODULE))
                                .map(vfModuleNode -> jsonToAaiNode(VF_MODULE, vfModuleNode, nodesAccumulator, nodesCounter))
                                .collect(toList())
                );
            } else {
                LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to get vf-modules for vnf " + parentNode.getId());
            }
        });
    }

    private void getChildren(ExecutorService threadPool, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator,
                             List<Relationship> relationships, ConcurrentLinkedQueue<String> visitedNodes, AAITreeNode parent, AtomicInteger nodesCounter, Tree<AAIServiceTree.AaiRelationship> pathsTree) {
        for (Relationship relationship : relationships) {
            createChildNode(threadPool, nodesAccumulator, relationship, visitedNodes, parent, nodesCounter, pathsTree);
        }
    }

    private void createChildNode(ExecutorService threadPool, ConcurrentSkipListSet<AAITreeNode> nodesAccumulator,
                                 Relationship relationship, ConcurrentLinkedQueue<String> visitedNodes, AAITreeNode parent, AtomicInteger nodesCounter, Tree<AAIServiceTree.AaiRelationship> pathsTree) {
        String newNodeType = relationship.getRelatedTo();
        Tree<AAIServiceTree.AaiRelationship> subTree = pathsTree.getSubTree(new AAIServiceTree.AaiRelationship(newNodeType));
        if (subTree!=null) {
            String newNodeUrl = relationship.getRelatedLink();
            if (!visitedNodes.contains(newNodeUrl)) {
                visitedNodes.add(newNodeUrl);
                threadPool.execute(() -> {
                            try {
                                parent.addChildren(buildNode(newNodeType, newNodeUrl, nodesAccumulator, threadPool, visitedNodes, nodesCounter,  subTree));
                            } catch (Exception e) {
                                parent.getChildren().add(createFailureNode(e));
                            }
                        }
                );
            }
        }
    }

    private AAITreeNode fillNodeMetaData(String nodeType, JsonNode model, @NotNull AtomicInteger nodesCounter) {
        AAITreeNode node = new AAITreeNode();
        node.setType(nodeType);
        node.setUniqueNumber(nodesCounter.getAndIncrement());
        node.setOrchestrationStatus(getStringDataFromJsonIfExists(model, AAIBaseProperties.ORCHESTRATION_STATUS.getAaiKey()));
        node.setProvStatus(getStringDataFromJsonIfExists(model, AAIBaseProperties.PROV_STATUS.getAaiKey()));
        node.setInMaint(getBooleanDataFromJsonIfExists(model, AAIBaseProperties.IN_MAINT.getAaiKey()));
        node.setModelVersionId(getStringDataFromJsonIfExists(model, AAIBaseProperties.MODEL_VERSION_ID.getAaiKey()));
        node.setModelCustomizationId(getStringDataFromJsonIfExists(model, AAIBaseProperties.MODEL_CUSTOMIZATION_ID.getAaiKey()));
        node.setModelInvariantId(getStringDataFromJsonIfExists(model, AAIBaseProperties.MODEL_INVARIANT_ID.getAaiKey()));
        node.setId(getStringDataFromJsonIfExists(model, nodeTypeToIdKeyMap.get(nodeType)));
        node.setName(getStringDataFromJsonIfExists(model, nodeTypeToNameKeyMap.get(nodeType)));
        node.setAdditionalProperties(aggregateAllOtherProperties(model, nodeType));

        return node;
    }

    private AAITreeNode createFailureNode(Exception exception) {
        return FailureAAITreeNode.of(exception);
    }

    private String getStringDataFromJsonIfExists(JsonNode model, String key) {
        if (model.has(key)) {
            return model.get(key).asText();
        }
        return null;
    }

    private Boolean getBooleanDataFromJsonIfExists(JsonNode model, String key) {
        if (model.has(key)) {
            return model.get(key).asBoolean();
        }
        return false;
    }

    private Map<String, Object> aggregateAllOtherProperties(JsonNode model, String nodeType) {
        Set<String> ignoreProperties = Stream.of(AAIBaseProperties.values())
                .map(AAIBaseProperties::getAaiKey).collect(toSet());

        return Streams.fromIterator(model.fields())
                .filter(not(field -> StringUtils.equals(field.getKey(), nodeTypeToIdKeyMap.get(nodeType))))
                .filter(not(field -> StringUtils.equals(field.getKey(), nodeTypeToNameKeyMap.get(nodeType))))
                .filter(not(field -> ignoreProperties.contains(field.getKey())))
                .collect(toMap(Map.Entry::getKey, v -> v.getValue().asText()));
    }

    private static HashMap<String, String> generateTypeToIdKeyMap() {
        HashMap<String, String> result = new HashMap<>();
        result.put(SERVICE_INSTANCE, "service-instance-id");
        result.put(GENERIC_VNF, "vnf-id");
        result.put(NETWORK, "network-id");
        result.put(COLLECTION_RESOURCE, "collection-id");
        result.put(CONFIGURATION, "configuration-id");
        result.put(PNF, "pnf-id");
        result.put(VF_MODULE, "vf-module-id");
        result.put(INSTANCE_GROUP, "id");
        result.put(PORT, "l-interface-id");
        result.put(VG, "volume-group-id");
        result.put(VLAN_TAG, "vlan-id");

        return result;
    }

    private static HashMap<String, String> generateTypeToNameKeyMap() {
        HashMap<String, String> result = new HashMap<>();
        result.put(SERVICE_INSTANCE, "service-instance-name");
        result.put(GENERIC_VNF, "vnf-name");
        result.put(NETWORK, "network-name");
        result.put(COLLECTION_RESOURCE, "collection-name");
        result.put(CONFIGURATION, "configuration-name");
        result.put(PNF, "pnf-name");
        result.put(VF_MODULE, "vf-module-name");
        result.put(INSTANCE_GROUP, "instance-group-name");
        result.put(PORT, "l-interface-name");
        result.put(VG, "volume-group-name");
        result.put(VLAN_TAG, "vlan-name");

        return result;
    }
}