[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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 org.onap.dmaap.dbcapi.database.DatabaseClass;
24 import org.onap.dmaap.dbcapi.model.DcaeLocation;
25 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31
32 public class DcaeLocationService {
33
34     private static final String DEFAULT_CENTRAL_LOCATION = "aCentralLocation"; // default value that is obvious to see is wrong
35     private final Map<String, DcaeLocation> dcaeLocations;
36
37     public DcaeLocationService() {
38         this(DatabaseClass.getDcaeLocations());
39     }
40
41     DcaeLocationService(Map<String, DcaeLocation> dcaeLocations) {
42         this.dcaeLocations = dcaeLocations;
43     }
44
45     public List<DcaeLocation> getAllDcaeLocations() {
46         return new ArrayList<>(dcaeLocations.values());
47     }
48
49     public DcaeLocation getDcaeLocation(String name) {
50         return dcaeLocations.get(name);
51     }
52
53     public DcaeLocation addDcaeLocation(DcaeLocation location) {
54         location.setLastMod();
55         location.setStatus(DmaapObject_Status.VALID);
56         dcaeLocations.put(location.getDcaeLocationName(), location);
57         return location;
58     }
59
60     public DcaeLocation updateDcaeLocation(DcaeLocation location) {
61         if (location.getDcaeLocationName().isEmpty()) {
62             return null;
63         }
64         location.setLastMod();
65         dcaeLocations.put(location.getDcaeLocationName(), location);
66         return location;
67     }
68
69     public DcaeLocation removeDcaeLocation(String locationName) {
70         return dcaeLocations.remove(locationName);
71     }
72
73     String getCentralLocation() {
74
75         Optional<DcaeLocation> firstCentralLocation =
76                 dcaeLocations.values().stream().filter(DcaeLocation::isCentral).findFirst();
77
78         return firstCentralLocation.isPresent() ? firstCentralLocation.get().getDcaeLocationName() : DEFAULT_CENTRAL_LOCATION;
79     }
80
81     boolean isEdgeLocation(String aName) {
82         return dcaeLocations.get(aName) != null && !dcaeLocations.get(aName).isCentral();
83     }
84
85 }