2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END============================================
19 * ===================================================================
23 import React from 'react'
24 import styled from 'styled-components';
25 import { withRouter } from "react-router-dom";
26 import LoopCache from '../../../api/LoopCache';
27 import OnapConstant from '../../../utils/OnapConstants';
29 const DivStyled = styled.div`
35 const emptySvg = (<svg> <text x="20" y="40">No LOOP (SVG)</text> </svg>);
37 class SvgGenerator extends React.Component {
42 static GENERATED_FROM_INSTANCE = "INSTANCE";
43 static GENERATED_FROM_TEMPLATE = "TEMPLATE";
46 loopCache: new LoopCache({}),
48 generatedFrom: SvgGenerator.GENERATED_FROM_INSTANCE, // INSTANCE / TEMPLATE
53 this.state.loopCache = props.loopCache;
54 this.state.clickable = props.clickable;
55 this.state.generatedFrom = props.generatedFrom;
56 this.handleSvgClick = this.handleSvgClick.bind(this);
57 this.renderSvg = this.renderSvg.bind(this);
60 shouldComponentUpdate(nextProps, nextState) {
61 return this.state.loopCache !== nextState.loopCache;
64 componentWillReceiveProps(newProps) {
65 if (this.state.loopCache !== newProps.loopCache) {
67 loopCache: newProps.loopCache,
72 handleSvgClick(event) {
73 if (this.state.clickable) {
74 console.debug("svg click event received");
75 var elementName = event.target.parentNode.getAttribute('policyId');
76 console.info("SVG element clicked", elementName);
77 if (elementName !== null) {
78 this.props.history.push("/policyModal/"+event.target.parentNode.getAttribute('policyType')+"/"+elementName);
84 return this.createOneBox(xPos,null,null,'VES Collector','VES',null);
87 createOneArrow(xPos) {
89 <svg width={this.boxSpace} height={this.boxHeight} x={xPos}>
91 <marker viewBox="0 0 20 20" markerWidth="20" markerHeight="20" orient="auto" refX="8.5" refY="5" id="arrow">
92 <path d="m 1 5 l 0 -3 l 7 3 l -7 3 z"
93 stroke-width= "1" stroke-linecap= "butt" stroke-dasharray= "10000, 1"
94 fill="#000000" stroke="#000000" />
97 <line x1="0" y1="50%" x2="100%" y2="50%" stroke-width="2" color="black" stroke="black" marker-end="url(#arrow)"/>
102 createBeginCircle(xPos, text) {
104 <svg width={this.boxWidth} height={this.boxHeight} x={xPos}>
105 <circle cx={this.boxWidth-30} cy="50%" r="30" stroke-width="1" color="black" stroke="black" fill="#27ae60"/>
106 <text x={this.boxWidth-30} y="50%" text-anchor="middle" dominant-baseline="middle" textLength="20%" lengthAdjust="spacingAndGlyphs" >{text}</text>
111 createEndCircle(xPos, text) {
113 <svg width={this.boxWidth} height={this.boxHeight} x={xPos}>
114 <circle cx={30} cy="50%" r="30" stroke-width="2" color="black" stroke="black" fill="#27ae60"/>
115 <text x={30} y="50%" text-anchor="middle" dominant-baseline="middle" textLength="20%" lengthAdjust="spacingAndGlyphs" >{text}</text>
120 createOneBox(xPos, policyId, loopElementModelId , name, title, policyType) {
122 <svg width={this.boxWidth} height={this.boxHeight} x={xPos} title="test">
123 <g policyId={policyId} loopElementModelId={loopElementModelId} policyType={policyType}>
124 <rect width="100%" height="100%" stroke-width="2" color="black" stroke="black" fill="#1abc9c"/>
125 <text x="50%" y="15%" color="white" fill="white" dominant-baseline="middle" text-anchor="middle" textLength="50%" lengthAdjust="spacingAndGlyphs">{title}</text>
126 <text x="50%" y="50%" text-anchor="middle" dominant-baseline="middle" textLength="80%" lengthAdjust="spacingAndGlyphs" >{name}</text>
127 <text x="50%" y="80%" text-anchor="middle" dominant-baseline="middle" textLength="110%" lengthAdjust="spacingAndGlyphs" >{policyId}</text>
133 createSvgFromTemplate() {
134 const allElements = [];
137 allElements.push(this.createBeginCircle(xPos,"Start"))
138 xPos+=(this.boxWidth+this.boxSpace);
140 allElements.push(this.createOneArrow(xPos-this.boxSpace));
142 allElements.push(this.createVesBox(xPos));
143 xPos+=(this.boxWidth+this.boxSpace);
145 allElements.push(this.createOneArrow(xPos-this.boxSpace));
146 //createOneBox(xPos, policyId, loopElementModelId , name, title, policyType)
147 for (var loopElement of this.state.loopCache.getAllLoopElementModels()) {
149 allElements.push(this.createOneBox(xPos,
152 loopElement['shortName'],
153 loopElement['loopElementType'],
154 loopElement['loopElementType']))
155 xPos+=(this.boxWidth+this.boxSpace);
156 allElements.push(this.createOneArrow(xPos-this.boxSpace));
159 allElements.push(this.createEndCircle(xPos, "End"))
160 xPos+=(this.boxWidth+this.boxSpace);
165 createSvgFromInstance() {
166 const allElements = [];
169 allElements.push(this.createBeginCircle(xPos,"Start"))
170 xPos+=(this.boxWidth+this.boxSpace);
172 allElements.push(this.createOneArrow(xPos-this.boxSpace));
174 allElements.push(this.createVesBox(xPos));
175 xPos+=(this.boxWidth+this.boxSpace);
177 allElements.push(this.createOneArrow(xPos-this.boxSpace));
179 for (var msPolicy in this.state.loopCache.getMicroServicePolicies()) {
180 var loopElementModelName = this.state.loopCache.getMicroServicePolicies()[msPolicy]['loopElementModel'];
181 if (loopElementModelName !== undefined) {
182 loopElementModelName = loopElementModelName['name'];
184 allElements.push(this.createOneBox(xPos,
185 this.state.loopCache.getMicroServicePolicies()[msPolicy]['name'],
186 loopElementModelName,
187 this.state.loopCache.getMicroServicePolicies()[msPolicy]['policyModel']['policyAcronym'],
189 OnapConstant.microServiceType))
190 xPos+=(this.boxWidth+this.boxSpace);
191 allElements.push(this.createOneArrow(xPos-this.boxSpace));
194 for (var opPolicy in this.state.loopCache.getOperationalPolicies()) {
195 loopElementModelName = this.state.loopCache.getOperationalPolicies()[opPolicy]['loopElementModel'];
196 if (loopElementModelName !== undefined) {
197 loopElementModelName = loopElementModelName['name'];
199 allElements.push(this.createOneBox(xPos,
200 this.state.loopCache.getOperationalPolicies()[opPolicy]['name'],
201 loopElementModelName,
202 this.state.loopCache.getOperationalPolicies()[opPolicy]['policyModel']['policyAcronym'],
204 OnapConstant.operationalPolicyType))
205 xPos+=(this.boxWidth+this.boxSpace);
206 allElements.push(this.createOneArrow(xPos-this.boxSpace));
209 allElements.push(this.createEndCircle(xPos, "End"))
210 xPos+=(this.boxWidth+this.boxSpace);
216 if (this.state.loopCache.getLoopName() === undefined) {
219 if (this.state.generatedFrom === SvgGenerator.GENERATED_FROM_INSTANCE) {
220 return this.createSvgFromInstance();
221 } else if (this.state.generatedFrom === SvgGenerator.GENERATED_FROM_TEMPLATE) {
222 return this.createSvgFromTemplate();
227 var allTheElements = this.renderSvg();
228 var svgWidth = this.boxWidth*allTheElements.length;
229 var svgHeight = this.boxHeight+100;
231 <DivStyled onClick={this.handleSvgClick} >
232 <svg height={svgHeight} width={svgWidth} preserveAspectRatio="none">
240 export default withRouter(SvgGenerator);