Bläddra i källkod

AMBARI-779. Introduce ManagementController interface. (Contributed by Tom Beerbower)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/AMBARI-666@1392497 13f79535-47bb-0310-9956-ffa450edef68
Hitesh Shah 13 år sedan
förälder
incheckning
e516813a69
22 ändrade filer med 1256 tillägg och 292 borttagningar
  1. 2 0
      AMBARI-666-CHANGES.txt
  2. 11 2
      ambari-api/src/main/java/org/apache/ambari/api/controller/internal/RequestImpl.java
  3. 218 0
      ambari-api/src/main/java/org/apache/ambari/api/controller/internal/ResourceProviderImpl.java
  4. 4 7
      ambari-api/src/main/java/org/apache/ambari/api/controller/internal/SchemaImpl.java
  5. 322 0
      ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCHelper.java
  6. 138 0
      ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCManagementController.java
  7. 0 213
      ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCResourceProvider.java
  8. 5 4
      ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/SQLiteConnectionFactory.java
  9. 229 0
      ambari-api/src/main/java/org/apache/ambari/api/controller/spi/ManagementController.java
  10. 2 0
      ambari-api/src/main/java/org/apache/ambari/api/controller/spi/Request.java
  11. 31 3
      ambari-api/src/main/java/org/apache/ambari/api/controller/spi/ResourceProvider.java
  12. 14 9
      ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/ClusterControllerHelper.java
  13. 2 1
      ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/DBHelper.java
  14. 9 10
      ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/Properties.java
  15. 1 1
      ambari-api/src/main/java/org/apache/ambari/api/query/QueryImpl.java
  16. 3 3
      ambari-api/src/main/resources/key_properties.json
  17. 21 21
      ambari-api/src/main/resources/properties.json
  18. 41 5
      ambari-api/src/test/java/org/apache/ambari/api/controller/internal/ClusterControllerImplTest.java
  19. 1 1
      ambari-api/src/test/java/org/apache/ambari/api/controller/internal/RequestImplTest.java
  20. 23 8
      ambari-api/src/test/java/org/apache/ambari/api/controller/internal/SchemaImplTest.java
  21. 170 0
      ambari-api/src/test/java/org/apache/ambari/api/controller/jdbc/JDBCManagementControllerTest.java
  22. 9 4
      ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java

+ 2 - 0
AMBARI-666-CHANGES.txt

@@ -12,6 +12,8 @@ AMBARI-666 branch (unreleased changes)
 
 
   NEW FEATURES
   NEW FEATURES
 
 
+  AMBARI-779. Introduce ManagementController interface. (Tom Beerbower via hitesh)
+
   AMBARI-755. Heartbeat handler: Update state as reported in heartbeat.
   AMBARI-755. Heartbeat handler: Update state as reported in heartbeat.
   (jitendra)
   (jitendra)
 
 

+ 11 - 2
ambari-api/src/main/java/org/apache/ambari/api/controller/internal/RequestImpl.java

