2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.common.impl;
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import org.onap.appc.dao.util.DBUtils;
34 import org.onap.appc.rankingframework.AbstractRankedAttributesResolverFactory;
35 import org.onap.appc.rankingframework.ConfigurationEntry;
36 import org.onap.appc.rankingframework.ConfigurationSet;
37 import org.onap.appc.rankingframework.RankedAttributesResolver;
39 abstract class AbstractResolverDataReader {
41 private class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
43 private final ResultSet resultSet;
45 private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
48 public boolean hasNext() {
50 return resultSet.next();
51 } catch (SQLException e) {
52 throw new DataReaderException(e);
57 public ConfigurationEntry<FlowKey> next() {
62 public void remove() {
63 throw new UnsupportedOperationException();
67 public Object getAttributeValue(String name) {
69 return resultSet.getObject(name);
70 } catch (SQLException e) {
71 throw new DataReaderException(e);
76 public FlowKey getResult() {
78 return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"),
79 resultSet.getString("dg_module"));
80 } catch (SQLException e) {
81 throw new DataReaderException(e);
86 ConfigurationSetAdaptor(ResultSet resultSet) {
87 this.resultSet = resultSet;
91 public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
93 return ResultSetIterator::new;
97 public Collection<String> getRankedAttributeNames() {
98 return getAttributeNames();
102 protected abstract Collection<String> getAttributeNames();
104 protected abstract String getQueryStmt();
107 RankedAttributesResolver<FlowKey> read() {
109 try (Connection conn = DBUtils.getConnection("sdnctl")) {
110 return getFlowKeyRankedAttributesResolver(conn);
112 } catch (SQLException e) {
113 throw new DataReaderException(e);
117 private RankedAttributesResolver<FlowKey> getFlowKeyRankedAttributesResolver(Connection conn) throws SQLException {
118 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
119 try (ResultSet res = stmt.executeQuery()) {
122 ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
123 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
125 throw new IllegalStateException();