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.openecomp.appc.workflow.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.Arrays;
32 import java.util.Collection;
33 import java.util.Iterator;
35 import org.openecomp.appc.dao.util.DBUtils;
36 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
37 import org.openecomp.appc.rankingframework.ConfigurationEntry;
38 import org.openecomp.appc.rankingframework.ConfigurationSet;
39 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
41 class WorkflowResolverDataReader {
43 private static final String QUERY_STMT = "SELECT action,api_version,vnf_type,vnf_version,dg_name,dg_version,dg_module FROM VNF_DG_MAPPING";
45 private static final Collection<String> ATTRIBUTE_NAMES = Arrays.asList("action","api_version", "vnf_type", "vnf_version");
47 private static class ConfigurationSetAdaptor implements ConfigurationSet<WorkflowKey> {
49 private final ResultSet resultSet;
51 private class ResultSetIterator implements Iterator<ConfigurationEntry<WorkflowKey>>, ConfigurationEntry<WorkflowKey> {
53 public boolean hasNext() {
55 return resultSet.next();
56 } catch (SQLException e) {
57 throw new RuntimeException(e);
62 public ConfigurationEntry<WorkflowKey> next() {
67 public void remove() {
68 throw new UnsupportedOperationException();
72 public Object getAttributeValue(String name) {
74 return resultSet.getObject(name);
75 } catch (SQLException e) {
76 throw new RuntimeException(e);
81 public WorkflowKey getResult() {
83 return new WorkflowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
84 } catch (SQLException e) {
85 throw new RuntimeException(e);
90 ConfigurationSetAdaptor(ResultSet resultSet) {
91 this.resultSet = resultSet;
95 public Iterable<ConfigurationEntry<WorkflowKey>> getEntries() {
96 return new Iterable<ConfigurationEntry<WorkflowKey>>() {
99 public Iterator<ConfigurationEntry<WorkflowKey>> iterator() {
100 return new ResultSetIterator();
106 public Collection<String> getRankedAttributeNames() {
107 return ATTRIBUTE_NAMES;
111 RankedAttributesResolver<WorkflowKey> read() {
113 try (Connection conn = DBUtils.getConnection("sdnctl")) {
114 try (PreparedStatement stmt = conn.prepareStatement(QUERY_STMT)) {
115 try (ResultSet res = stmt.executeQuery()) {
118 ConfigurationSet<WorkflowKey> resolverConfig = new ConfigurationSetAdaptor(res);
119 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
121 // TODO: Return empty object
122 throw new IllegalStateException();
127 } catch (SQLException e) {
128 throw new RuntimeException(e);