f86569decce2a0515696e07541f429b9131d493a
[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
33
34 public class Graph {
35         private HashMap<String, String> graph;
36         private boolean hasCentral;
37         
38         private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
39         
40         //TODO add to properties file
41         private static String centralDcaeLayerName = "central";
42
43         
44         public Graph(HashMap<String, String> graph) {
45                 super();
46                 this.graph = graph;
47         }
48
49         public Graph( List<MR_Client> clients, boolean strict ) {
50                 if ( clients == null )
51                         return;
52                 this.graph = new HashMap<String, String>();
53                 this.hasCentral = false;
54                 for( MR_Client client: clients ) {
55                         if ( ! strict || client.isStatusValid()) {
56                                 String loc = client.getDcaeLocationName();
57                                 for( String action : client.getAction() ){
58                                         DcaeLocation dcaeLoc = locations.get(loc);
59                                         if ( ! action.equals("view") && dcaeLoc != null ) {
60                                                 graph.put(loc, dcaeLoc.getDcaeLayer());
61                                         }
62                                 }
63                                 String layer = graph.get(loc);
64                                 if ( layer != null && layer.contains(centralDcaeLayerName) ) {
65                                         this.hasCentral = true;
66                                 }
67         
68                         }               
69                 }               
70         }
71         
72         public HashMap<String, String> getGraph() {
73                 return graph;
74         }
75
76         public void setGraph(HashMap<String, String> graph) {
77                 this.graph = graph;
78         }
79         
80         public String put( String key, String val ) {
81                 return graph.put(key, val);
82         }
83         
84         public String get( String key ) {
85                 return graph.get(key);
86         }
87         
88         public Collection<String> getKeys() {
89                 return graph.keySet();
90         }
91         public boolean isHasCentral() {
92                 return hasCentral;
93         }
94         public void setHasCentral(boolean hasCentral) {
95                 this.hasCentral = hasCentral;
96         }
97         
98         public String getCentralLoc() {
99                 if ( ! hasCentral ) {
100                         return null;
101                 }
102                 for( String loc : graph.keySet()) {
103                         if ( graph.get(loc).contains(centralDcaeLayerName)) {
104                                 return loc;
105                         }
106                 }
107                 return null;
108         }
109         
110         
111 }