2 * ============LICENSE_START=======================================================
3 * ONAP : CCSDK.apps.sdnr.wt.apigateway
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.apigateway.database;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.TimeUnit;
31 public class DatabaseEntryProvider implements AutoCloseable {
33 private final DatabaseHttpClient httpClient;
34 private int refreshInterval;
35 private final Map<String, MediatorServerInfo> entries;
36 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
37 private boolean isRunning;
39 protected DatabaseEntryProvider (DatabaseHttpClient httpClient,int refreshInterval) {
40 this.httpClient = httpClient;
41 this.refreshInterval = refreshInterval;
42 this.entries = new HashMap<String, MediatorServerInfo>();
43 this.isRunning = false;
44 this.scheduler.scheduleAtFixedRate(onTick, this.refreshInterval, this.refreshInterval, TimeUnit.SECONDS);
46 public DatabaseEntryProvider(String dbBaseUri, int refreshInterval) {
48 this.httpClient = new DatabaseHttpClient(dbBaseUri, false);
49 this.refreshInterval = refreshInterval;
50 this.entries = new HashMap<String, MediatorServerInfo>();
51 this.isRunning = false;
52 this.scheduler.scheduleAtFixedRate(onTick, this.refreshInterval, this.refreshInterval, TimeUnit.SECONDS);
55 private final Runnable onTick = new Runnable() {
60 Map<String, MediatorServerInfo> map = DatabaseEntryProvider.this.httpClient.requestEntries();
61 DatabaseEntryProvider.this.entries.putAll(map);
67 public String getHostUrl(String dbServerId) {
68 MediatorServerInfo info = this.entries.getOrDefault(dbServerId, null);
69 return info == null ? null : info.getHost();
73 public void close() throws Exception {
74 this.scheduler.shutdown();
77 public boolean triggerReloadSync() {
78 new Thread(onTick).start();
80 while(isRunning && i-->0) {
83 } catch (InterruptedException e) {
90 public void setEntries(Map<String, MediatorServerInfo> e) {
93 this.entries.putAll(e);
95 public String printEntries() {
97 if(this.entries==null || this.entries.size()<=0) {
100 for(Entry<String, MediatorServerInfo> entry:this.entries.entrySet()) {
101 s+=String.format("%s:%s", entry.getKey(),entry.getValue().toString()+"\n");