2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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.openecomp.appc.dg.common.impl;
24 import org.openecomp.appc.dao.util.DBUtils;
25 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
26 import org.openecomp.appc.rankingframework.ConfigurationEntry;
27 import org.openecomp.appc.rankingframework.ConfigurationSet;
28 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
30 import java.sql.Connection;
31 import java.sql.PreparedStatement;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34 import java.util.Collection;
35 import java.util.Iterator;
37 abstract class AbstractResolverDataReader {
39 private class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
41 private final ResultSet resultSet;
43 private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
45 public boolean hasNext() {
47 return resultSet.next();
48 } catch (SQLException e) {
49 throw new RuntimeException(e);
54 public ConfigurationEntry<FlowKey> next() {
59 public void remove() {
60 throw new UnsupportedOperationException();
64 public Object getAttributeValue(String name) {
66 return resultSet.getObject(name);
67 } catch (SQLException e) {
68 throw new RuntimeException(e);
73 public FlowKey getResult() {
75 return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
76 } catch (SQLException e) {
77 throw new RuntimeException(e);
82 ConfigurationSetAdaptor(ResultSet resultSet) {
83 this.resultSet = resultSet;
87 public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
88 return new Iterable<ConfigurationEntry<FlowKey>>() {
91 public Iterator<ConfigurationEntry<FlowKey>> iterator() {
92 return new ResultSetIterator();
98 public Collection<String> getRankedAttributeNames() {
99 return getAttributeNames();
103 protected abstract Collection<String> getAttributeNames();
104 protected abstract String getQueryStmt();
108 RankedAttributesResolver<FlowKey> read() {
110 try (Connection conn = DBUtils.getConnection("sdnctl")) {
111 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
112 try (ResultSet res = stmt.executeQuery()) {
115 ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
116 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
118 throw new IllegalStateException();
123 } catch (SQLException e) {
124 throw new RuntimeException(e);