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<Node>();
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<String, Node>();
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 boolean cycle = findCycle(currentNode);
112 private boolean findCycle(Node currentNode) {
113 if (currentNode==null)
115 if (currentNode.isOnStack())
117 if (currentNode.isVisited())
119 currentNode.setOnStack(true);
120 currentNode.setVisited(true);
121 for (Node childNode : currentNode.getLinks()) {
122 if (findCycle(childNode))
125 currentNode.setOnStack(false);
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);
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);
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);