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 org.onap.appc.dao.util.DBUtils;
28 import org.onap.appc.rankingframework.AbstractRankedAttributesResolverFactory;
29 import org.onap.appc.rankingframework.ConfigurationEntry;
30 import org.onap.appc.rankingframework.ConfigurationSet;
31 import org.onap.appc.rankingframework.RankedAttributesResolver;
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.ResultSet;
36 import java.sql.SQLException;
37 import java.util.Collection;
38 import java.util.Iterator;
40 abstract class AbstractResolverDataReader {
42 private class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
44 private final ResultSet resultSet;
46 private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
48 public boolean hasNext() {
50 return resultSet.next();
51 } catch (SQLException e) {
52 throw new RuntimeException(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 RuntimeException(e);
76 public FlowKey getResult() {
78 return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
79 } catch (SQLException e) {
80 throw new RuntimeException(e);
85 ConfigurationSetAdaptor(ResultSet resultSet) {
86 this.resultSet = resultSet;
90 public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
91 return new Iterable<ConfigurationEntry<FlowKey>>() {
94 public Iterator<ConfigurationEntry<FlowKey>> iterator() {
95 return new ResultSetIterator();
101 public Collection<String> getRankedAttributeNames() {
102 return getAttributeNames();
106 protected abstract Collection<String> getAttributeNames();
107 protected abstract String getQueryStmt();
111 RankedAttributesResolver<FlowKey> read() {
113 try (Connection conn = DBUtils.getConnection("sdnctl")) {
114 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
115 try (ResultSet res = stmt.executeQuery()) {
118 ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
119 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
121 throw new IllegalStateException();
126 } catch (SQLException e) {
127 throw new RuntimeException(e);