9e576857bc742caac62d1d9058abf524dea50c8a
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
19
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.TimeUnit;
26
27 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.read.mediator.server.list.output.Data;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class MediatorServerDataProvider implements AutoCloseable {
36
37         private static final Logger LOG = LoggerFactory.getLogger(MediatorServerDataProvider.class);
38
39         private final HtDatabaseClient dbClient;
40         private final DataObjectAcessor<Data> mediatorserverRW;
41         private final int REFRESH_INTERVAL = 60;
42         private final Map<String, Data> entries;
43         private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
44         private boolean isRunning;
45
46         public MediatorServerDataProvider(HostInfo[] hosts) throws Exception {
47                 this(hosts, null, null);
48         }
49
50         public MediatorServerDataProvider(HostInfo[] hosts, String authUsername, String authPassword) throws Exception {
51                 super();
52                 LOG.info("Start {}", this.getClass().getName());
53                 this.entries = new HashMap<>();
54                 this.dbClient = new HtDatabaseClient(hosts, authUsername, authPassword);
55                 this.mediatorserverRW = new DataObjectAcessor<>(dbClient, Entity.MediatorServer, Data.class);
56                 this.scheduler.scheduleAtFixedRate(onTick, this.REFRESH_INTERVAL, this.REFRESH_INTERVAL, TimeUnit.SECONDS);
57         }
58
59         private final Runnable onTick = new Runnable() {
60
61                 @Override
62                 public void run() {
63                         isRunning = true;
64                         runIt();
65                         isRunning = false;
66                 }
67
68         };
69
70         private void runIt() {
71                 SearchResult<Data> result = MediatorServerDataProvider.this.mediatorserverRW.doReadAll();
72                 List<Data> data = result.getHits();
73                 for (Data item : data) {
74                         MediatorServerDataProvider.this.entries.put(item.getId(), item);
75                 }
76         }
77
78         /**
79          * 
80          * @param dbServerId
81          * @return url or null if not exists
82          */
83         public String getHostUrl(String dbServerId) {
84                 Data info = this.entries.getOrDefault(dbServerId, null);
85                 return info == null ? null : info.getUrl();
86         }
87
88         public boolean triggerReloadSync() {
89                 if (!isRunning) {
90                         runIt();
91                 }
92                 return true;
93         }
94
95         @Override
96         public void close() throws Exception {
97                 this.scheduler.shutdown();
98         }
99 }