Alternative MR replication method
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.util;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.onap.dmaap.dbcapi.database.DatabaseClass;
30 import org.onap.dmaap.dbcapi.model.DcaeLocation;
31 import org.onap.dmaap.dbcapi.model.MR_Client;
32 import org.onap.dmaap.dbcapi.model.MR_Cluster;
33 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
34
35
36 public class Graph {
37         private HashMap<String, String> graph;
38         private boolean hasCentral;
39         
40         private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
41         
42         //TODO add to properties file
43         private static String centralDcaeLayerName = "central";
44
45         
46         public Graph(HashMap<String, String> graph) {
47                 super();
48                 this.graph = graph;
49         }
50
51         public Graph( List<MR_Client> clients, boolean strict ) {
52                 if ( clients == null )
53                         return;
54                 initGraph( clients, strict, "" );
55                 return;
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                 return;
63         }
64         
65         private void initGraph(List<MR_Client> clients, boolean strict, String group ) {
66                 MR_ClusterService clusters = new MR_ClusterService();
67                 this.graph = new HashMap<String, String>();
68                 this.hasCentral = false;
69                 for( MR_Client client: clients ) {
70                         if ( ! strict || client.isStatusValid()) {
71                                 String loc = client.getDcaeLocationName();
72                                 DcaeLocation dcaeLoc = locations.get(loc);
73                                 if ( dcaeLoc == null ) continue;
74                                 MR_Cluster c = clusters.getMr_ClusterByLoc(loc);
75                                 if ( group != null &&  ! group.isEmpty() && ! group.equals(c.getReplicationGroup())) continue;
76                                 
77                                 for( String action : client.getAction() ){                      
78                                         if ( ! action.equals("view") && dcaeLoc != null ) {
79                                                 String layer = dcaeLoc.getDcaeLayer();
80                                                 if ( layer != null && layer.contains(centralDcaeLayerName) ) {
81                                                         this.hasCentral = true;
82                                                 }
83                                                 graph.put(loc, layer);
84                                         }
85                                 }
86         
87                         }               
88                 }               
89         }
90         
91         public HashMap<String, String> getGraph() {
92                 return graph;
93         }
94
95         public void setGraph(HashMap<String, String> graph) {
96                 this.graph = graph;
97         }
98         
99         public String put( String key, String val ) {
100                 return graph.put(key, val);
101         }
102         
103         public String get( String key ) {
104                 return graph.get(key);
105         }
106         
107         public Collection<String> getKeys() {
108                 return graph.keySet();
109         }
110         public boolean hasCentral() {
111                 return hasCentral;
112         }
113         public void setHasCentral(boolean hasCentral) {
114                 this.hasCentral = hasCentral;
115         }
116         
117         public String getCentralLoc() {
118                 if ( ! hasCentral ) {
119                         return null;
120                 }
121                 for( String loc : graph.keySet()) {
122                         if ( graph.get(loc).contains(centralDcaeLayerName)) {
123                                 return loc;
124                         }
125                 }
126                 return null;
127         }
128         public boolean isEmpty() {
129                 return graph.isEmpty();
130         }
131         
132         
133 }