Get policy in CsarInstaller
[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
24 package org.onap.clamp.clds.sdc.controller.installer;
25
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class ChainGenerator {
35
36     ChainGenerator() {
37     }
38
39     /**
40      * Get list of microservices chain.
41      * 
42      * @param input A set of microservices
43      * @return The list of microservice chained
44      */
45     public List<BlueprintMicroService> getChainOfMicroServices(Set<BlueprintMicroService> input) {
46         LinkedList<BlueprintMicroService> returnList = new LinkedList<>();
47         if (preValidate(input)) {
48             LinkedList<BlueprintMicroService> theList = new LinkedList<>();
49             for (BlueprintMicroService ms : input) {
50                 insertNodeTemplateIntoChain(ms, theList);
51             }
52             if (postValidate(theList)) {
53                 returnList = theList;
54             }
55         }
56         return returnList;
57     }
58
59     private boolean preValidate(Set<BlueprintMicroService> input) {
60         List<BlueprintMicroService> noInputs = input.stream().filter(ms -> "".equals(ms.getInputFrom()))
61                 .collect(Collectors.toList());
62         return noInputs.size() == 1;
63     }
64
65     private boolean postValidate(LinkedList<BlueprintMicroService> microServices) {
66         for (int i = 1; i < microServices.size() - 1; i++) {
67             BlueprintMicroService prev = microServices.get(i - 1);
68             BlueprintMicroService current = microServices.get(i);
69             if (!current.getInputFrom().equals(prev.getName())) {
70                 return false;
71             }
72         }
73         return true;
74     }
75
76     private void insertNodeTemplateIntoChain(BlueprintMicroService microServicetoInsert,
77             LinkedList<BlueprintMicroService> chainOfMicroServices) {
78         int insertIndex = 0;
79         for (int i = 0; i < chainOfMicroServices.size(); i++) {
80             BlueprintMicroService current = chainOfMicroServices.get(i);
81             if (microServicetoInsert.getName().equals(current.getInputFrom())) {
82                 insertIndex = i;
83                 break;
84             } else if (current.getName().equals(microServicetoInsert.getInputFrom())) {
85                 insertIndex = i + 1;
86                 break;
87             }
88         }
89         chainOfMicroServices.add(insertIndex, microServicetoInsert);
90     }
91 }