f9e0fcbc2c4f982e80d43d88282487f30e33b8e6
[ccsdk/features.git] / sdnr / wt / data-provider / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / impl / DataProviderImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
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  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.impl;
23
24 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
25 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.AboutHttpServlet;
26 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.MsServlet;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.ReadyHttpServlet;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEntityDataProvider;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.StatusChangedHandler.StatusKey;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
36 import org.opendaylight.mdsal.binding.api.RpcProviderService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class DataProviderImpl implements IEntityDataProvider, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(DataProviderImpl.class);
43
44     private static final String APPLICATION_NAME = "data-provider";
45     private RpcProviderService rpcProviderService = null;
46     private MsServlet mediatorServerServlet;
47     private DataProviderServiceImpl rpcApiService;
48     private AboutHttpServlet aboutServlet;
49     private DataTreeHttpServlet treeServlet;
50     private HtDatabaseClient dbClient;
51
52     // Blueprint 1
53     public DataProviderImpl() {
54         super();
55         LOG.info("Creating provider for {}", APPLICATION_NAME);
56     }
57
58     public void setRpcProviderService(RpcProviderService rpcProviderService) {
59         this.rpcProviderService = rpcProviderService;
60     }
61
62     public void setMediatorServerServlet(MsServlet servlet) {
63         this.mediatorServerServlet = servlet;
64     }
65
66     public void setAboutServlet(AboutHttpServlet aboutServlet) {
67         this.aboutServlet = aboutServlet;
68     }
69
70     public void setTreeServlet(DataTreeHttpServlet treeServlet) {
71         this.treeServlet = treeServlet;
72     }
73
74     public void init() throws Exception {
75
76         LOG.info("Session Initiated start {}", APPLICATION_NAME);
77
78         // Start RPC Service
79         this.rpcApiService = new DataProviderServiceImpl(rpcProviderService, this.mediatorServerServlet);
80         this.treeServlet.setDatabaseClient(this.rpcApiService.getRawClient());
81         LOG.info("Session Initiated end. Initialization done");
82     }
83
84     @Override
85     public void close() throws Exception {
86         LOG.info("DeviceManagerImpl closing ...");
87
88         close(dbClient);
89         close(rpcApiService);
90         LOG.info("DeviceManagerImpl closing done");
91     }
92
93     /**
94      * Used to close all Services, that should support AutoCloseable Pattern
95      *
96      * @param toClose
97      * @throws Exception
98      */
99     private void close(AutoCloseable... toCloseList) throws Exception {
100         for (AutoCloseable element : toCloseList) {
101             if (element != null) {
102                 element.close();
103             }
104         }
105     }
106
107     @Override
108     public DataProvider getDataProvider() {
109         return rpcApiService.getDataProvider();
110     }
111
112     @Override
113     public HtDatabaseMaintenance getHtDatabaseMaintenance() {
114         return rpcApiService.getHtDatabaseMaintenance();
115     }
116
117     @Override
118     public IEsConfig getEsConfig() {
119         return rpcApiService.getEsConfig();
120     }
121
122     @Override
123     public NetconfTimeStamp getConverter() {
124         return NetconfTimeStampImpl.getConverter();
125     }
126
127     @Override
128     public void setReadyStatus(boolean status) {
129         ReadyHttpServlet.setStatus(status);
130     }
131
132     @Override
133     public void setStatus(StatusKey key, String value) {
134         if (this.aboutServlet != null) {
135             if (key == StatusKey.CLUSTER_SIZE) {
136                 this.aboutServlet.setClusterSize(value);
137             }
138         }
139     }
140
141 }