[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / util / Graph.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2019 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.util;
24
25 import org.onap.dmaap.dbcapi.database.DatabaseClass;
26 import org.onap.dmaap.dbcapi.model.DcaeLocation;
27 import org.onap.dmaap.dbcapi.model.MR_Client;
28 import org.onap.dmaap.dbcapi.model.MR_Cluster;
29 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
30
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36
37 public class Graph {
38         private HashMap<String, String> graph;
39         private boolean hasCentral;
40         
41         private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
42         
43         //TODO add to properties file
44         private static String centralDcaeLayerName = "central";
45
46         
47         public Graph(HashMap<String, String> graph) {
48                 super();
49                 this.graph = graph;
50         }
51
52         public Graph( List<MR_Client> clients, boolean strict ) {
53                 if ( clients == null )
54                         return;
55                 initGraph( clients, strict, "" );
56
57         }
58         public Graph( List<MR_Client> clients, boolean strict, String group ) {
59                 if ( clients == null )
60                         return;
61                 initGraph( clients, strict, group );
62         }
63         
64         private void initGraph(List<MR_Client> clients, boolean strict, String group ) {
65                 MR_ClusterService clusters = new MR_ClusterService();
66                 this.graph = new HashMap<>();
67                 this.hasCentral = false;
68                 for( MR_Client client: clients ) {
69                         if ( ! strict || client.isStatusValid()) {
70                                 String loc = client.getDcaeLocationName();
71                                 DcaeLocation dcaeLoc = locations.get(loc);
72                                 if ( dcaeLoc == null ) continue;
73                                 MR_Cluster c = clusters.getMr_ClusterByLoc(loc);
74                                 if ( group != null &&  ! group.isEmpty() && ! group.equals(c.getReplicationGroup())) continue;
75                                 
76                                 for( String action : client.getAction() ){                      
77                                         if ( ! action.equals("view") && dcaeLoc != null ) {
78                                                 String layer = dcaeLoc.getDcaeLayer();
79                                                 if ( layer != null && layer.contains(centralDcaeLayerName) ) {
80                                                         this.hasCentral = true;
81                                                 }
82                                                 graph.put(loc, layer);
83                                         }
84                                 }
85         
86                         }               
87                 }               
88         }
89         
90         public HashMap<String, String> getGraph() {
91                 return graph;
92         }
93
94         public void setGraph(HashMap<String, String> graph) {
95                 this.graph = graph;
96         }
97         
98         public String put( String key, String val ) {
99                 return graph.put(key, val);
100         }
101         
102         public String get( String key ) {
103                 return graph.get(key);
104         }
105         
106         public Collection<String> getKeys() {
107                 return graph.keySet();
108         }
109         public boolean hasCentral() {
110                 return hasCentral;
111         }
112         public void setHasCentral(boolean hasCentral) {
113                 this.hasCentral = hasCentral;
114         }
115         
116         public String getCentralLoc() {
117                 if ( ! hasCentral ) {
118                         return null;
119                 }
120                 for( String loc : graph.keySet()) {
121                         if ( graph.get(loc).contains(centralDcaeLayerName)) {
122                                 return loc;
123                         }
124                 }
125                 return null;
126         }
127         public boolean isEmpty() {
128                 return graph.isEmpty();
129         }
130         
131         
132 }