c1c9b93c0c30fdf7328793d1435410db78cbd09f
[ccsdk/features.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
5  * in compliance with the License. You may obtain a copy of the License at\r
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License\r
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
11  * or implied. See the License for the specific language governing permissions and limitations under\r
12  * the License.\r
13  */\r
14 \r
15 package org.onap.ccsdk.config.assignment.processor;\r
16 \r
17 import java.util.ArrayList;\r
18 import java.util.HashMap;\r
19 import java.util.List;\r
20 import java.util.Map;\r
21 import org.apache.commons.collections.CollectionUtils;\r
22 import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
23 import org.onap.ccsdk.config.model.utils.TopologicalSortingUtils;\r
24 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
25 import com.att.eelf.configuration.EELFLogger;\r
26 import com.att.eelf.configuration.EELFManager;\r
27 \r
28 public class ResourceAssignmentProcessor {\r
29     private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceAssignmentProcessor.class);\r
30     \r
31     private List<ResourceAssignment> assignments;\r
32     private Map<String, ResourceAssignment> resourceAssignmentMap;\r
33     \r
34     @SuppressWarnings("squid:S1172")\r
35     public ResourceAssignmentProcessor(List<ResourceAssignment> assignments, SvcLogicContext ctx) {\r
36         this.assignments = assignments;\r
37         this.resourceAssignmentMap = new HashMap<>();\r
38     }\r
39     \r
40     @SuppressWarnings("squid:S3776")\r
41     public List<List<ResourceAssignment>> process() {\r
42         List<List<ResourceAssignment>> sequenceBatchResourceAssignment = new ArrayList<>();\r
43         if (this.assignments != null) {\r
44             logger.info("Assignments ({})", this.assignments);\r
45             this.assignments.forEach(resourceMapping -> {\r
46                 if (resourceMapping != null) {\r
47                     logger.trace("Processing Key ({})", resourceMapping.getName());\r
48                     resourceAssignmentMap.put(resourceMapping.getName(), resourceMapping);\r
49                 }\r
50             });\r
51             \r
52             TopologicalSortingUtils<ResourceAssignment> topologySorting = new TopologicalSortingUtils<>();\r
53             this.resourceAssignmentMap.forEach((mappingKey, mapping) -> {\r
54                 if (mapping != null) {\r
55                     if (mapping.getDependencies() != null && !mapping.getDependencies().isEmpty()) {\r
56                         for (String dependency : mapping.getDependencies()) {\r
57                             topologySorting.add(resourceAssignmentMap.get(dependency), mapping);\r
58                         }\r
59                     } else {\r
60                         topologySorting.add(null, mapping);\r
61                     }\r
62                 }\r
63             });\r
64             \r
65             List<ResourceAssignment> sequencedResourceAssignments = topologySorting.topSort();\r
66             logger.info("Sorted Sequenced Assignments ({})", sequencedResourceAssignments);\r
67             \r
68             List<ResourceAssignment> batchResourceAssignment = null;\r
69             List<String> batchAssignmentName = null;\r
70             for (int i = 0; i < sequencedResourceAssignments.size(); i++) {\r
71                 ResourceAssignment resourceAssignment = sequencedResourceAssignments.get(i);\r
72                 ResourceAssignment previousResourceAssignment = null;\r
73                 \r
74                 if (i > 0) {\r
75                     previousResourceAssignment = sequencedResourceAssignments.get(i - 1);\r
76                 }\r
77                 if (resourceAssignment != null) {\r
78                     \r
79                     boolean dependencyPresence = false;\r
80                     if (batchAssignmentName != null && resourceAssignment.getDependencies() != null) {\r
81                         dependencyPresence =\r
82                                 CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.getDependencies());\r
83                     }\r
84                     \r
85                     logger.trace("({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.getName(),\r
86                             batchAssignmentName, resourceAssignment.getDependencies(), dependencyPresence);\r
87                     \r
88                     if (previousResourceAssignment != null && resourceAssignment.getDictionarySource() != null\r
89                             && resourceAssignment.getDictionarySource()\r
90                                     .equalsIgnoreCase(previousResourceAssignment.getDictionarySource())\r
91                             && !dependencyPresence) {\r
92                         batchResourceAssignment.add(resourceAssignment);\r
93                         batchAssignmentName.add(resourceAssignment.getName());\r
94                     } else {\r
95                         if (batchResourceAssignment != null) {\r
96                             sequenceBatchResourceAssignment.add(batchResourceAssignment);\r
97                             logger.trace("Created old Set ({})", batchAssignmentName);\r
98                         }\r
99                         batchResourceAssignment = new ArrayList<>();\r
100                         batchResourceAssignment.add(resourceAssignment);\r
101                         \r
102                         batchAssignmentName = new ArrayList<>();\r
103                         batchAssignmentName.add(resourceAssignment.getName());\r
104                     }\r
105                 }\r
106                 \r
107                 if (i == (sequencedResourceAssignments.size() - 1)) {\r
108                     logger.trace("Created old Set ({})", batchAssignmentName);\r
109                     sequenceBatchResourceAssignment.add(batchResourceAssignment);\r
110                 }\r
111             }\r
112             logger.info("Batched Sequence : ({})", sequenceBatchResourceAssignment);\r
113         }\r
114         return sequenceBatchResourceAssignment;\r
115     }\r
116     \r
117 }\r