fix apigateway for mediatorserver routes
[ccsdk/features.git] / sdnr / wt / apigateway / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / apigateway / database / DatabaseEntryProvider.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.ccsdk.features.sdnr.wt.apigateway.database;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.TimeUnit;
29
30
31 public class DatabaseEntryProvider implements AutoCloseable {
32
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;
38
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);
45         }
46         public DatabaseEntryProvider(String dbBaseUri, int refreshInterval) {
47
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);
53         }
54
55         private final Runnable onTick = new Runnable() {
56
57                 @Override
58                 public void run() {
59                         isRunning = true;
60                         Map<String, MediatorServerInfo> map = DatabaseEntryProvider.this.httpClient.requestEntries();
61                         DatabaseEntryProvider.this.entries.putAll(map);
62                         isRunning = false;
63                 }
64
65         };
66
67         public String getHostUrl(String dbServerId) {
68                 MediatorServerInfo info = this.entries.getOrDefault(dbServerId, null);
69                 return info == null ? null : info.getHost();
70         }
71
72         @Override
73         public void close() throws Exception {
74                 this.scheduler.shutdown();
75         }
76
77         public boolean triggerReloadSync() {
78                 new Thread(onTick).start();
79                 int i=20;
80                 while(isRunning && i-->0) {
81                         try {
82                                 Thread.sleep(500);
83                         } catch (InterruptedException e) {
84                                 Thread.interrupted();
85                         }                       
86                 }
87                 return i>0;
88         }
89
90         public void setEntries(Map<String, MediatorServerInfo> e) {
91         
92                 this.entries.clear();
93                 this.entries.putAll(e); 
94         }
95         public String printEntries() {
96                 String s="";
97                 if(this.entries==null || this.entries.size()<=0) {
98                         return "empty";
99                 }
100                 for(Entry<String, MediatorServerInfo> entry:this.entries.entrySet()) {
101                         s+=String.format("%s:%s", entry.getKey(),entry.getValue().toString()+"\n");
102                 }
103                 return s;
104         }
105
106 }