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