b05b80f0054ccf6649e83d5ec20c3c2c7b94c94a
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / ChainGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             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  *
22  */
23 package org.onap.clamp.clds.sdc.controller.installer;
24
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class ChainGenerator {
33
34     ChainGenerator() {}
35
36     List<MicroService> getChainOfMicroServices(Set<MicroService> input) {
37         LinkedList<MicroService> returnList = new LinkedList<>();
38         if(preValidate(input)) {
39             LinkedList<MicroService> theList = new LinkedList<>();
40             for (MicroService ms : input) {
41                 insertNodeTemplateIntoChain(ms, theList);
42             }
43             if(postValidate(theList)) {
44                 returnList = theList;
45             }
46         }
47         return returnList;
48     }
49
50     private boolean preValidate(Set<MicroService> input) {
51         List<MicroService> noInputs =
52             input.stream().filter(ms -> "".equals(ms.getInputFrom())).collect(Collectors.toList());
53         return noInputs.size() == 1;
54     }
55
56     private boolean postValidate(LinkedList<MicroService> microServices) {
57         for (int i = 1; i < microServices.size() - 1; i++) {
58             MicroService prev = microServices.get(i-1);
59             MicroService current = microServices.get(i);
60             if(!current.getInputFrom().equals(prev.getName())) {
61                 return false;
62             }
63         }
64         return true;
65     }
66
67     private void insertNodeTemplateIntoChain(MicroService microServicetoInsert,
68         LinkedList<MicroService> chainOfMicroServices) {
69         int insertIndex = 0;
70         for (int i = 0; i < chainOfMicroServices.size(); i++) {
71             MicroService current = chainOfMicroServices.get(i);
72             if (microServicetoInsert.getName().equals(current.getInputFrom())) {
73                 insertIndex = i;
74                 break;
75             } else if (current.getName().equals(microServicetoInsert.getInputFrom())) {
76                 insertIndex = i + 1;
77                 break;
78             }
79         }
80         chainOfMicroServices.add(insertIndex, microServicetoInsert);
81     }
82 }