23ccca7fba392bc071fa3891a6d485d5a0e56fab
[music.git] / music-core / src / main / java / org / onap / music / main / DeadlockDetectionUtil.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2019 AT&T Intellectual Property
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.main;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 public class DeadlockDetectionUtil {
30         private Map<String, Node> nodeList = null;
31         public enum OwnershipType {NONE, CREATED, ACQUIRED};
32
33         private class Node implements Comparable<Node> {
34                 private String id;
35                 private List<Node> links;
36                 private boolean visited = false;
37                 private boolean onStack = false;
38                 
39                 @Override
40                 public String toString() {
41                         StringBuffer sb = new StringBuffer();
42                         for (Node link : links)
43                                 sb.append(link.id);
44                         return "Node [id=" + id + ", links=" + sb.toString() + ", visited=" + visited + ", onStack=" + onStack + "]";
45                 }
46
47                 public Node(String id) {
48                         super();
49                         this.id = id;
50                         this.links = new ArrayList<Node>();
51                 }
52
53                 public List<Node> getLinks() {
54                         return links;
55                 }
56
57                 public void addLink(Node link) {
58                         this.links.add(link);
59                 }
60                 
61                 public void removeLink(Node link) {
62                         this.links.remove(link);
63                 }
64
65                 public boolean isVisited() {
66                         return visited;
67                 }
68
69                 public boolean isOnStack() {
70                         return onStack;
71                 }
72
73                 public void setVisited(boolean visited) {
74                         this.visited = visited;
75                 }
76
77                 public void setOnStack(boolean onStack) {
78                         this.onStack = onStack;
79                 }
80
81                 @Override
82                 public int compareTo(Node arg0) {
83                         return id.compareTo(arg0.id);
84                 }
85         }
86         
87         public DeadlockDetectionUtil() {
88                 this.nodeList = new HashMap<String, Node>();
89         }
90
91         public void listAllNodes() {
92                 System.out.println("In DeadlockDetectionUtil: ");
93                 for (String key : nodeList.keySet()) {
94                         System.out.println("    " + key + " : " + nodeList.get(key));
95                 }
96         }
97
98         public boolean checkForDeadlock(String resource, String owner, OwnershipType operation) {
99                 setExisting(resource, owner, operation);
100
101                 Node currentNode = null;
102                 if (operation.equals(OwnershipType.ACQUIRED)) {
103                         currentNode = nodeList.get("r" + resource);
104                 } else if (operation.equals(OwnershipType.CREATED)) {
105                         currentNode = nodeList.get("o" + owner);
106                 }
107
108                 boolean cycle = findCycle(currentNode);
109                 return cycle;
110         }
111
112         private boolean findCycle(Node currentNode) {
113                 if (currentNode==null)
114                         return false;
115                 if (currentNode.isOnStack())
116                         return true;
117                 if (currentNode.isVisited())
118                         return false;
119                 currentNode.setOnStack(true);
120                 currentNode.setVisited(true);
121                 for (Node childNode : currentNode.getLinks()) {
122                         if (findCycle(childNode))
123                                 return true;
124                 }
125                 currentNode.setOnStack(false);
126                 return false;
127         }
128
129         public void setExisting(String resource, String owner, OwnershipType operation) {
130                 String resourceKey = "r" + resource;
131                 Node resourceNode = nodeList.get(resourceKey);
132                 if (resourceNode==null) {
133                         resourceNode = new Node(resourceKey);
134                         nodeList.put(resourceKey, resourceNode);
135                 }
136                 
137                 String ownerKey = "o" + owner;
138                 Node ownerNode = nodeList.get(ownerKey);
139                 if (ownerNode==null) {
140                         ownerNode = new Node(ownerKey);
141                         nodeList.put(ownerKey, ownerNode);
142                 }
143                 
144                 if (operation.equals(OwnershipType.ACQUIRED)) {
145                         resourceNode.addLink(ownerNode);
146                         ownerNode.removeLink(resourceNode);
147                 } else if (operation.equals(OwnershipType.CREATED)) {
148                         ownerNode.addLink(resourceNode);
149                         resourceNode.removeLink(ownerNode);
150                 }
151         }
152
153 }