700c77f444af7589f263fed07edc40590e226635
[dmaap/dbcapi.git] / 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                 return;
57
58         }
59         public Graph( List<MR_Client> clients, boolean strict, String group ) {
60                 if ( clients == null )
61                         return;
62                 initGraph( clients, strict, group );
63                 return;
64         }
65         
66         private void initGraph(List<MR_Client> clients, boolean strict, String group ) {
67                 MR_ClusterService clusters = new MR_ClusterService();
68                 this.graph = new HashMap<String, String>();
69                 this.hasCentral = false;
70                 for( MR_Client client: clients ) {
71                         if ( ! strict || client.isStatusValid()) {
72                                 String loc = client.getDcaeLocationName();
73                                 DcaeLocation dcaeLoc = locations.get(loc);
74                                 if ( dcaeLoc == null ) continue;
75                                 MR_Cluster c = clusters.getMr_ClusterByLoc(loc);
76                                 if ( group != null &&  ! group.isEmpty() && ! group.equals(c.getReplicationGroup())) continue;
77                                 
78                                 for( String action : client.getAction() ){                      
79                                         if ( ! action.equals("view") && dcaeLoc != null ) {
80                                                 String layer = dcaeLoc.getDcaeLayer();
81                                                 if ( layer != null && layer.contains(centralDcaeLayerName) ) {
82                                                         this.hasCentral = true;
83                                                 }
84                                                 graph.put(loc, layer);
85                                         }
86                                 }
87         
88                         }               
89                 }               
90         }
91         
92         public HashMap<String, String> getGraph() {
93                 return graph;
94         }
95
96         public void setGraph(HashMap<String, String> graph) {
97                 this.graph = graph;
98         }
99         
100         public String put( String key, String val ) {
101                 return graph.put(key, val);
102         }
103         
104         public String get( String key ) {
105                 return graph.get(key);
106         }
107         
108         public Collection<String> getKeys() {
109                 return graph.keySet();
110         }
111         public boolean hasCentral() {
112                 return hasCentral;
113         }
114         public void setHasCentral(boolean hasCentral) {
115                 this.hasCentral = hasCentral;
116         }
117         
118         public String getCentralLoc() {
119                 if ( ! hasCentral ) {
120                         return null;
121                 }
122                 for( String loc : graph.keySet()) {
123                         if ( graph.get(loc).contains(centralDcaeLayerName)) {
124                                 return loc;
125                         }
126                 }
127                 return null;
128         }
129         public boolean isEmpty() {
130                 return graph.isEmpty();
131         }
132         
133         
134 }