Merge "adding save for topology template content"
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / designer / graph.generator.util.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software 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 import {TopologyTemplate} from './model/designer.topologyTemplate.model';
22 import {Injectable} from '@angular/core';
23 import {GraphUtil} from './graph.util';
24
25 @Injectable({
26     providedIn: 'root'
27 })
28 export class GraphGenerator {
29
30     constructor(private graphUtil: GraphUtil) {
31     }
32
33     clear(boardGraph: joint.dia.Graph) {
34         boardGraph.clear();
35     }
36
37     /**
38      * loops over workflows
39      * create action element
40      * from steps --> create function element
41      * add function element to action element
42      * example toplogyTemplate
43      *
44      * {
45      * "workflows": {
46      *   "Action1": {
47      *       "steps": {
48      *           "STEP_NAME": {
49      *               "target": "NODE_TEMPLATE_NAME",
50      *                   "description": ""
51      *           }
52      *       }
53      *   }
54      * },
55      * "node_templates": {
56      *    "NODE_TEMPLATE_NAME": {
57      *        "type": "dg-generic",
58      *            "properties": {
59      *            "dependency-node-templates": [
60      *                "component-config-snapshots-executor",
61      *                "component-jython-executor"
62      *            ]
63      *        }
64      *    },
65      *    "component-config-snapshots-executor": {
66      *        "type": "component-config-snapshots-executor",
67      *            "properties": { }
68      *    },
69      *    "component-jython-executor": {
70      *        "type": "component-jython-executor",
71      *            "properties": { }
72      *    }
73      * }
74      * }
75      */
76
77     public populate(topologyTempalte: TopologyTemplate,
78                     boardGraph: joint.dia.Graph) {
79
80         Object.keys(topologyTempalte.workflows).forEach(workFlowName => {
81             console.log('drawing workflow item --> ', workFlowName);
82
83             // create action element
84             const actionElement =
85                 this.graphUtil.createCustomActionWithName(workFlowName, boardGraph);
86
87             // create board function elements
88             const workflow = topologyTempalte.workflows[workFlowName].steps;
89             const stepName = Object.keys(workflow)[0];
90             if (stepName) {
91                 const nodeTemplateName = workflow[stepName].target;
92                 const functionType = topologyTempalte.node_templates[nodeTemplateName].type;
93                 console.log('draw function with ', stepName, functionType);
94
95                 this.graphUtil.dropFunctionOverActionRelativeToParent(
96                     actionElement,
97                     stepName, functionType, boardGraph);
98
99                 // TODO handle dg-generic case (multi-step in the same action)
100                 if (functionType === 'dg-generic') {
101                     const props = topologyTempalte.node_templates[nodeTemplateName].properties;
102                     console.log('dg props', props);
103                     props['dependency-node-templates'].forEach(dependencyStepName => {
104                         const dependencyType = topologyTempalte.node_templates[dependencyStepName].type;
105                         console.log('dependencyType', dependencyType);
106                         this.graphUtil.dropFunctionOverActionRelativeToParent(
107                             actionElement,
108                             dependencyStepName, dependencyType, boardGraph);
109
110                     });
111                 }
112             }
113         });
114
115     }
116 }