de72ade62fe090d915161e212f08e3753eba6ba5
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / DcaeLocationService.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.service;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.onap.dmaap.dbcapi.database.DatabaseClass;
28 import org.onap.dmaap.dbcapi.model.DcaeLocation;
29 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
30
31 public class DcaeLocationService {
32         
33         private Map<String, DcaeLocation> dcaeLocations = DatabaseClass.getDcaeLocations();
34         
35         public Map<String, DcaeLocation> getDcaeLocations() {
36                 return dcaeLocations;
37         }
38         
39         public List<DcaeLocation> getAllDcaeLocations() {
40                 return new ArrayList<DcaeLocation>(dcaeLocations.values());
41         }
42         
43         public DcaeLocation getDcaeLocation( String name ) {
44                 return dcaeLocations.get(name);
45         }
46
47         public DcaeLocation addDcaeLocation( DcaeLocation location ) {
48                 location.setLastMod();
49                 location.setStatus(DmaapObject_Status.VALID);
50                 dcaeLocations.put( location.getDcaeLocationName(), location );
51                 return location;
52         }
53         
54         public DcaeLocation updateDcaeLocation( DcaeLocation location ) {
55                 if ( location.getDcaeLocationName().isEmpty()) {
56                         return null;
57                 }
58                 location.setLastMod();
59                 dcaeLocations.put( location.getDcaeLocationName(), location );
60                 return location;
61         }
62         
63         public DcaeLocation removeDcaeLocation( String locationName ) {
64                 return dcaeLocations.remove(locationName);
65         }
66
67         public String getCentralLocation() {
68                 for( Map.Entry<String, DcaeLocation> entry: dcaeLocations.entrySet() ) {
69                         DcaeLocation loc = entry.getValue();
70                         if ( loc.isCentral() ) {
71                                 // use the name of the first central location we hit
72                                 return loc.getDcaeLocationName();
73                         }
74                         
75                 }
76                 return "aCentralLocation";  // default value that is obvious to see is wrong
77         }       
78         
79         public boolean isEdgeLocation(String aName) {
80                 DcaeLocation loc = dcaeLocations.get(aName);
81                 if ( loc == null ) {
82                         return false;
83                 }
84                 if ( ! loc.isCentral() ) {
85                                 return true;
86                 }
87                 return false;
88         }       
89
90 }