2 * ============LICENSE_START==========================================
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
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.main;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 public class DeadlockDetectionUtil {
30 private Map<String, Node> nodeList = null;
31 public enum OwnershipType {NONE, CREATED, ACQUIRED};
33 private class Node implements Comparable<Node> {
35 private List<Node> links;
36 private boolean visited = false;
37 private boolean onStack = false;
40 public String toString() {
41 StringBuffer sb = new StringBuffer();
42 for (Node link : links)
44 return "Node [id=" + id + ", links=" + sb.toString() + ", visited=" + visited + ", onStack=" + onStack + "]";
47 public Node(String id) {
50 this.links = new ArrayList<>();
53 public List<Node> getLinks() {
57 public void addLink(Node link) {
61 public void removeLink(Node link) {
62 this.links.remove(link);
65 public boolean isVisited() {
69 public boolean isOnStack() {
73 public void setVisited(boolean visited) {
74 this.visited = visited;
77 public void setOnStack(boolean onStack) {
78 this.onStack = onStack;
82 public int compareTo(Node arg0) {
83 return id.compareTo(arg0.id);
87 public DeadlockDetectionUtil() {
88 this.nodeList = new HashMap<>();
91 public void listAllNodes() {
92 System.out.println("In DeadlockDetectionUtil: ");
93 for (String key : nodeList.keySet()) {
94 System.out.println(" " + key + " : " + nodeList.get(key));
98 public boolean checkForDeadlock(String resource, String owner, OwnershipType operation) {
99 setExisting(resource, owner, operation);
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);
108 return findCycle(currentNode);
111 private boolean findCycle(Node currentNode) {
112 if (currentNode==null)
114 if (currentNode.isOnStack())
116 if (currentNode.isVisited())
118 currentNode.setOnStack(true);
119 currentNode.setVisited(true);
120 for (Node childNode : currentNode.getLinks()) {
121 if (findCycle(childNode))
124 currentNode.setOnStack(false);
128 public void setExisting(String resource, String owner, OwnershipType operation) {
129 String resourceKey = "r" + resource;
130 Node resourceNode = nodeList.get(resourceKey);
131 if (resourceNode==null) {
132 resourceNode = new Node(resourceKey);
133 nodeList.put(resourceKey, resourceNode);
136 String ownerKey = "o" + owner;
137 Node ownerNode = nodeList.get(ownerKey);
138 if (ownerNode==null) {
139 ownerNode = new Node(ownerKey);
140 nodeList.put(ownerKey, ownerNode);
143 if (operation.equals(OwnershipType.ACQUIRED)) {
144 resourceNode.addLink(ownerNode);
145 ownerNode.removeLink(resourceNode);
146 } else if (operation.equals(OwnershipType.CREATED)) {
147 ownerNode.addLink(resourceNode);
148 resourceNode.removeLink(ownerNode);