6cf342f2c0fbdf86e903b8c4f4ab54626780dd53
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / ClampGraphBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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  * Modifications copyright (c) 2019 AT&T
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.util.drawing;
26
27 import java.util.HashSet;
28 import java.util.Set;
29 import org.onap.clamp.loop.template.LoopElementModel;
30 import org.onap.clamp.policy.microservice.MicroServicePolicy;
31 import org.onap.clamp.policy.operational.OperationalPolicy;
32
33 public class ClampGraphBuilder {
34     private Set<OperationalPolicy> policies = new HashSet<>();
35     private String collector;
36     private Set<MicroServicePolicy> microServices = new HashSet<>();
37     private Set<LoopElementModel> loopElementModels = new HashSet<>();
38     private final Painter painter;
39
40     public ClampGraphBuilder(Painter painter) {
41         this.painter = painter;
42     }
43
44     public ClampGraphBuilder collector(String collector) {
45         this.collector = collector;
46         return this;
47     }
48
49     public ClampGraphBuilder addPolicy(OperationalPolicy policy) {
50         this.policies.add(policy);
51         return this;
52     }
53
54     public ClampGraphBuilder addAllPolicies(Set<OperationalPolicy> policies) {
55         this.policies.addAll(policies);
56         return this;
57     }
58
59     public ClampGraphBuilder addMicroService(MicroServicePolicy ms) {
60         microServices.add(ms);
61         return this;
62     }
63
64     public ClampGraphBuilder addAllMicroServices(Set<MicroServicePolicy> msList) {
65         microServices.addAll(msList);
66         return this;
67     }
68
69     /**
70      * This method adds all loop element specified in input to the current structure.
71      *
72      * @param loopElementModels A set of LoopElementModels
73      * @return Return the current ClampGraphBuilder
74      */
75     public ClampGraphBuilder addAllLoopElementModels(Set<LoopElementModel> loopElementModels) {
76         for (LoopElementModel elem : loopElementModels) {
77             this.addLoopElementModel(elem);
78         }
79         return this;
80     }
81
82     /**
83      * This method adds one loop element specified in input to the current structure.
84      *
85      * @param loopElementModel A LoopElementModels
86      * @return Return the current ClampGraphBuilder
87      */
88     public ClampGraphBuilder addLoopElementModel(LoopElementModel loopElementModel) {
89         if (LoopElementModel.MICRO_SERVICE_TYPE.equals(loopElementModel.getLoopElementType())) {
90             microServices.add(new MicroServicePolicy(loopElementModel.getName(),
91                     loopElementModel.getPolicyModels().first(), false,null,loopElementModel,null,null));
92         } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(loopElementModel.getLoopElementType())) {
93             policies.add(new OperationalPolicy(loopElementModel.getName(), null, null,
94                     loopElementModel.getPolicyModels().first(), loopElementModel, null, null));
95         }
96         return this;
97     }
98
99     /**
100      * Build the SVG.
101      *
102      * @return Clamp graph (SVG)
103      */
104     public ClampGraph build() {
105         return new ClampGraph(painter.doPaint(collector, microServices, policies));
106     }
107 }