5ec59dbafb086396edd0b63912ec3cfe185f76b6
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / Painter.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.awt.BasicStroke;
28 import java.awt.Color;
29 import java.awt.Point;
30 import java.awt.RenderingHints;
31 import java.util.Set;
32 import org.apache.batik.svggen.SVGGraphics2D;
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.onap.clamp.policy.operational.OperationalPolicy;
35
36 public class Painter {
37     private final int canvasSize;
38     private final SVGGraphics2D g2d;
39     private final DocumentBuilder documentBuilder;
40
41     private static final int DEFAULT_CANVAS_SIZE = 900;
42     private static final int SLIM_LINE = 2;
43     private static final int THICK_LINE = 4;
44     private static final double RECT_RATIO = 3.0 / 2.0;
45     private static final int CIRCLE_RADIUS = 17;
46     private static final int MINIMUM_BASE_LENGTH = 120;
47
48     /**
49      * Constructor to create instance of Painter.
50      *
51      * @param svgGraphics2D   svg graphics
52      * @param documentBuilder document builder
53      */
54     public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) {
55         this.g2d = svgGraphics2D;
56         this.documentBuilder = documentBuilder;
57         this.canvasSize = DEFAULT_CANVAS_SIZE;
58     }
59
60     DocumentBuilder doPaint(String collector, Set<MicroServicePolicy> microServices, Set<OperationalPolicy> policies) {
61         int numOfRectangles = 2 + microServices.size();
62         int numOfArrows = numOfRectangles + 1;
63         int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles);
64         if (baseLength < MINIMUM_BASE_LENGTH) {
65             baseLength = MINIMUM_BASE_LENGTH;
66         }
67         int rectHeight = (int) (baseLength / RECT_RATIO);
68
69         adjustGraphics2DProperties();
70
71         Point origin = new Point(1, rectHeight / 2);
72         ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight);
73
74         doTheActualDrawing(collector, microServices, policies, ib);
75
76         return ib.getDocumentBuilder();
77     }
78
79     private void doTheActualDrawing(String collector, Set<MicroServicePolicy> microServices,
80                                     Set<OperationalPolicy> policies,
81                                     ImageBuilder ib) {
82         ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector);
83
84         for (MicroServicePolicy ms : microServices) {
85             ib.arrow().rectangle(ms.getName(),
86                     RectTypes.MICROSERVICE, ms.getPolicyModel().getPolicyAcronym()).arrow();
87         }
88         for (OperationalPolicy policy : policies) {
89             ib.arrow().rectangle(policy.getName(), RectTypes.POLICY, policy.getPolicyModel().getPolicyAcronym())
90                     .arrow();
91         }
92         ib.circle("stop-circle", THICK_LINE);
93     }
94
95     private void adjustGraphics2DProperties() {
96         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
97         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
98         g2d.setStroke(new BasicStroke(SLIM_LINE));
99         g2d.setPaint(Color.BLACK);
100     }
101
102 }