aboutsummaryrefslogtreecommitdiffstats
path: root/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ResourceAssignmentProcessor.java
blob: c1c9b93c0c30fdf7328793d1435410db78cbd09f (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
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.onap.ccsdk.config.assignment.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.onap.ccsdk.config.model.data.ResourceAssignment;
import org.onap.ccsdk.config.model.utils.TopologicalSortingUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

public class ResourceAssignmentProcessor {
    private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceAssignmentProcessor.class);
    
    private List<ResourceAssignment> assignments;
    private Map<String, ResourceAssignment> resourceAssignmentMap;
    
    @SuppressWarnings("squid:S1172")
    public ResourceAssignmentProcessor(List<ResourceAssignment> assignments, SvcLogicContext ctx) {
        this.assignments = assignments;
        this.resourceAssignmentMap = new HashMap<>();
    }
    
    @SuppressWarnings("squid:S3776")
    public List<List<ResourceAssignment>> process() {
        List<List<ResourceAssignment>> sequenceBatchResourceAssignment = new ArrayList<>();
        if (this.assignments != null) {
            logger.info("Assignments ({})", this.assignments);
            this.assignments.forEach(resourceMapping -> {
                if (resourceMapping != null) {
                    logger.trace("Processing Key ({})", resourceMapping.getName());
                    resourceAssignmentMap.put(resourceMapping.getName(), resourceMapping);
                }
            });
            
            TopologicalSortingUtils<ResourceAssignment> topologySorting = new TopologicalSortingUtils<>();
            this.resourceAssignmentMap.forEach((mappingKey, mapping) -> {
                if (mapping != null) {
                    if (mapping.getDependencies() != null && !mapping.getDependencies().isEmpty()) {
                        for (String dependency : mapping.getDependencies()) {
                            topologySorting.add(resourceAssignmentMap.get(dependency), mapping);
                        }
                    } else {
                        topologySorting.add(null, mapping);
                    }
                }
            });
            
            List<ResourceAssignment> sequencedResourceAssignments = topologySorting.topSort();
            logger.info("Sorted Sequenced Assignments ({})", sequencedResourceAssignments);
            
            List<ResourceAssignment> batchResourceAssignment = null;
            List<String> batchAssignmentName = null;
            for (int i = 0; i < sequencedResourceAssignments.size(); i++) {
                ResourceAssignment resourceAssignment = sequencedResourceAssignments.get(i);
                ResourceAssignment previousResourceAssignment = null;
                
                if (i > 0) {
                    previousResourceAssignment = sequencedResourceAssignments.get(i - 1);
                }
                if (resourceAssignment != null) {
                    
                    boolean dependencyPresence = false;
                    if (batchAssignmentName != null && resourceAssignment.getDependencies() != null) {
                        dependencyPresence =
                                CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.getDependencies());
                    }
                    
                    logger.trace("({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.getName(),
                            batchAssignmentName, resourceAssignment.getDependencies(), dependencyPresence);
                    
                    if (previousResourceAssignment != null && resourceAssignment.getDictionarySource() != null
                            && resourceAssignment.getDictionarySource()
                                    .equalsIgnoreCase(previousResourceAssignment.getDictionarySource())
                            && !dependencyPresence) {
                        batchResourceAssignment.add(resourceAssignment);
                        batchAssignmentName.add(resourceAssignment.getName());
                    } else {
                        if (batchResourceAssignment != null) {
                            sequenceBatchResourceAssignment.add(batchResourceAssignment);
                            logger.trace("Created old Set ({})", batchAssignmentName);
                        }
                        batchResourceAssignment = new ArrayList<>();
                        batchResourceAssignment.add(resourceAssignment);
                        
                        batchAssignmentName = new ArrayList<>();
                        batchAssignmentName.add(resourceAssignment.getName());
                    }
                }
                
                if (i == (sequencedResourceAssignments.size() - 1)) {
                    logger.trace("Created old Set ({})", batchAssignmentName);
                    sequenceBatchResourceAssignment.add(batchResourceAssignment);
                }
            }
            logger.info("Batched Sequence : ({})", sequenceBatchResourceAssignment);
        }
        return sequenceBatchResourceAssignment;
    }
    
}