fc796c9eda6e516a187acfca4d70177a5b0d4011
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / data / BluePrintGraph.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.data
18
19 enum class EdgeLabel(val id: String) {
20     SUCCESS("success"),
21     FAILURE("failure"),
22     DEFAULT("*")
23 }
24
25 enum class EdgeStatus(val id: String) {
26     NOT_STARTED("not_started"),
27     EXECUTED("executed"),
28     SKIPPED("skipped")
29 }
30
31 enum class NodeStatus(val id: String) {
32     NOT_STARTED("not_started"),
33     READY("ready"),
34     EXECUTING("executing"),
35     EXECUTED("executed"),
36     SKIPPED("skipped"),
37     TERMINATED("terminated")
38 }
39
40 class Graph {
41     val nodes: MutableMap<String, Node> = hashMapOf()
42     val edges: MutableSet<Edge> = mutableSetOf()
43
44     fun addNode(value: String): Node {
45         val node = Node(value)
46         nodes[value] = node
47         return node
48     }
49
50     fun addEdge(source: String, destination: String, label: EdgeLabel) {
51         if (!nodes.containsKey(source)) {
52             addNode(source)
53         }
54         if (!nodes.containsKey(destination)) {
55             addNode(destination)
56         }
57         val edge = Edge(nodes[source]!!, nodes[destination]!!, label)
58         if (!edges.contains(edge)) {
59             edges.add(edge)
60             nodes[source]!!.edges.add(edge)
61         }
62     }
63
64     override fun toString(): String {
65         val standaloneNodes = nodes.values.filter { node -> edges.all { it.source != node && it.target != node } }
66         val s = (edges.map { it.toString() } + standaloneNodes.map { it.toString() }).joinToString()
67         return "[$s]"
68     }
69
70     fun print(): String {
71         val buffer = StringBuffer("Nodes :")
72         nodes.values.forEach {
73             buffer.append("\n\t$it")
74         }
75         buffer.append("\nEdges :")
76         edges.forEach {
77             buffer.append("\n\t$it")
78         }
79         return buffer.toString()
80     }
81
82     override fun equals(other: Any?): Boolean {
83         if (this === other) return true
84         if (other?.javaClass != javaClass) return false
85         other as Graph
86         return nodes == other.nodes && edges == other.edges
87     }
88
89     override fun hashCode() = 31 * nodes.hashCode() + edges.hashCode()
90
91     fun equivalentTo(other: Graph): Boolean {
92         return nodes == other.nodes && edges.all { edge -> other.edges.any { it.equivalentTo(edge) } }
93     }
94
95     data class Node(val id: String, var status: NodeStatus = NodeStatus.NOT_STARTED) {
96         val edges: MutableList<Edge> = ArrayList()
97
98         fun neighbors(): List<Node> = edges.map { edge -> edge.target(this) }
99
100         fun neighbors(label: EdgeLabel): List<Node> = edges.filter { it.label == label }
101                 .map { edge -> edge.target(this) }
102
103         fun labelEdges(label: EdgeLabel): List<Edge> = edges.filter { it.label == label }
104
105         override fun toString() = "$id, Status($status)"
106     }
107
108     data class Edge(
109             val source: Node,
110             val target: Node,
111             val label: EdgeLabel,
112             var status: EdgeStatus = EdgeStatus.NOT_STARTED) {
113
114         fun target(node: Node): Node = target
115
116         fun equivalentTo(other: Edge) =
117                 (source == other.source && target == other.target)
118                         || (source == other.target && target == other.source)
119
120         override fun toString() =
121                 "${source.id}>${target.id}/$label($status)"
122     }
123
124     data class TermForm(val nodes: Collection<String>, val edges: List<Term>) {
125
126         data class Term(val source: String, val target: String, val label: EdgeLabel) {
127             override fun toString() = "Term($source, $target, $label)"
128         }
129     }
130
131     data class AdjacencyList<String, out EdgeLabel>(val entries: List<Entry<String, EdgeLabel>>) {
132         constructor(vararg entries: Entry<String, EdgeLabel>) : this(entries.asList())
133
134         override fun toString() = "AdjacencyList(${entries.joinToString()})"
135
136         data class Entry<out String, out EdgeLabel>(val node: String, val links: List<Link<String, EdgeLabel>> = emptyList<Nothing>()) {
137             constructor(node: String, vararg links: Link<String, EdgeLabel>) : this(node, links.asList())
138
139             override fun toString() = "Entry($node, links[${links.joinToString()}])"
140         }
141
142         data class Link<out String, out EdgeLabel>(val node: String, val label: EdgeLabel) {
143             override fun toString() = if (label == null) "$node" else "$node/$label"
144         }
145     }
146
147     companion object {
148
149         fun labeledDirectedTerms(termForm: TermForm): Graph =
150                 createFromTerms(termForm) { graph, n1, n2, value -> graph.addEdge(n1, n2, value) }
151
152         fun labeledDirectedAdjacent(adjacencyList: AdjacencyList<String, EdgeLabel>): Graph =
153                 fromAdjacencyList(adjacencyList) { graph, n1, n2, value ->
154                     graph.addEdge(n1, n2, value)
155                 }
156
157         private fun createFromTerms(termForm: TermForm,
158                                     addFunction: (Graph, String, String, EdgeLabel) -> Unit): Graph {
159             val graph = Graph()
160             termForm.nodes.forEach { graph.addNode(it) }
161             termForm.edges.forEach { addFunction(graph, it.source, it.target, it.label) }
162             return graph
163         }
164
165         private fun fromAdjacencyList(adjacencyList: AdjacencyList<String, EdgeLabel>,
166                                       addFunction: (Graph, String, String, EdgeLabel) -> Unit): Graph {
167             val graph = Graph()
168             adjacencyList.entries.forEach { graph.addNode(it.node) }
169             adjacencyList.entries.forEach { (node, links) ->
170                 links.forEach { addFunction(graph, node, it.node, it.label) }
171             }
172             return graph
173         }
174     }
175 }