2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
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;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Optional;
32 public class DcaeLocationService {
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;
37 public DcaeLocationService() {
38 this(DatabaseClass.getDcaeLocations());
41 DcaeLocationService(Map<String, DcaeLocation> dcaeLocations) {
42 this.dcaeLocations = dcaeLocations;
45 public List<DcaeLocation> getAllDcaeLocations() {
46 return new ArrayList<>(dcaeLocations.values());
49 public DcaeLocation getDcaeLocation(String name) {
50 return dcaeLocations.get(name);
53 public DcaeLocation addDcaeLocation(DcaeLocation location) {
54 location.setLastMod();
55 location.setStatus(DmaapObject_Status.VALID);
56 dcaeLocations.put(location.getDcaeLocationName(), location);
60 public DcaeLocation updateDcaeLocation(DcaeLocation location) {
61 if (location.getDcaeLocationName().isEmpty()) {
64 location.setLastMod();
65 dcaeLocations.put(location.getDcaeLocationName(), location);
69 public DcaeLocation removeDcaeLocation(String locationName) {
70 return dcaeLocations.remove(locationName);
73 String getCentralLocation() {
75 Optional<DcaeLocation> firstCentralLocation =
76 dcaeLocations.values().stream().filter(DcaeLocation::isCentral).findFirst();
78 return firstCentralLocation.isPresent() ? firstCentralLocation.get().getDcaeLocationName() : DEFAULT_CENTRAL_LOCATION;
81 boolean isEdgeLocation(String aName) {
82 return dcaeLocations.get(aName) != null && !dcaeLocations.get(aName).isCentral();