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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.TimeUnit;
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;
35 public class MediatorServerDataProvider implements AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(MediatorServerDataProvider.class);
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;
46 public MediatorServerDataProvider(HostInfo[] hosts) throws Exception {
47 this(hosts, null, null);
50 public MediatorServerDataProvider(HostInfo[] hosts, String authUsername, String authPassword) throws Exception {
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);
59 private final Runnable onTick = new Runnable() {
64 SearchResult<Data> result = MediatorServerDataProvider.this.mediatorserverRW.doReadAll();
65 List<Data> data = result.getHits();
66 for (Data item : data) {
67 MediatorServerDataProvider.this.entries.put(item.getId(), item);
74 public String getHostUrl(String dbServerId) {
75 Data info = this.entries.getOrDefault(dbServerId, null);
76 return info == null ? null : info.getUrl();
79 public boolean triggerReloadSync() {
80 new Thread(onTick).start();
82 while (isRunning && i-- > 0) {
85 } catch (InterruptedException e) {
86 Thread.currentThread().interrupt();
93 public void close() throws Exception {
94 this.scheduler.shutdown();