@@ -22,6 +22,8 @@ import org.apache.ambari.api.controller.spi.PropertyId;
 import org.apache.ambari.api.controller.spi.Request;
 import org.apache.ambari.api.controller.spi.Request;
 
 
 import java.util.Collections;
 import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -30,9 +32,11 @@ import java.util.Set;
 public class RequestImpl implements Request {
 public class RequestImpl implements Request {
 
 
   private final Set<PropertyId> propertyIds;
   private final Set<PropertyId> propertyIds;
+  private final Set<Map<PropertyId, String>> properties;
 
 
-  public RequestImpl(Set<PropertyId> propertyIds) {
-    this.propertyIds = Collections.unmodifiableSet(propertyIds);
+  public RequestImpl(Set<PropertyId> propertyIds, Set<Map<PropertyId, String>> properties) {
+    this.propertyIds = propertyIds == null ? Collections.unmodifiableSet(new HashSet<PropertyId>()) : Collections.unmodifiableSet(propertyIds);
+    this.properties = properties == null ? Collections.unmodifiableSet(new HashSet<Map<PropertyId, String>>()) : Collections.unmodifiableSet(properties);
   }
   }
 
 
   @Override
   @Override
@@ -40,6 +44,11 @@ public class RequestImpl implements Request {
     return propertyIds;
     return propertyIds;
   }
   }
 
 
+  @Override
+  public Set<Map<PropertyId, String>> getProperties() {
+    return properties;
+  }
+
   @Override
   @Override
   public TemporalInfo getTemporalInfo(PropertyId id) {
   public TemporalInfo getTemporalInfo(PropertyId id) {
     return null;  //TODO
     return null;  //TODO

+ 218 - 0
ambari-api/src/main/java/org/apache/ambari/api/controller/internal/ResourceProviderImpl.java

@@ -0,0 +1,218 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.api.controller.internal;
+
+import org.apache.ambari.api.controller.spi.ManagementController;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.PropertyId;
+import org.apache.ambari.api.controller.spi.Request;
+import org.apache.ambari.api.controller.spi.Resource;
+import org.apache.ambari.api.controller.spi.ResourceProvider;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ */
+public abstract class ResourceProviderImpl implements ResourceProvider {
+
+  protected final Set<PropertyId> propertyIds;
+
+  private final ManagementController managementController;
+
+  private ResourceProviderImpl(Set<PropertyId> propertyIds, ManagementController managementController) {
+    this.propertyIds = propertyIds;
+    this.managementController = managementController;
+  }
+
+  @Override
+  public Set<PropertyId> getPropertyIds() {
+    return propertyIds;
+  }
+
+  public ManagementController getManagementController() {
+    return managementController;
+  }
+
+  public static ResourceProvider getResourceProvider(Resource.Type type, Set<PropertyId> propertyIds, ManagementController managementController)  {
+  
+    switch (type) {
+      case Cluster:
+        return new ClusterResourceProvider(propertyIds, managementController);
+      case Service:
+        return new ServiceResourceProvider(propertyIds, managementController);
+      case Component:
+        return new ComponentResourceProvider(propertyIds, managementController);
+      case Host:
+        return new HostResourceProvider(propertyIds, managementController);
+      case HostComponent:
+        return new HostComponentResourceProvider(propertyIds, managementController);
+    }
+    throw new IllegalArgumentException("Unknown type " + type);
+  }
+
+  protected Request getRequest(Request request) {
+    Set<PropertyId> propertyIds = new HashSet<PropertyId>(request.getPropertyIds());
+    if (propertyIds.size() == 0) {
+      request = new RequestImpl(this.propertyIds, null);
+    }
+    return request;
+  }
+
+  private static class ClusterResourceProvider extends ResourceProviderImpl{
+
+    private ClusterResourceProvider(Set<PropertyId> propertyIds, ManagementController managementController) {
+      super(propertyIds, managementController);
+    }
+
+    @Override
+    public void createResources(Request request) {
+      getManagementController().createClusters(request);
+    }
+
+    @Override
+    public Set<Resource> getResources(Request request, Predicate predicate) {
+      request = getRequest(request);
+      return getManagementController().getClusters(request, predicate);
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      getManagementController().updateClusters(request, predicate);
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      getManagementController().deleteClusters(predicate);
+    }
+  }
+
+  private static class ServiceResourceProvider extends ResourceProviderImpl{
+
+    private ServiceResourceProvider(Set<PropertyId> propertyIds, ManagementController managementController) {
+      super(propertyIds, managementController);
+    }
+
+    @Override
+    public void createResources(Request request) {
+      getManagementController().createServices(request);
+    }
+
+    @Override
+    public Set<Resource> getResources(Request request, Predicate predicate) {
+      request = getRequest(request);
+      return getManagementController().getServices(request, predicate);
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      getManagementController().updateServices(request, predicate);
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      getManagementController().deleteServices(predicate);
+    }
+  }
+
+  private static class ComponentResourceProvider extends ResourceProviderImpl{
+
+    private ComponentResourceProvider(Set<PropertyId> propertyIds, ManagementController managementController) {
+      super(propertyIds, managementController);
+    }
+
+    @Override
+    public void createResources(Request request) {
+      getManagementController().createComponents(request);
+    }
+
+    @Override
+    public Set<Resource> getResources(Request request, Predicate predicate) {
+      request = getRequest(request);
+      return getManagementController().getComponents(request, predicate);
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      getManagementController().updateComponents(request, predicate);
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      getManagementController().deleteComponents(predicate);
+    }
+  }
+
+  private static class HostResourceProvider extends ResourceProviderImpl{
+
+    private HostResourceProvider(Set<PropertyId> propertyIds, ManagementController managementController) {
+      super(propertyIds, managementController);
+    }
+
+    @Override
+    public void createResources(Request request) {
+      getManagementController().createHosts(request);
+    }
+
+    @Override
+    public Set<Resource> getResources(Request request, Predicate predicate) {
+      request = getRequest(request);
+      return getManagementController().getHosts(request, predicate);
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      getManagementController().updateHosts(request, predicate);
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      getManagementController().deleteHosts(predicate);
+    }
+  }
+
+  private static class HostComponentResourceProvider extends ResourceProviderImpl{
+
+    private HostComponentResourceProvider(Set<PropertyId> propertyIds, ManagementController managementController) {
+      super(propertyIds, managementController);
+    }
+
+    @Override
+    public void createResources(Request request) {
+      getManagementController().createHostComponents(request);
+    }
+
+    @Override
+    public Set<Resource> getResources(Request request, Predicate predicate) {
+      request = getRequest(request);
+      return getManagementController().getHostComponents(request, predicate);
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      getManagementController().updateHostComponents(request, predicate);
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      getManagementController().deleteHostComponents(predicate);
+    }
+  }
+}
+

+ 4 - 7
ambari-api/src/main/java/org/apache/ambari/api/controller/internal/SchemaImpl.java

@@ -34,16 +34,13 @@ import java.util.Set;
  * Default schema implementation.
  * Default schema implementation.
  */
  */
 public class SchemaImpl implements Schema {
 public class SchemaImpl implements Schema {
-  private final Resource.Type type;
   private final ResourceProvider resourceProvider;
   private final ResourceProvider resourceProvider;
   private final List<PropertyProvider> propertyProviders;
   private final List<PropertyProvider> propertyProviders;
-  private final Map<String, PropertyId> keyPropertyIds;
+  private final Map<Resource.Type, PropertyId> keyPropertyIds;
 
 
-  public SchemaImpl(Resource.Type type,
-                    ResourceProvider resourceProvider,
+  public SchemaImpl(ResourceProvider resourceProvider,
                     List<PropertyProvider> propertyProviders,
                     List<PropertyProvider> propertyProviders,
-                    Map<String, PropertyId> keyPropertyIds) {
-    this.type = type;
+                    Map<Resource.Type, PropertyId> keyPropertyIds) {
     this.resourceProvider = resourceProvider;
     this.resourceProvider = resourceProvider;
     this.propertyProviders = propertyProviders;
     this.propertyProviders = propertyProviders;
     this.keyPropertyIds = keyPropertyIds;
     this.keyPropertyIds = keyPropertyIds;
@@ -51,7 +48,7 @@ public class SchemaImpl implements Schema {
 
 
   @Override
   @Override
   public PropertyId getKeyPropertyId(Resource.Type type) {
   public PropertyId getKeyPropertyId(Resource.Type type) {
-    return keyPropertyIds.get(type.toString());
+    return keyPropertyIds.get(type);
   }
   }
 
 
   @Override
   @Override

+ 322 - 0
ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCHelper.java

@@ -0,0 +1,322 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.api.controller.jdbc;
+
+import org.apache.ambari.api.controller.internal.PropertyIdImpl;
+import org.apache.ambari.api.controller.internal.ResourceImpl;
+import org.apache.ambari.api.controller.predicate.BasePredicate;
+import org.apache.ambari.api.controller.predicate.PredicateVisitorAcceptor;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.PropertyId;
+import org.apache.ambari.api.controller.spi.Request;
+import org.apache.ambari.api.controller.spi.Resource;
+import org.apache.ambari.api.controller.utilities.PredicateHelper;
+import org.apache.ambari.api.controller.utilities.Properties;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Helper class for JDBC related operations.
+ */
+public class JDBCHelper {
+
+  public static void createResources(ConnectionFactory connectionFactory, Request request) {
+    try {
+      Connection connection = connectionFactory.getConnection();
+
+      try {
+
+        Set<Map<PropertyId, String>> propertySet = request.getProperties();
+
+        for (Map<PropertyId, String> properties : propertySet) {
+          String sql = getInsertSQL(properties);
+
+          Statement statement = connection.createStatement();
+
+          statement.execute(sql);
+        }
+      } finally {
+        connection.close();
+      }
+
+    } catch (SQLException e) {
+      throw new IllegalStateException("DB error : ", e);
+    }
+  }
+
+  public static Set<Resource> getResources(ConnectionFactory connectionFactory, Resource.Type type, Request request, Predicate predicate) {
+
+    Set<Resource> resources = new HashSet<Resource>();
+    Set<PropertyId> propertyIds = new HashSet<PropertyId>(request.getPropertyIds());
+    if (predicate != null) {
+      propertyIds.addAll(PredicateHelper.getPropertyIds(predicate));
+    }
+
+    try {
+      Connection connection = connectionFactory.getConnection();
+
+      try {
+
+        Map<PropertyId, PropertyId> joinKeys = getJoins(connection, propertyIds);
+        String sql = getSelectSQL(propertyIds, predicate, joinKeys);
+        Statement statement = connection.createStatement();
+
+        ResultSet rs = statement.executeQuery(sql);
+
+        while (rs.next()) {
+          ResultSetMetaData metaData = rs.getMetaData();
+          int columnCount = metaData.getColumnCount();
+
+          final ResourceImpl resource = new ResourceImpl(type);
+          for (int i = 1; i <= columnCount; ++i) {
+            PropertyIdImpl propertyId = new PropertyIdImpl(metaData.getColumnName(i), metaData.getTableName(i), false);
+            if (propertyIds.contains(propertyId)) {
+              resource.setProperty(propertyId, rs.getString(i));
+            }
+          }
+          resources.add(resource);
+        }
+
+      } finally {
+        connection.close();
+      }
+
+    } catch (SQLException e) {
+      throw new IllegalStateException("DB error : ", e);
+    }
+
+    return resources;
+  }
+
+  public static void updateResources(ConnectionFactory connectionFactory, Request request, Predicate predicate) {
+
+    try {
+      Connection connection = connectionFactory.getConnection();
+      try {
+        Set<Map<PropertyId, String>> propertySet = request.getProperties();
+
+        Map<PropertyId, String> properties = propertySet.iterator().next();
+
+        String sql = getUpdateSQL(properties, predicate);
+
+        Statement statement = connection.createStatement();
+
+        statement.execute(sql);
+      } finally {
+        connection.close();
+      }
+
+    } catch (SQLException e) {
+      throw new IllegalStateException("DB error : ", e);
+    }
+  }
+
+  public static void deleteResources(ConnectionFactory connectionFactory, Predicate predicate) {
+    try {
+      Connection connection = connectionFactory.getConnection();
+      try {
+        String sql = getDeleteSQL(predicate);
+
+        Statement statement = connection.createStatement();
+        statement.execute(sql);
+      } finally {
+        connection.close();
+      }
+
+    } catch (SQLException e) {
+      throw new IllegalStateException("DB error : ", e);
+    }
+  }
+
+
+  // ----- Helper methods ----------------------------------------------------
+
+  private static String getInsertSQL(Map<PropertyId, String> properties) {
+
+    StringBuilder columns = new StringBuilder();
+    StringBuilder values = new StringBuilder();
+    String table = null;
+
+
+    for (Map.Entry<PropertyId, String> entry : properties.entrySet()) {
+      PropertyId propertyId    = entry.getKey();
+      String     propertyValue = entry.getValue();
+
+      table = propertyId.getCategory();
+
+
+      if (columns.length() > 0) {
+        columns.append(", ");
+      }
+      columns.append(propertyId.getName());
+
+      if (values.length() > 0) {
+        values.append(", ");
+      }
+      values.append("'");
+      values.append(propertyValue);
+      values.append("'");
+    }
+
+    return "insert into " + table + " (" +
+        columns + ") values (" +values + ")";
+  }
+
+  private static String getSelectSQL(Set<PropertyId> propertyIds, Predicate predicate, Map<PropertyId, PropertyId> joinKeys) {
+
+    StringBuilder columns = new StringBuilder();
+    Set<String> tableSet = new HashSet<String>();
+
+    for (PropertyId propertyId : propertyIds) {
+      if (columns.length() > 0) {
+        columns.append(", ");
+      }
+      columns.append(propertyId.getCategory()).append(".").append(propertyId.getName());
+      tableSet.add(propertyId.getCategory());
+    }
+
+    boolean haveWhereClause = false;
+    StringBuilder whereClause = new StringBuilder();
+    if (predicate != null &&
+        propertyIds.containsAll(PredicateHelper.getPropertyIds(predicate)) &&
+        predicate instanceof PredicateVisitorAcceptor) {
+
+      SQLPredicateVisitor visitor = new SQLPredicateVisitor();
+      ((PredicateVisitorAcceptor) predicate).accept(visitor);
+      whereClause.append(visitor.getSQL());
+      haveWhereClause = true;
+    }
+
+    StringBuilder joinClause = new StringBuilder();
+
+    if (tableSet.size() > 1) {
+      for (Map.Entry<PropertyId, PropertyId> entry : joinKeys.entrySet()) {
+        String category1 = entry.getKey().getCategory();
+        String category2 = entry.getValue().getCategory();
+        if (tableSet.contains(category1) && tableSet.contains(category2)){
+          if (haveWhereClause || joinClause.length() > 0) {
+            joinClause.append(" and ");
+          }
+          joinClause.append(category1).append(".").append(entry.getKey().getName());
+          joinClause.append("=");
+          joinClause.append(category2).append(".").append(entry.getValue().getName());
+          tableSet.add(category1);
+          tableSet.add(category2);
+        }
+      }
+      haveWhereClause = true;
+    }
+
+    StringBuilder tables = new StringBuilder();
+
+    for (String table : tableSet) {
+      if (tables.length() > 0) {
+        tables.append(", ");
+      }
+      tables.append(table);
+    }
+
+    String sql = "select " + columns + " from " + tables;
+
+    if (haveWhereClause) {
+      sql = sql + " where " + whereClause + joinClause;
+    }
+    return sql;
+  }
+
+  private static String getDeleteSQL(Predicate predicate) {
+
+    StringBuilder whereClause = new StringBuilder();
+    if (predicate instanceof BasePredicate) {
+
+      BasePredicate basePredicate = (BasePredicate) predicate;
+
+      SQLPredicateVisitor visitor = new SQLPredicateVisitor();
+      basePredicate.accept(visitor);
+      whereClause.append(visitor.getSQL());
+
+      String table = basePredicate.getPropertyIds().iterator().next().getCategory();
+
+      return "delete from " + table + " where " + whereClause;
+    }
+    throw new IllegalStateException("Can't generate SQL.");
+  }
+
+  private static String getUpdateSQL(Map<PropertyId, String> properties, Predicate predicate) {
+
+    if (predicate instanceof BasePredicate) {
+
+      StringBuilder whereClause = new StringBuilder();
+
+      BasePredicate basePredicate = (BasePredicate) predicate;
+
+      SQLPredicateVisitor visitor = new SQLPredicateVisitor();
+      basePredicate.accept(visitor);
+      whereClause.append(visitor.getSQL());
+
+      String table = basePredicate.getPropertyIds().iterator().next().getCategory();
+
+
+      StringBuilder setClause = new StringBuilder();
+      for (Map.Entry<PropertyId, String> entry : properties.entrySet()) {
+
+        if (setClause.length() > 0) {
+          setClause.append(", ");
+        }
+        setClause.append(entry.getKey().getName());
+        setClause.append(" = ");
+        setClause.append("'");
+        setClause.append(entry.getValue());
+        setClause.append("'");
+      }
+
+      return "update " + table + " set " + setClause + " where " + whereClause;
+    }
+    throw new IllegalStateException("Can't generate SQL.");
+  }
+
+  private static Map<PropertyId, PropertyId> getJoins(Connection connection, Set<PropertyId> tables) throws SQLException {
+    Map<PropertyId, PropertyId> joins = new HashMap<PropertyId, PropertyId>();
+    DatabaseMetaData meta = connection.getMetaData();
+
+    for (PropertyId propertyId : tables) {
+      ResultSet rs = meta.getImportedKeys(connection.getCatalog(), null, propertyId.getCategory());
+
+      while (rs.next()) {
+
+        PropertyId pkPropertyId = Properties.getPropertyId(
+            rs.getString("PKCOLUMN_NAME"), rs.getString("PKTABLE_NAME"));
+
+        PropertyId fkPropertyId = Properties.getPropertyId(
+            rs.getString("FKCOLUMN_NAME"), rs.getString("FKTABLE_NAME"));
+        joins.put(pkPropertyId, fkPropertyId);
+      }
+    }
+    return joins;
+  }
+}

+ 138 - 0
ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCManagementController.java

@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.api.controller.jdbc;
+
+import org.apache.ambari.api.controller.spi.ManagementController;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.Request;
+import org.apache.ambari.api.controller.spi.Resource;
+
+import java.util.Set;
+
+/**
+ * Generic JDBC implementation of a management controller.
+ */
+public class JDBCManagementController implements ManagementController {
+
+  private final ConnectionFactory connectionFactory;
+
+  public JDBCManagementController(ConnectionFactory connectionFactory) {
+    this.connectionFactory = connectionFactory;
+  }
+
+  @Override
+  public void createClusters(Request request) {
+    JDBCHelper.createResources(connectionFactory, request);
+  }
+
+  @Override
+  public void createServices(Request request) {
+    JDBCHelper.createResources(connectionFactory, request);
+  }
+
+  @Override
+  public void createComponents(Request request) {
+    JDBCHelper.createResources(connectionFactory, request);
+  }
+
+  @Override
+  public void createHosts(Request request) {
+    JDBCHelper.createResources(connectionFactory, request);
+  }
+
+  @Override
+  public void createHostComponents(Request request) {
+    JDBCHelper.createResources(connectionFactory, request);
+  }
+
+  @Override
+  public Set<Resource> getClusters(Request request, Predicate predicate) {
+    return JDBCHelper.getResources(connectionFactory, Resource.Type.Cluster, request, predicate);
+  }
+
+  @Override
+  public Set<Resource> getServices(Request request, Predicate predicate) {
+    return JDBCHelper.getResources(connectionFactory, Resource.Type.Service, request, predicate);
+  }
+
+  @Override
+  public Set<Resource> getComponents(Request request, Predicate predicate) {
+    return JDBCHelper.getResources(connectionFactory, Resource.Type.Component, request, predicate);
+  }
+
+  @Override
+  public Set<Resource> getHosts(Request request, Predicate predicate) {
+    return JDBCHelper.getResources(connectionFactory, Resource.Type.Host, request, predicate);
+  }
+
+  @Override
+  public Set<Resource> getHostComponents(Request request, Predicate predicate) {
+    return JDBCHelper.getResources(connectionFactory, Resource.Type.HostComponent, request, predicate);
+  }
+
+  @Override
+  public void updateClusters(Request request, Predicate predicate) {
+    JDBCHelper.updateResources(connectionFactory, request, predicate);
+  }
+
+  @Override
+  public void updateServices(Request request, Predicate predicate) {
+    JDBCHelper.updateResources(connectionFactory, request, predicate);
+  }
+
+  @Override
+  public void updateComponents(Request request, Predicate predicate) {
+    JDBCHelper.updateResources(connectionFactory, request, predicate);
+  }
+
+  @Override
+  public void updateHosts(Request request, Predicate predicate) {
+    JDBCHelper.updateResources(connectionFactory, request, predicate);
+  }
+
+  @Override
+  public void updateHostComponents(Request request, Predicate predicate) {
+    JDBCHelper.updateResources(connectionFactory, request, predicate);
+  }
+
+  @Override
+  public void deleteClusters(Predicate predicate) {
+    JDBCHelper.deleteResources(connectionFactory, predicate);
+  }
+
+  @Override
+  public void deleteServices(Predicate predicate) {
+    JDBCHelper.deleteResources(connectionFactory, predicate);
+  }
+
+  @Override
+  public void deleteComponents(Predicate predicate) {
+    JDBCHelper.deleteResources(connectionFactory, predicate);
+  }
+
+  @Override
+  public void deleteHosts(Predicate predicate) {
+    JDBCHelper.deleteResources(connectionFactory, predicate);
+  }
+
+  @Override
+  public void deleteHostComponents(Predicate predicate) {
+    JDBCHelper.deleteResources(connectionFactory, predicate);
+  }
+}

+ 0 - 213
ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/JDBCResourceProvider.java

@@ -1,213 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.api.controller.jdbc;
-
-import org.apache.ambari.api.controller.internal.PropertyIdImpl;
-import org.apache.ambari.api.controller.internal.ResourceImpl;
-import org.apache.ambari.api.controller.predicate.PredicateVisitorAcceptor;
-import org.apache.ambari.api.controller.spi.Predicate;
-import org.apache.ambari.api.controller.spi.PropertyId;
-import org.apache.ambari.api.controller.spi.Request;
-import org.apache.ambari.api.controller.spi.Resource;
-import org.apache.ambari.api.controller.spi.ResourceProvider;
-import org.apache.ambari.api.controller.utilities.PredicateHelper;
-import org.apache.ambari.api.controller.utilities.Properties;
-
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * JDBC based resource provider.
- */
-public class JDBCResourceProvider implements ResourceProvider {
-
-  private final Resource.Type type;
-
-  private final Set<PropertyId> propertyIds;
-
-  private final PropertyId[][] f_keys;
-
-  private final ConnectionFactory connectionFactory;
-
-  private static final Map<Resource.Type, PropertyId[][]> F_KEYS = new HashMap<Resource.Type, PropertyId[][]>();
-
-  static {
-    PropertyId[][] f_keys = new PropertyId[][]{
-        {new PropertyIdImpl("service_name", "Services", false), new PropertyIdImpl("service_name", "ServiceInfo", false)}};
-    F_KEYS.put(Resource.Type.Service, f_keys);
-
-    f_keys = new PropertyId[][]{
-        {new PropertyIdImpl("service_name", "ServiceComponents", false), new PropertyIdImpl("service_name", "ServiceComponentInfo", false)},
-        {new PropertyIdImpl("component_name", "ServiceComponents", false), new PropertyIdImpl("component_name", "ServiceComponentInfo", false)}};
-    F_KEYS.put(Resource.Type.Component, f_keys);
-  }
-
-
-  private JDBCResourceProvider(ConnectionFactory connectionFactory, Resource.Type type) {
-    this.connectionFactory = connectionFactory;
-    this.type = type;
-    this.propertyIds = Properties.getPropertyIds(type, "DB");
-    this.f_keys = F_KEYS.get(type);
-  }
-
-  @Override
-  public Set<Resource> getResources(Request request, Predicate predicate) {
-
-    Set<Resource> resources = new HashSet<Resource>();
-    Set<PropertyId> propertyIds = new HashSet<PropertyId>(request.getPropertyIds());
-    if (propertyIds.isEmpty()) {
-      propertyIds.addAll(this.propertyIds);
-    } else {
-      if (predicate != null) {
-        propertyIds.addAll(PredicateHelper.getPropertyIds(predicate));
-      }
-      propertyIds.retainAll(this.propertyIds);
-    }
-
-    try {
-      Connection connection = connectionFactory.getConnection();
-
-      try {
-        String sql = getSQL(propertyIds, predicate, f_keys);
-
-//                System.out.println(sql);
-
-        Statement statement = connection.createStatement();
-        statement.setQueryTimeout(30);  // set timeout to 30 sec.
-
-        ResultSet rs = statement.executeQuery(sql);
-
-        while (rs.next()) {
-          ResultSetMetaData metaData = rs.getMetaData();
-          int columnCount = metaData.getColumnCount();
-
-          final ResourceImpl resource = new ResourceImpl(type);
-          for (int i = 1; i <= columnCount; ++i) {
-            PropertyIdImpl propertyId = new PropertyIdImpl(metaData.getColumnName(i), metaData.getTableName(i), false);
-
-//                        System.out.println(i + ")" + propertyId);
-
-            if (propertyIds.contains(propertyId)) {
-              resource.setProperty(propertyId, rs.getString(i));
-            }
-          }
-          resources.add(resource);
-        }
-
-      } finally {
-        connection.close();
-      }
-
-    } catch (SQLException e) {
-      throw new IllegalStateException("DB error : ", e);
-    }
-
-    return resources;
-  }
-
-  private String getSQL(Set<PropertyId> propertyIds, Predicate predicate, PropertyId[][] f_keys) {
-
-    StringBuilder columns = new StringBuilder();
-    Set<String> tableSet = new HashSet<String>();
-
-    for (PropertyId propertyId : propertyIds) {
-      if (columns.length() > 0) {
-        columns.append(", ");
-      }
-      columns.append(propertyId.getCategory()).append(".").append(propertyId.getName());
-      tableSet.add(propertyId.getCategory());
-    }
-
-
-    boolean haveWhereClause = false;
-    StringBuilder whereClause = new StringBuilder();
-    if (predicate != null &&
-        propertyIds.containsAll(PredicateHelper.getPropertyIds(predicate)) &&
-        predicate instanceof PredicateVisitorAcceptor) {
-
-      SQLPredicateVisitor visitor = new SQLPredicateVisitor();
-      ((PredicateVisitorAcceptor) predicate).accept(visitor);
-      whereClause.append(visitor.getSQL());
-      haveWhereClause = true;
-    }
-
-    StringBuilder joinClause = new StringBuilder();
-
-    if (f_keys != null) {
-      for (PropertyId[] f_key : f_keys) {
-        if (haveWhereClause || joinClause.length() > 0) {
-          joinClause.append(" and ");
-        }
-        String category1 = f_key[0].getCategory();
-        joinClause.append(category1).append(".").append(f_key[0].getName());
-        joinClause.append("=");
-        String category2 = f_key[1].getCategory();
-        joinClause.append(category2).append(".").append(f_key[1].getName());
-        tableSet.add(category1);
-        tableSet.add(category2);
-      }
-      haveWhereClause = true;
-    }
-
-    StringBuilder tables = new StringBuilder();
-
-    for (String table : tableSet) {
-      if (tables.length() > 0) {
-        tables.append(", ");
-      }
-      tables.append(table);
-    }
-
-    String sql = "select " + columns + " from " + tables;
-
-    if (haveWhereClause) {
-      sql = sql + " where " +
-          (whereClause == null ? "" : whereClause) +
-          (joinClause == null ? "" : joinClause);
-    }
-
-    System.out.println(sql);
-
-    return sql;
-  }
-
-  @Override
-  public Set<PropertyId> getPropertyIds() {
-    return propertyIds;
-  }
-
-  /**
-   * Factory method.
-   *
-   * @param connectionFactory the factory used to obtain a {@link Connection connection}
-   * @param type              the {@link Resource.Type resource type}
-   * @return a new {@link ResourceProvider} instance
-   */
-  public static ResourceProvider create(ConnectionFactory connectionFactory, Resource.Type type) {
-    return new JDBCResourceProvider(connectionFactory, type);
-  }
-
-}

+ 5 - 4
ambari-api/src/main/java/org/apache/ambari/api/controller/jdbc/SQLiteConnectionFactory.java

@@ -27,11 +27,12 @@ import java.sql.SQLException;
  */
  */
 public class SQLiteConnectionFactory implements ConnectionFactory {
 public class SQLiteConnectionFactory implements ConnectionFactory {
 
 
-  private static String DB_FILE_NAME = System.getProperty("ambariapi.dbfile", "src/test/resources/data.db");
+  public static final String CONNECTION_URL = "jdbc:sqlite:";
 
 
-  public static final String CONNECTION_URL = "jdbc:sqlite:" + DB_FILE_NAME;
+  private final String dbFile;
 
 
-  public SQLiteConnectionFactory() {
+  public SQLiteConnectionFactory(String dbFile) {
+    this.dbFile = dbFile;
     try {
     try {
       Class.forName("org.sqlite.JDBC");
       Class.forName("org.sqlite.JDBC");
     } catch (ClassNotFoundException e) {
     } catch (ClassNotFoundException e) {
@@ -41,6 +42,6 @@ public class SQLiteConnectionFactory implements ConnectionFactory {
 
 
   @Override
   @Override
   public Connection getConnection() throws SQLException {
   public Connection getConnection() throws SQLException {
-    return DriverManager.getConnection(CONNECTION_URL);
+    return DriverManager.getConnection(CONNECTION_URL + dbFile);
   }
   }
 }
 }

+ 229 - 0
ambari-api/src/main/java/org/apache/ambari/api/controller/spi/ManagementController.java

@@ -0,0 +1,229 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.api.controller.spi;
+
+import java.util.Set;
+
+/**
+ * Management controller interface.
+ */
+public interface ManagementController {
+
+
+  // ----- Create -----------------------------------------------------------
+
+  /**
+   * Create the clusters defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the clusters to be created
+   */
+  public void createClusters(Request request);
+
+  /**
+   * Create the services defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the services to be created
+   */
+  public void createServices(Request request);
+
+  /**
+   * Create the components defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the components to be created
+   */
+  public void createComponents(Request request);
+
+  /**
+   * Create the hosts defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the hosts to be created
+   */
+  public void createHosts(Request request);
+
+  /**
+   * Create the host components defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the host components to be created
+   */
+  public void createHostComponents(Request request);
+
+
+  // ----- Read -------------------------------------------------------------
+
+  /**
+   * Get a set of cluster {@link Resource resources} based on the given request and predicate
+   * information.
+   *
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   clusters are returned
+   * @return a set of cluster resources based on the given request and predicate information
+   */
+  public Set<Resource> getClusters(Request request, Predicate predicate);
+
+  /**
+   * Get a set of service {@link Resource resources} based on the given request and predicate
+   * information.
+   *
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   services are returned
+   * @return a set of service resources based on the given request and predicate information
+   */
+  public Set<Resource> getServices(Request request, Predicate predicate);
+
+  /**
+   * Get a set of component {@link Resource resources} based on the given request and predicate
+   * information.
+   *
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   components are returned
+   * @return a set of component resources based on the given request and predicate information
+   */
+  public Set<Resource> getComponents(Request request, Predicate predicate);
+
+  /**
+   * Get a set of host {@link Resource resources} based on the given request and predicate
+   * information.
+   *
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   hosts are returned
+   * @return a set of host resources based on the given request and predicate information
+   */
+  public Set<Resource> getHosts(Request request, Predicate predicate);
+
+  /**
+   * Get a set of host component {@link Resource resources} based on the given request and predicate
+   * information.
+   *
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   host components are returned
+   * @return a set of host component resources based on the given request and predicate information
+   */
+  public Set<Resource> getHostComponents(Request request, Predicate predicate);
+
+
+  // ----- Update -----------------------------------------------------------
+
+  /**
+   * Update the clusters selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the clusters to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   clusters are updated
+   */
+  public void updateClusters(Request request, Predicate predicate);
+
+  /**
+   * Update the services selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the services to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   services are updated
+   */
+  public void updateServices(Request request, Predicate predicate);
+
+  /**
+   * Update the components selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the components to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   components are updated
+   */
+  public void updateComponents(Request request, Predicate predicate);
+
+  /**
+   * Update the hosts selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the hosts to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   hosts are updated
+   */
+  public void updateHosts(Request request, Predicate predicate);
+
+  /**
+   * Update the host components selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the host components to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   host components are updated
+   */
+  public void updateHostComponents(Request request, Predicate predicate);
+
+
+  // ----- Delete -----------------------------------------------------------
+
+  /**
+   * Delete the clusters selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  clusters are deleted
+   */
+  public void deleteClusters(Predicate predicate);
+
+  /**
+   * Delete the services selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  services are deleted
+   */
+  public void deleteServices(Predicate predicate);
+
+  /**
+   * Delete the components selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  components are deleted
+   */
+  public void deleteComponents(Predicate predicate);
+
+  /**
+   * Delete the hosts selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  hosts are deleted
+   */
+  public void deleteHosts(Predicate predicate);
+
+  /**
+   * Delete the host components selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  host components are deleted
+   */
+  public void deleteHostComponents(Predicate predicate);
+}

+ 2 - 0
ambari-api/src/main/java/org/apache/ambari/api/controller/spi/Request.java

@@ -17,6 +17,7 @@
  */
  */
 package org.apache.ambari.api.controller.spi;
 package org.apache.ambari.api.controller.spi;
 
 
+import java.util.Map;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -34,6 +35,7 @@ public interface Request {
    */
    */
   public Set<PropertyId> getPropertyIds();
   public Set<PropertyId> getPropertyIds();
 
 
+  public Set<Map<PropertyId, String>> getProperties();
   /**
   /**
    * Get the {@link TemporalInfo temporal information} for the given property
    * Get the {@link TemporalInfo temporal information} for the given property
    * id for this request, if any.
    * id for this request, if any.

+ 31 - 3
ambari-api/src/main/java/org/apache/ambari/api/controller/spi/ResourceProvider.java

@@ -28,6 +28,15 @@ import java.util.Set;
  * for a given request.
  * for a given request.
  */
  */
 public interface ResourceProvider {
 public interface ResourceProvider {
+
+  /**
+   * Create the resources defined by the properties in the given request object.
+   *
+   * @param request  the request object which defines the set of properties
+   *                 for the resources to be created
+   */
+  public void createResources(Request request);
+
   /**
   /**
    * Get a set of {@link Resource resources} based on the given request and predicate
    * Get a set of {@link Resource resources} based on the given request and predicate
    * information.
    * information.
@@ -44,13 +53,32 @@ public interface ResourceProvider {
    * the resources of a given type and allow the calling cluster controller to filter
    * the resources of a given type and allow the calling cluster controller to filter
    * based on the predicate.
    * based on the predicate.
    *
    *
-   * @param request   the request object which defines the desired set of properties
-   * @param predicate the predicate object which can be used to filter which
-   *                  resources are returned
+   * @param request    the request object which defines the desired set of properties
+   * @param predicate  the predicate object which can be used to filter which
+   *                   resources are returned
    * @return a set of resources based on the given request and predicate information
    * @return a set of resources based on the given request and predicate information
    */
    */
   public Set<Resource> getResources(Request request, Predicate predicate);
   public Set<Resource> getResources(Request request, Predicate predicate);
 
 
+  /**
+   * Update the resources selected by the given predicate with the properties
+   * from the given request object.
+   *
+   * @param request    the request object which defines the set of properties
+   *                   for the resources to be updated
+   * @param predicate  the predicate object which can be used to filter which
+   *                   resources are updated
+   */
+  public void updateResources(Request request, Predicate predicate);
+
+  /**
+   * Delete the resources selected by the given predicate.
+   *
+   * @param predicate the predicate object which can be used to filter which
+   *                  resources are deleted
+   */
+  public void deleteResources(Predicate predicate);
+
   /**
   /**
    * Get the set of property ids for the properties that this provider can provide.
    * Get the set of property ids for the properties that this provider can provide.
    *
    *

+ 14 - 9
ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/ClusterControllerHelper.java

@@ -19,11 +19,13 @@
 package org.apache.ambari.api.controller.utilities;
 package org.apache.ambari.api.controller.utilities;
 
 
 import org.apache.ambari.api.controller.internal.ClusterControllerImpl;
 import org.apache.ambari.api.controller.internal.ClusterControllerImpl;
+import org.apache.ambari.api.controller.internal.PropertyIdImpl;
+import org.apache.ambari.api.controller.internal.ResourceProviderImpl;
 import org.apache.ambari.api.controller.internal.SchemaImpl;
 import org.apache.ambari.api.controller.internal.SchemaImpl;
-import org.apache.ambari.api.controller.jdbc.ConnectionFactory;
-import org.apache.ambari.api.controller.jdbc.JDBCResourceProvider;
-import org.apache.ambari.api.controller.jdbc.SQLiteConnectionFactory;
+import org.apache.ambari.api.controller.jdbc.JDBCManagementController;
 import org.apache.ambari.api.controller.spi.ClusterController;
 import org.apache.ambari.api.controller.spi.ClusterController;
+import org.apache.ambari.api.controller.spi.ManagementController;
+import org.apache.ambari.api.controller.spi.PropertyId;
 import org.apache.ambari.api.controller.spi.PropertyProvider;
 import org.apache.ambari.api.controller.spi.PropertyProvider;
 import org.apache.ambari.api.controller.spi.Resource;
 import org.apache.ambari.api.controller.spi.Resource;
 import org.apache.ambari.api.controller.spi.ResourceProvider;
 import org.apache.ambari.api.controller.spi.ResourceProvider;
@@ -40,11 +42,7 @@ import java.util.Map;
 public class ClusterControllerHelper {
 public class ClusterControllerHelper {
   private static ClusterController controller;
   private static ClusterController controller;
 
 
-  public static final ConnectionFactory CONNECTION_FACTORY = new SQLiteConnectionFactory();
-
-
   public static synchronized ClusterController getClusterController() {
   public static synchronized ClusterController getClusterController() {
-
     if (controller == null) {
     if (controller == null) {
       controller = new ClusterControllerImpl(getResourceSchemas());
       controller = new ClusterControllerImpl(getResourceSchemas());
     }
     }
@@ -55,15 +53,22 @@ public class ClusterControllerHelper {
     Map<Resource.Type, Schema> schemas = new HashMap<Resource.Type, Schema>();
     Map<Resource.Type, Schema> schemas = new HashMap<Resource.Type, Schema>();
 
 
     schemas.put(Resource.Type.Cluster, getResourceSchema(Resource.Type.Cluster));
     schemas.put(Resource.Type.Cluster, getResourceSchema(Resource.Type.Cluster));
+    schemas.put(Resource.Type.Service, getResourceSchema(Resource.Type.Service));
+    schemas.put(Resource.Type.Host, getResourceSchema(Resource.Type.Host));
+    schemas.put(Resource.Type.Component, getResourceSchema(Resource.Type.Component));
+    schemas.put(Resource.Type.HostComponent, getResourceSchema(Resource.Type.HostComponent));
 
 
     return schemas;
     return schemas;
   }
   }
 
 
   private static Schema getResourceSchema(Resource.Type type) {
   private static Schema getResourceSchema(Resource.Type type) {
 
 
-    ResourceProvider resourceProvider =  JDBCResourceProvider.create(CONNECTION_FACTORY, type);
+    ManagementController managementController = new JDBCManagementController(DBHelper.CONNECTION_FACTORY);
+
+    ResourceProvider resourceProvider = ResourceProviderImpl.getResourceProvider(type, Properties.getPropertyIds(type, "DB"), managementController);
+
     List<PropertyProvider> propertyProviders = new LinkedList<PropertyProvider>();
     List<PropertyProvider> propertyProviders = new LinkedList<PropertyProvider>();
 
 
-    return new SchemaImpl(type, resourceProvider, propertyProviders, Properties.getKeyPropertyIds(type));
+    return new SchemaImpl(resourceProvider, propertyProviders, Properties.getKeyPropertyIds(type));
   }
   }
 }
 }

+ 2 - 1
ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/DBHelper.java

@@ -34,8 +34,9 @@ import java.util.Map;
  *
  *
  */
  */
 public class DBHelper {
 public class DBHelper {
+  private static String DB_FILE_NAME = System.getProperty("ambariapi.dbfile", "src/test/resources/data.db");
 
 
-  public static final ConnectionFactory CONNECTION_FACTORY = new SQLiteConnectionFactory();
+  public static final ConnectionFactory CONNECTION_FACTORY = new SQLiteConnectionFactory(DB_FILE_NAME);
 
 
   private static final Map<String, String> HOSTS = readHosts();
   private static final Map<String, String> HOSTS = readHosts();
 
 

+ 9 - 10
ambari-api/src/main/java/org/apache/ambari/api/controller/utilities/Properties.java

@@ -36,8 +36,8 @@ public class Properties {
   private static final String PROPERTIES_FILE = "properties.json";
   private static final String PROPERTIES_FILE = "properties.json";
   private static final String KEY_PROPERTIES_FILE = "key_properties.json";
   private static final String KEY_PROPERTIES_FILE = "key_properties.json";
 
 
-  private static final Map<String, Map<String, Set<PropertyId>>> PROPERTY_IDS = readPropertyIds(PROPERTIES_FILE);
-  private static final Map<String, Map<String, PropertyId>> KEY_PROPERTY_IDS = readKeyPropertyIds(KEY_PROPERTIES_FILE);
+  private static final Map<Resource.Type, Map<String, Set<PropertyId>>> PROPERTY_IDS = readPropertyIds(PROPERTIES_FILE);
+  private static final Map<Resource.Type, Map<Resource.Type, PropertyId>> KEY_PROPERTY_IDS = readKeyPropertyIds(KEY_PROPERTIES_FILE);
 
 
   public static PropertyId getPropertyId(String name, String category) {
   public static PropertyId getPropertyId(String name, String category) {
     return new PropertyIdImpl(name, category, false);
     return new PropertyIdImpl(name, category, false);
@@ -49,37 +49,36 @@ public class Properties {
 
 
   public static Set<PropertyId> getPropertyIds(Resource.Type resourceType, String providerKey) {
   public static Set<PropertyId> getPropertyIds(Resource.Type resourceType, String providerKey) {
 
 
-    Map<String, Set<PropertyId>> propertyIds = PROPERTY_IDS.get(resourceType.toString());
+    Map<String, Set<PropertyId>> propertyIds = PROPERTY_IDS.get(resourceType);
     if (propertyIds != null) {
     if (propertyIds != null) {
       return propertyIds.get(providerKey);
       return propertyIds.get(providerKey);
     }
     }
     return Collections.emptySet();
     return Collections.emptySet();
   }
   }
 
 
-  public static Map<String, PropertyId> getKeyPropertyIds(Resource.Type resourceType) {
-    return KEY_PROPERTY_IDS.get(resourceType.toString());
+  public static Map<Resource.Type, PropertyId> getKeyPropertyIds(Resource.Type resourceType) {
+    return KEY_PROPERTY_IDS.get(resourceType);
   }
   }
 
 
-  private static Map<String, Map<String, Set<PropertyId>>> readPropertyIds(String filename) {
+  private static Map<Resource.Type, Map<String, Set<PropertyId>>> readPropertyIds(String filename) {
     ObjectMapper mapper = new ObjectMapper();
     ObjectMapper mapper = new ObjectMapper();
 
 
     try {
     try {
-      return mapper.readValue(ClassLoader.getSystemResourceAsStream(filename), new TypeReference<Map<String, Map<String, Set<PropertyIdImpl>>>>() {
+      return mapper.readValue(ClassLoader.getSystemResourceAsStream(filename), new TypeReference<Map<Resource.Type, Map<String, Set<PropertyIdImpl>>>>() {
       });
       });
     } catch (IOException e) {
     } catch (IOException e) {
       throw new IllegalStateException("Can't read properties file " + filename, e);
       throw new IllegalStateException("Can't read properties file " + filename, e);
     }
     }
   }
   }
 
 
-  private static Map<String, Map<String, PropertyId>> readKeyPropertyIds(String filename) {
+  private static Map<Resource.Type, Map<Resource.Type, PropertyId>> readKeyPropertyIds(String filename) {
     ObjectMapper mapper = new ObjectMapper();
     ObjectMapper mapper = new ObjectMapper();
 
 
     try {
     try {
-      return mapper.readValue(ClassLoader.getSystemResourceAsStream(filename), new TypeReference<Map<String, Map<String, PropertyIdImpl>>>() {
+      return mapper.readValue(ClassLoader.getSystemResourceAsStream(filename), new TypeReference<Map<Resource.Type, Map<Resource.Type, PropertyIdImpl>>>() {
       });
       });
     } catch (IOException e) {
     } catch (IOException e) {
       throw new IllegalStateException("Can't read properties file " + filename, e);
       throw new IllegalStateException("Can't read properties file " + filename, e);
     }
     }
   }
   }
-
 }
 }

+ 1 - 1
ambari-api/src/main/java/org/apache/ambari/api/query/QueryImpl.java

@@ -159,7 +159,7 @@ public class QueryImpl implements Query {
         setProperties.add(new PropertyIdImpl(property, group, false));
         setProperties.add(new PropertyIdImpl(property, group, false));
       }
       }
     }
     }
-    return new RequestImpl(setProperties);
+    return new RequestImpl(setProperties, null);
   }
   }
 
 
   //todo
   //todo

+ 3 - 3
ambari-api/src/main/resources/key_properties.json

@@ -14,7 +14,7 @@
     },
     },
     "Service":{
     "Service":{
       "name":"service_name",
       "name":"service_name",
-      "category":"Services",
+      "category":"ServiceInfo",
       "temporal":false
       "temporal":false
     }
     }
   },
   },
@@ -38,12 +38,12 @@
     },
     },
     "Service":{
     "Service":{
       "name":"service_name",
       "name":"service_name",
-      "category":"ServiceComponents",
+      "category":"ServiceComponentInfo",
       "temporal":false
       "temporal":false
     },
     },
     "Component":{
     "Component":{
       "name":"component_name",
       "name":"component_name",
-      "category":"ServiceComponents",
+      "category":"ServiceComponentInfo",
       "temporal":false
       "temporal":false
     }
     }
   },
   },

+ 21 - 21
ambari-api/src/main/resources/properties.json

@@ -26,32 +26,32 @@
     "DB":[
     "DB":[
       {
       {
         "name":"service_name",
         "name":"service_name",
-        "category":"Services",
+        "category":"ServiceInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"description",
-        "category":"Services",
+        "name":"cluster_name",
+        "category":"ServiceInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"display_name",
-        "category":"Services",
+        "name":"state",
+        "category":"ServiceInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"attributes",
+        "name":"description",
         "category":"Services",
         "category":"Services",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"cluster_name",
-        "category":"ServiceInfo",
+        "name":"display_name",
+        "category":"Services",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"state",
-        "category":"ServiceInfo",
+        "name":"attributes",
+        "category":"Services",
         "temporal":false
         "temporal":false
       }
       }
     ],
     ],
@@ -107,32 +107,32 @@
     "DB":[
     "DB":[
       {
       {
         "name":"service_name",
         "name":"service_name",
-        "category":"ServiceComponents",
+        "category":"ServiceComponentInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
         "name":"component_name",
         "name":"component_name",
-        "category":"ServiceComponents",
+        "category":"ServiceComponentInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"display_name",
-        "category":"ServiceComponents",
+        "name":"cluster_name",
+        "category":"ServiceComponentInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"description",
-        "category":"ServiceComponents",
+        "name":"state",
+        "category":"ServiceComponentInfo",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"cluster_name",
-        "category":"ServiceComponentInfo",
+        "name":"display_name",
+        "category":"ServiceComponents",
         "temporal":false
         "temporal":false
       },
       },
       {
       {
-        "name":"state",
-        "category":"ServiceComponentInfo",
+        "name":"description",
+        "category":"ServiceComponents",
         "temporal":false
         "temporal":false
       }
       }
     ],
     ],
@@ -169,7 +169,7 @@
         "temporal":false
         "temporal":false
       }
       }
     ],
     ],
-    "JMX":[
+      "JMX":[
       {
       {
         "name":"memNonHeapUsedM",
         "name":"memNonHeapUsedM",
         "category":"jvm",
         "category":"jvm",

+ 41 - 5
ambari-api/src/test/java/org/apache/ambari/api/controller/internal/ClusterControllerImplTest.java

@@ -28,10 +28,10 @@ import org.apache.ambari.api.controller.spi.Resource;
 import org.apache.ambari.api.controller.spi.ResourceProvider;
 import org.apache.ambari.api.controller.spi.ResourceProvider;
 import org.apache.ambari.api.controller.spi.Schema;
 import org.apache.ambari.api.controller.spi.Schema;
 import org.apache.ambari.api.controller.utilities.PredicateBuilder;
 import org.apache.ambari.api.controller.utilities.PredicateBuilder;
-import org.apache.ambari.api.controller.utilities.PredicateHelper;
 import org.apache.ambari.api.controller.utilities.Properties;
 import org.apache.ambari.api.controller.utilities.Properties;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.LinkedList;
@@ -72,6 +72,21 @@ public class ClusterControllerImplTest {
       return resources;
       return resources;
     }
     }
 
 
+    @Override
+    public void createResources(Request request) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     @Override
     @Override
     public Set<PropertyId> getPropertyIds() {
     public Set<PropertyId> getPropertyIds() {
       return resourceProviderProperties;
       return resourceProviderProperties;
@@ -114,12 +129,22 @@ public class ClusterControllerImplTest {
     propertyProviders.add(propertyProvider);
     propertyProviders.add(propertyProvider);
   }
   }
 
 
-  private static final Map<String, PropertyId> keyPropertyIds = new HashMap<String, PropertyId>();
+  private static final Map<Resource.Type, PropertyId> keyPropertyIds = new HashMap<Resource.Type, PropertyId>();
 
 
   private static Map<Resource.Type, Schema> schemas = new HashMap<Resource.Type, Schema>();
   private static Map<Resource.Type, Schema> schemas = new HashMap<Resource.Type, Schema>();
 
 
+  private static final SchemaImpl hostSchema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
+  private static final SchemaImpl serviceSchema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
+  private static final SchemaImpl clusterSchema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
+  private static final SchemaImpl componentSchema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
+  private static final SchemaImpl hostComponentSchema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
+
   static {
   static {
-    schemas.put(Resource.Type.Host, new SchemaImpl(Resource.Type.HostComponent, resourceProvider, propertyProviders, keyPropertyIds));
+    schemas.put(Resource.Type.Host, hostSchema);
+    schemas.put(Resource.Type.Service, serviceSchema);
+    schemas.put(Resource.Type.Cluster, clusterSchema);
+    schemas.put(Resource.Type.Component, componentSchema);
+    schemas.put(Resource.Type.HostComponent, hostComponentSchema);
   }
   }
 
 
   private static final Set<PropertyId> propertyIds = new HashSet<PropertyId>();
   private static final Set<PropertyId> propertyIds = new HashSet<PropertyId>();
@@ -137,7 +162,7 @@ public class ClusterControllerImplTest {
   public void testGetResources() {
   public void testGetResources() {
     ClusterController controller = new ClusterControllerImpl(schemas);
     ClusterController controller = new ClusterControllerImpl(schemas);
 
 
-    Request request = new RequestImpl(propertyIds);
+    Request request = new RequestImpl(propertyIds, null);
 
 
     Iterable<Resource> iterable = controller.getResources(Resource.Type.Host, request, null);
     Iterable<Resource> iterable = controller.getResources(Resource.Type.Host, request, null);
 
 
@@ -153,7 +178,7 @@ public class ClusterControllerImplTest {
   public void testGetResourcesWithPredicate() {
   public void testGetResourcesWithPredicate() {
     ClusterController controller = new ClusterControllerImpl(schemas);
     ClusterController controller = new ClusterControllerImpl(schemas);
 
 
-    Request request = new RequestImpl(propertyIds);
+    Request request = new RequestImpl(propertyIds, null);
 
 
     Predicate predicate = new PredicateBuilder().property("p2", "c1").equals(1).toPredicate();
     Predicate predicate = new PredicateBuilder().property("p2", "c1").equals(1).toPredicate();
 
 
@@ -166,6 +191,17 @@ public class ClusterControllerImplTest {
     }
     }
     Assert.assertEquals(2, cnt);
     Assert.assertEquals(2, cnt);
   }
   }
+
+  @Test
+  public void testGetSchema() {
+    ClusterController controller = new ClusterControllerImpl(schemas);
+
+    Assert.assertSame(hostSchema, controller.getSchema(Resource.Type.Host));
+    Assert.assertSame(serviceSchema, controller.getSchema(Resource.Type.Service));
+    Assert.assertSame(clusterSchema, controller.getSchema(Resource.Type.Cluster));
+    Assert.assertSame(componentSchema, controller.getSchema(Resource.Type.Component));
+    Assert.assertSame(hostComponentSchema, controller.getSchema(Resource.Type.HostComponent));
+  }
 }
 }
 
 
 
 

+ 1 - 1
ambari-api/src/test/java/org/apache/ambari/api/controller/internal/RequestImplTest.java

@@ -43,7 +43,7 @@ public class RequestImplTest {
 
 
   @Test
   @Test
   public void testGetPropertyIds() {
   public void testGetPropertyIds() {
-    Request request = new RequestImpl(propertyIds);
+    Request request = new RequestImpl(propertyIds, null);
 
 
     Assert.assertEquals(propertyIds, request.getPropertyIds());
     Assert.assertEquals(propertyIds, request.getPropertyIds());
   }
   }

+ 23 - 8
ambari-api/src/test/java/org/apache/ambari/api/controller/internal/SchemaImplTest.java

@@ -56,6 +56,21 @@ public class SchemaImplTest {
       return null;
       return null;
     }
     }
 
 
+    @Override
+    public void createResources(Request request) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public void updateResources(Request request, Predicate predicate) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public void deleteResources(Predicate predicate) {
+      //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     @Override
     @Override
     public Set<PropertyId> getPropertyIds() {
     public Set<PropertyId> getPropertyIds() {
       return resourceProviderProperties;
       return resourceProviderProperties;
@@ -89,17 +104,17 @@ public class SchemaImplTest {
     propertyProviders.add(propertyProvider);
     propertyProviders.add(propertyProvider);
   }
   }
 
 
-  private static final Map<String, PropertyId> keyPropertyIds = new HashMap<String, PropertyId>();
+  private static final Map<Resource.Type, PropertyId> keyPropertyIds = new HashMap<Resource.Type, PropertyId>();
 
 
   static {
   static {
-    keyPropertyIds.put(Resource.Type.Cluster.toString(), Properties.getPropertyId("p1", "c1"));
-    keyPropertyIds.put(Resource.Type.Host.toString(), Properties.getPropertyId("p2", "c1"));
-    keyPropertyIds.put(Resource.Type.Component.toString(), Properties.getPropertyId("p3", "c1"));
+    keyPropertyIds.put(Resource.Type.Cluster, Properties.getPropertyId("p1", "c1"));
+    keyPropertyIds.put(Resource.Type.Host, Properties.getPropertyId("p2", "c1"));
+    keyPropertyIds.put(Resource.Type.Component, Properties.getPropertyId("p3", "c1"));
   }
   }
 
 
   @Test
   @Test
   public void testGetKeyPropertyId() {
   public void testGetKeyPropertyId() {
-    Schema schema = new SchemaImpl(Resource.Type.HostComponent, resourceProvider, propertyProviders, keyPropertyIds);
+    Schema schema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
 
 
     Assert.assertEquals(Properties.getPropertyId("p1", "c1"), schema.getKeyPropertyId(Resource.Type.Cluster));
     Assert.assertEquals(Properties.getPropertyId("p1", "c1"), schema.getKeyPropertyId(Resource.Type.Cluster));
     Assert.assertEquals(Properties.getPropertyId("p2", "c1"), schema.getKeyPropertyId(Resource.Type.Host));
     Assert.assertEquals(Properties.getPropertyId("p2", "c1"), schema.getKeyPropertyId(Resource.Type.Host));
@@ -108,7 +123,7 @@ public class SchemaImplTest {
 
 
   @Test
   @Test
   public void testGetCategories() {
   public void testGetCategories() {
-    Schema schema = new SchemaImpl(Resource.Type.HostComponent, resourceProvider, propertyProviders, keyPropertyIds);
+    Schema schema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
 
 
     Map<String, Set<String>> categories = schema.getCategories();
     Map<String, Set<String>> categories = schema.getCategories();
     Assert.assertEquals(4, categories.size());
     Assert.assertEquals(4, categories.size());
@@ -140,14 +155,14 @@ public class SchemaImplTest {
 
 
   @Test
   @Test
   public void testGetResourceProvider() {
   public void testGetResourceProvider() {
-    Schema schema = new SchemaImpl(Resource.Type.HostComponent, resourceProvider, propertyProviders, keyPropertyIds);
+    Schema schema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
 
 
     Assert.assertSame(resourceProvider, schema.getResourceProvider());
     Assert.assertSame(resourceProvider, schema.getResourceProvider());
   }
   }
 
 
   @Test
   @Test
   public void testGetPropertyProviders() {
   public void testGetPropertyProviders() {
-    Schema schema = new SchemaImpl(Resource.Type.HostComponent, resourceProvider, propertyProviders, keyPropertyIds);
+    Schema schema = new SchemaImpl(resourceProvider, propertyProviders, keyPropertyIds);
 
 
     Assert.assertSame(propertyProviders, schema.getPropertyProviders());
     Assert.assertSame(propertyProviders, schema.getPropertyProviders());
   }
   }

+ 170 - 0
ambari-api/src/test/java/org/apache/ambari/api/controller/jdbc/JDBCManagementControllerTest.java

@@ -0,0 +1,170 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.api.controller.jdbc;
+
+import org.apache.ambari.api.controller.internal.RequestImpl;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.PropertyId;
+import org.apache.ambari.api.controller.spi.Request;
+import org.apache.ambari.api.controller.utilities.PredicateBuilder;
+import org.apache.ambari.api.controller.utilities.Properties;
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+/**
+ *
+ */
+public class JDBCManagementControllerTest {
+
+  @Test
+  public void testCreateCluster() throws Exception {
+    ConnectionFactory connectionFactory = createNiceMock(ConnectionFactory.class);
+    Connection connection = createNiceMock(Connection.class);
+    Statement statement = createNiceMock(Statement.class);
+
+    expect(connectionFactory.getConnection()).andReturn(connection).once();
+    expect(connection.createStatement()).andReturn(statement).once();
+    expect(statement.execute("insert into Clusters (state, version, cluster_name) values ('initial', '1.0', 'MyCluster')")).andReturn(true).once();
+
+    replay(connectionFactory, connection, statement);
+
+    JDBCManagementController provider =  new JDBCManagementController(connectionFactory);
+
+    Map<PropertyId, String> properties = new HashMap<PropertyId, String>();
+
+    PropertyId id = Properties.getPropertyId("cluster_name", "Clusters");
+    properties.put(id, "MyCluster");
+
+    id = Properties.getPropertyId("version", "Clusters");
+    properties.put(id, "1.0");
+
+    id = Properties.getPropertyId("state", "Clusters");
+    properties.put(id, "initial");
+
+    Set<Map<PropertyId, String>> propertySet = new HashSet<Map<PropertyId, String>>();
+    propertySet.add(properties);
+
+    Request request = new RequestImpl(null, propertySet);
+
+    provider.createClusters(request);
+
+    verify(connectionFactory, connection, statement);
+  }
+
+  @Test
+  public void testCreateService() throws Exception{
+
+    ConnectionFactory connectionFactory = createNiceMock(ConnectionFactory.class);
+    Connection connection = createNiceMock(Connection.class);
+    Statement statement = createNiceMock(Statement.class);
+
+    expect(connectionFactory.getConnection()).andReturn(connection).once();
+    expect(connection.createStatement()).andReturn(statement).once();
+    expect(statement.execute("insert into ServiceInfo (service_name, cluster_name, state) values ('MyService', 'MyCluster', 'initial')")).andReturn(true).once();
+
+    replay(connectionFactory, connection, statement);
+
+    JDBCManagementController provider =  new JDBCManagementController(connectionFactory);
+
+    Map<PropertyId, String> properties = new HashMap<PropertyId, String>();
+
+    PropertyId id = Properties.getPropertyId("cluster_name", "ServiceInfo");
+    properties.put(id, "MyCluster");
+
+    id = Properties.getPropertyId("service_name", "ServiceInfo");
+    properties.put(id, "MyService");
+
+    id = Properties.getPropertyId("state", "ServiceInfo");
+    properties.put(id, "initial");
+
+    Set<Map<PropertyId, String>> propertySet = new HashSet<Map<PropertyId, String>>();
+    propertySet.add(properties);
+
+    Request request = new RequestImpl(null, propertySet);
+
+    provider.createServices(request);
+
+    verify(connectionFactory, connection, statement);
+  }
+
+  @Test
+  public void testDeleteCluster() throws Exception{
+
+    ConnectionFactory connectionFactory = createNiceMock(ConnectionFactory.class);
+    Connection connection = createNiceMock(Connection.class);
+    Statement statement = createNiceMock(Statement.class);
+
+    expect(connectionFactory.getConnection()).andReturn(connection).once();
+    expect(connection.createStatement()).andReturn(statement).once();
+    expect(statement.execute("delete from Clusters where Clusters.cluster_name = \"MyCluster\"")).andReturn(true).once();
+
+    replay(connectionFactory, connection, statement);
+
+    JDBCManagementController provider =  new JDBCManagementController(connectionFactory);
+
+    Predicate predicate = new PredicateBuilder().property("cluster_name", "Clusters").equals("MyCluster").toPredicate();
+
+    provider.deleteServices(predicate);
+
+    verify(connectionFactory, connection, statement);
+  }
+
+  @Test
+  public void testUpdateCluster() throws Exception{
+
+    ConnectionFactory connectionFactory = createNiceMock(ConnectionFactory.class);
+    Connection connection = createNiceMock(Connection.class);
+    Statement statement = createNiceMock(Statement.class);
+
+    expect(connectionFactory.getConnection()).andReturn(connection).once();
+    expect(connection.createStatement()).andReturn(statement).once();
+    expect(statement.execute("update Clusters set state = 'running' where Clusters.cluster_name = \"MyCluster\"")).andReturn(true).once();
+
+    replay(connectionFactory, connection, statement);
+
+    JDBCManagementController provider =  new JDBCManagementController(connectionFactory);
+
+    Map<PropertyId, String> properties = new HashMap<PropertyId, String>();
+
+    PropertyId id = Properties.getPropertyId("state", "Clusters");
+    properties.put(id, "running");
+
+    Predicate predicate = new PredicateBuilder().property("cluster_name", "Clusters").equals("MyCluster").toPredicate();
+
+    Set<Map<PropertyId, String>> propertySet = new HashSet<Map<PropertyId, String>>();
+    propertySet.add(properties);
+
+    Request request = new RequestImpl(null, propertySet);
+
+    provider.updateClusters(request, predicate);
+
+    verify(connectionFactory, connection, statement);
+  }
+}

+ 9 - 4
ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java

@@ -19,6 +19,7 @@ package org.apache.ambari.api.controller.utilities;
 
 
 import org.apache.ambari.api.controller.spi.PropertyId;
 import org.apache.ambari.api.controller.spi.PropertyId;
 import org.apache.ambari.api.controller.spi.Resource;
 import org.apache.ambari.api.controller.spi.Resource;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.Test;
 
 
 import java.util.Map;
 import java.util.Map;
@@ -29,26 +30,30 @@ import java.util.Set;
  */
  */
 public class PropertiesTest {
 public class PropertiesTest {
 
 
+  @Ignore
   @Test
   @Test
   public void testGetPropertyIds() throws Exception {
   public void testGetPropertyIds() throws Exception {
 
 
 
 
     Set<PropertyId> propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "DB");
     Set<PropertyId> propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "DB");
 
 
+    System.out.println("DB");
     for (PropertyId propertyId : propertyIds) {
     for (PropertyId propertyId : propertyIds) {
-//            System.out.println(propertyId);
+            System.out.println(propertyId);
     }
     }
 
 
     propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "JMX");
     propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "JMX");
 
 
+    System.out.println("JMX");
     for (PropertyId propertyId : propertyIds) {
     for (PropertyId propertyId : propertyIds) {
-//            System.out.println(propertyId);
+            System.out.println(propertyId);
     }
     }
 
 
     propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "GANGLIA");
     propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "GANGLIA");
 
 
+    System.out.println("GANGLIA");
     for (PropertyId propertyId : propertyIds) {
     for (PropertyId propertyId : propertyIds) {
-//            System.out.println(propertyId);
+            System.out.println(propertyId);
     }
     }
   }
   }
 
 
@@ -56,7 +61,7 @@ public class PropertiesTest {
   @Test
   @Test
   public void testGetKeyPropertyIds() throws Exception {
   public void testGetKeyPropertyIds() throws Exception {
 
 
-    Map<String, PropertyId> keyProperties = Properties.getKeyPropertyIds(Resource.Type.Service);
+    Map<Resource.Type, PropertyId> keyProperties = Properties.getKeyPropertyIds(Resource.Type.Service);
 
 
     System.out.println(keyProperties);
     System.out.println(keyProperties);
   }
   }