2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Arrays;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionLogStatus;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.Data;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.DataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.FaultsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.NetworkElementConnectionsBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint32;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SqlDBStatusReader {
42 private final Logger LOG = LoggerFactory.getLogger(SqlDBStatusReader.class);
44 private final SqlDBClient dbClient;
45 private final String controllerId;
47 public SqlDBStatusReader(SqlDBClient dbClient, String controllerId) {
48 this.dbClient = dbClient;
49 this.controllerId = controllerId;
52 public QueryResult<Data> getDataStatus() {
53 String selectQuery = createCountQuery("severity", Entity.Faultcurrent, this.controllerId);
54 long criticalCount = 0;
57 long warningCount = 0;
60 data = this.dbClient.read(selectQuery);
63 severity = data.getString(1);
64 if (severity != null) {
65 if (severity.equals(SeverityType.Critical.getName())) {
66 criticalCount = data.getLong(2);
67 } else if (severity.equals(SeverityType.Major.getName())) {
68 majorCount = data.getLong(2);
69 } else if (severity.equals(SeverityType.Minor.getName())) {
70 minorCount = data.getLong(2);
71 } else if (severity.equals(SeverityType.Warning.getName())) {
72 warningCount = data.getLong(2);
77 } catch (SQLException e) {
78 LOG.warn("problem reading status:", e);
80 DataBuilder builder = new DataBuilder().setFaults(
81 new FaultsBuilder().setCriticals(Uint32.valueOf(criticalCount)).setMajors(Uint32.valueOf(majorCount))
82 .setMinors(Uint32.valueOf(minorCount)).setWarnings(Uint32.valueOf(warningCount)).build());
83 selectQuery = createCountQuery("status", Entity.NetworkelementConnection, this.controllerId);
84 NetworkElementConnectionsBuilder neBuilder = new NetworkElementConnectionsBuilder();
86 long connectedCount = 0, connectingCount = 0, disconnectedCount = 0, mountedCount = 0, unableToConnectCount = 0,
87 undefinedCount = 0, unmountedCount = 0;
90 data = this.dbClient.read(selectQuery);
92 state = data.getString(1);
93 cnt = data.getLong(2);
95 if (state.equals(ConnectionLogStatus.Connected.getName())) {
97 } else if (state.equals(ConnectionLogStatus.Connecting.getName())) {
98 connectingCount = cnt;
99 } else if (state.equals(ConnectionLogStatus.Disconnected.getName())) {
100 disconnectedCount = cnt;
101 } else if (state.equals(ConnectionLogStatus.Mounted.getName())) {
103 } else if (state.equals(ConnectionLogStatus.UnableToConnect.getName())) {
104 unableToConnectCount = cnt;
105 } else if (state.equals(ConnectionLogStatus.Undefined.getName())) {
106 undefinedCount = cnt;
107 } else if (state.equals(ConnectionLogStatus.Unmounted.getName())) {
108 unmountedCount = cnt;
112 } catch (SQLException e) {
113 LOG.warn("problem reading status:", e);
115 neBuilder.setConnected(Uint32.valueOf(connectedCount)).setConnecting(Uint32.valueOf(connectingCount))
116 .setDisconnected(Uint32.valueOf(disconnectedCount)).setMounted(Uint32.valueOf(mountedCount))
117 .setTotal(Uint32.valueOf(connectedCount + connectingCount + disconnectedCount + mountedCount
118 + unableToConnectCount + undefinedCount + unmountedCount))
119 .setUnableToConnect(Uint32.valueOf(unableToConnectCount)).setUndefined(Uint32.valueOf(undefinedCount))
120 .setUnmounted(Uint32.valueOf(unmountedCount));
121 builder.setNetworkElementConnections(neBuilder.build());
122 return new QueryResult<Data>(Arrays.asList(builder.build()), 1, 1, 1);
125 private static String createCountQuery(String key, Entity e, String controllerId) {
126 return String.format("SELECT `%s`, COUNT(`%s`) " + "FROM `%s` " + "%s " + "GROUP BY `%s`;", key, key,
128 controllerId != null ? String.format("WHERE `%s`='%s'", SqlDBMapper.ODLID_DBCOL, controllerId) : "",