2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
25 import org.openecomp.appc.dao.util.DBUtils;
26 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
27 import org.openecomp.appc.rankingframework.ConfigurationEntry;
28 import org.openecomp.appc.rankingframework.ConfigurationSet;
29 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
31 import java.sql.Connection;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.util.Collection;
36 import java.util.Iterator;
38 abstract class AbstractResolverDataReader {
40 private class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
42 private final ResultSet resultSet;
44 private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
46 public boolean hasNext() {
48 return resultSet.next();
49 } catch (SQLException e) {
50 throw new RuntimeException(e);
55 public ConfigurationEntry<FlowKey> next() {
60 public void remove() {
61 throw new UnsupportedOperationException();
65 public Object getAttributeValue(String name) {
67 return resultSet.getObject(name);
68 } catch (SQLException e) {
69 throw new RuntimeException(e);
74 public FlowKey getResult() {
76 return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
77 } catch (SQLException e) {
78 throw new RuntimeException(e);
83 ConfigurationSetAdaptor(ResultSet resultSet) {
84 this.resultSet = resultSet;
88 public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
89 return new Iterable<ConfigurationEntry<FlowKey>>() {
92 public Iterator<ConfigurationEntry<FlowKey>> iterator() {
93 return new ResultSetIterator();
99 public Collection<String> getRankedAttributeNames() {
100 return getAttributeNames();
104 protected abstract Collection<String> getAttributeNames();
105 protected abstract String getQueryStmt();
109 RankedAttributesResolver<FlowKey> read() {
111 try (Connection conn = DBUtils.getConnection("sdnctl")) {
112 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
113 try (ResultSet res = stmt.executeQuery()) {
116 ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
117 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
119 throw new IllegalStateException();
124 } catch (SQLException e) {
125 throw new RuntimeException(e);