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) sb.append(link.id);
43 return "Node [id=" + id + ", links=" + sb.toString() + ", visited=" + visited + ", onStack=" + onStack + "]";
46 public Node(String id) {
49 this.links = new ArrayList<Node>();
52 public List<Node> getLinks() {
56 public void addLink(Node link) {
60 public void removeLink(Node link) {
61 this.links.remove(link);
64 public boolean isVisited() {
68 public boolean isOnStack() {
72 public void setVisited(boolean visited) {
73 this.visited = visited;
76 public void setOnStack(boolean onStack) {
77 this.onStack = onStack;
81 public int compareTo(Node arg0) {
82 return id.compareTo(arg0.id);
86 public DeadlockDetectionUtil() {
87 this.nodeList = new HashMap<String, Node>();
90 public void listAllNodes() {
91 System.out.println("In DeadlockDetectionUtil: ");
92 for (String key : nodeList.keySet()) {
93 System.out.println(" " + key + " : " + nodeList.get(key));
97 public boolean checkForDeadlock(String resource, String owner, OwnershipType operation) {
98 setExisting(resource, owner, operation);
100 Node currentNode = null;
101 if (operation.equals(OwnershipType.ACQUIRED)) {
102 currentNode = nodeList.get("r" + resource);
103 } else if (operation.equals(OwnershipType.CREATED)) {
104 currentNode = nodeList.get("o" + owner);
107 boolean cycle = findCycle(currentNode);
111 private boolean findCycle(Node currentNode) {
112 if (currentNode==null) return false;
113 if (currentNode.isOnStack()) return true;
114 if (currentNode.isVisited()) return false;
115 currentNode.setOnStack(true);
116 currentNode.setVisited(true);
117 for (Node childNode : currentNode.getLinks()) {
118 if (findCycle(childNode)) return true;
120 currentNode.setOnStack(false);
124 public void setExisting(String resource, String owner, OwnershipType operation) {
125 String resourceKey = "r" + resource;
126 Node resourceNode = nodeList.get(resourceKey);
127 if (resourceNode==null) {
128 resourceNode = new Node(resourceKey);
129 nodeList.put(resourceKey, resourceNode);
132 String ownerKey = "o" + owner;
133 Node ownerNode = nodeList.get(ownerKey);
134 if (ownerNode==null) {
135 ownerNode = new Node(ownerKey);
136 nodeList.put(ownerKey, ownerNode);
139 if (operation.equals(OwnershipType.ACQUIRED)) {
140 resourceNode.addLink(ownerNode);
141 ownerNode.removeLink(resourceNode);
142 } else if (operation.equals(OwnershipType.CREATED)) {
143 ownerNode.addLink(resourceNode);
144 resourceNode.removeLink(ownerNode);