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.workflow.impl;
 
  24 import java.sql.Connection;
 
  25 import java.sql.PreparedStatement;
 
  26 import java.sql.ResultSet;
 
  27 import java.sql.SQLException;
 
  28 import java.util.Arrays;
 
  29 import java.util.Collection;
 
  30 import java.util.Iterator;
 
  32 import org.openecomp.appc.dao.util.DBUtils;
 
  33 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
 
  34 import org.openecomp.appc.rankingframework.ConfigurationEntry;
 
  35 import org.openecomp.appc.rankingframework.ConfigurationSet;
 
  36 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
 
  38 class WorkflowResolverDataReader {
 
  40     private static final String QUERY_STMT = "SELECT action,api_version,vnf_type,vnf_version,dg_name,dg_version,dg_module FROM VNF_DG_MAPPING";
 
  42     private static final Collection<String> ATTRIBUTE_NAMES = Arrays.asList("action","api_version", "vnf_type", "vnf_version");
 
  44     private static class ConfigurationSetAdaptor implements ConfigurationSet<WorkflowKey> {
 
  46         private final ResultSet resultSet;
 
  48         private class ResultSetIterator implements Iterator<ConfigurationEntry<WorkflowKey>>, ConfigurationEntry<WorkflowKey> {
 
  50             public boolean hasNext() {
 
  52                     return resultSet.next();
 
  53                 } catch (SQLException e) {
 
  54                     throw new RuntimeException(e);
 
  59             public ConfigurationEntry<WorkflowKey> next() {
 
  64             public void remove() {
 
  65                 throw new UnsupportedOperationException();
 
  69             public Object getAttributeValue(String name) {
 
  71                     return resultSet.getObject(name);
 
  72                 } catch (SQLException e) {
 
  73                     throw new RuntimeException(e);
 
  78             public WorkflowKey getResult() {
 
  80                     return new WorkflowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
 
  81                 } catch (SQLException e) {
 
  82                     throw new RuntimeException(e);
 
  87         ConfigurationSetAdaptor(ResultSet resultSet) {
 
  88             this.resultSet = resultSet;
 
  92         public Iterable<ConfigurationEntry<WorkflowKey>> getEntries() {
 
  93             return new Iterable<ConfigurationEntry<WorkflowKey>>() {
 
  96                 public Iterator<ConfigurationEntry<WorkflowKey>> iterator() {
 
  97                     return new ResultSetIterator();
 
 103         public Collection<String> getRankedAttributeNames() {
 
 104             return ATTRIBUTE_NAMES;
 
 108     RankedAttributesResolver<WorkflowKey> read() {
 
 110             try (Connection conn = DBUtils.getConnection("sdnctl")) {
 
 111                 try (PreparedStatement stmt = conn.prepareStatement(QUERY_STMT)) {
 
 112                     try (ResultSet res = stmt.executeQuery()) {
 
 115                             ConfigurationSet<WorkflowKey> resolverConfig = new ConfigurationSetAdaptor(res);
 
 116                             return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
 
 118                             // TODO: Return empty object
 
 119                             throw new IllegalStateException();
 
 124         } catch (SQLException e) {
 
 125             throw new RuntimeException(e);