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.workflow.impl;
25 import java.sql.Connection;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Iterator;
33 import org.openecomp.appc.dao.util.DBUtils;
34 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
35 import org.openecomp.appc.rankingframework.ConfigurationEntry;
36 import org.openecomp.appc.rankingframework.ConfigurationSet;
37 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
39 class WorkflowResolverDataReader {
41 private static final String QUERY_STMT = "SELECT action,api_version,vnf_type,vnf_version,dg_name,dg_version,dg_module FROM VNF_DG_MAPPING";
43 private static final Collection<String> ATTRIBUTE_NAMES = Arrays.asList("action","api_version", "vnf_type", "vnf_version");
45 private static class ConfigurationSetAdaptor implements ConfigurationSet<WorkflowKey> {
47 private final ResultSet resultSet;
49 private class ResultSetIterator implements Iterator<ConfigurationEntry<WorkflowKey>>, ConfigurationEntry<WorkflowKey> {
51 public boolean hasNext() {
53 return resultSet.next();
54 } catch (SQLException e) {
55 throw new RuntimeException(e);
60 public ConfigurationEntry<WorkflowKey> next() {
65 public void remove() {
66 throw new UnsupportedOperationException();
70 public Object getAttributeValue(String name) {
72 return resultSet.getObject(name);
73 } catch (SQLException e) {
74 throw new RuntimeException(e);
79 public WorkflowKey getResult() {
81 return new WorkflowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
82 } catch (SQLException e) {
83 throw new RuntimeException(e);
88 ConfigurationSetAdaptor(ResultSet resultSet) {
89 this.resultSet = resultSet;
93 public Iterable<ConfigurationEntry<WorkflowKey>> getEntries() {
94 return new Iterable<ConfigurationEntry<WorkflowKey>>() {
97 public Iterator<ConfigurationEntry<WorkflowKey>> iterator() {
98 return new ResultSetIterator();
104 public Collection<String> getRankedAttributeNames() {
105 return ATTRIBUTE_NAMES;
109 RankedAttributesResolver<WorkflowKey> read() {
111 try (Connection conn = DBUtils.getConnection("sdnctl")) {
112 try (PreparedStatement stmt = conn.prepareStatement(QUERY_STMT)) {
113 try (ResultSet res = stmt.executeQuery()) {
116 ConfigurationSet<WorkflowKey> resolverConfig = new ConfigurationSetAdaptor(res);
117 return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
119 // TODO: Return empty object
120 throw new IllegalStateException();
125 } catch (SQLException e) {
126 throw new RuntimeException(e);