bc61e81e5337e93517cba4eede98e66c0ecdea49
[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                         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);
68                         }
69                         isRunning = false;
70                 }
71
72         };
73
74         public String getHostUrl(String dbServerId) {
75                 Data info = this.entries.getOrDefault(dbServerId, null);
76                 return info == null ? null : info.getUrl();
77         }
78
79         public boolean triggerReloadSync() {
80                 new Thread(onTick).start();
81                 int i = 20;
82                 while (isRunning && i-- > 0) {
83                         try {
84                                 Thread.sleep(500);
85                         } catch (InterruptedException e) {
86                                 Thread.currentThread().interrupt();
87                         }
88                 }
89                 return i > 0;
90         }
91
92         @Override
93         public void close() throws Exception {
94                 this.scheduler.shutdown();
95         }
96 }