ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject ambari git commit: AMBARI-12949 - Downgrade fails with err: 500 status code received on POST method for API: /api/vi/clusters/mycluster/upgrades (Di Li via jonathanhurley)
Date Fri, 18 Sep 2015 23:12:35 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 9dd623abb -> c8595d6e9


AMBARI-12949 - Downgrade fails with err: 500 status code received on POST method for API:
/api/vi/clusters/mycluster/upgrades (Di Li via jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c8595d6e
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c8595d6e
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c8595d6e

Branch: refs/heads/trunk
Commit: c8595d6e9a60584f371a513c8dd3579a53f66d69
Parents: 9dd623a
Author: Jonathan Hurley <jhurley@hortonworks.com>
Authored: Fri Sep 18 19:12:19 2015 -0400
Committer: Jonathan Hurley <jhurley@hortonworks.com>
Committed: Fri Sep 18 19:12:19 2015 -0400

----------------------------------------------------------------------
 .../ambari/server/orm/dao/ClusterDAO.java       |  26 ++
 .../orm/entities/ClusterConfigEntity.java       |   5 +-
 .../server/state/cluster/ClusterImpl.java       |  49 ++-
 .../server/orm/dao/ServiceConfigDAOTest.java    | 346 ++++++++++++++++++-
 4 files changed, 410 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/c8595d6e/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
index 6e55128..609d0de 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
@@ -203,6 +203,32 @@ public class ClusterDAO {
   }
 
   /**
+   * Gets the latest configurations for a given stack for all of the
+   * configurations of the specified cluster.
+   *
+   * @param clusterId
+   *          the cluster that the service is a part of.
+   * @param stackId
+   *          the stack to get the latest configurations for (not {@code null}).
+   * @return the latest configurations for the specified cluster and stack.
+   */
+  @RequiresSession
+  public List<ClusterConfigMappingEntity> getClusterConfigMappingsByStack(long clusterId,
+      StackId stackId) {
+    StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
+        stackId.getStackVersion());
+
+    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createNamedQuery(
+        "ClusterConfigEntity.findClusterConfigMappingsByStack",
+        ClusterConfigMappingEntity.class);
+
+    query.setParameter("clusterId", clusterId);
+    query.setParameter("stack", stackEntity);
+
+    return daoUtils.selectList(query);
+  }  
+  
+  /**
    * Create Cluster entity in Database
    * @param clusterEntity entity to create
    */

http://git-wip-us.apache.org/repos/asf/ambari/blob/c8595d6e/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
index 899ffe8..d8d7aa4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java
@@ -51,7 +51,10 @@ import javax.persistence.UniqueConstraint;
 @NamedQueries({
     @NamedQuery(name = "ClusterConfigEntity.findNextConfigVersion", query = "SELECT COALESCE(MAX(clusterConfig.version),0)
+ 1 as nextVersion FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.type=:configType
AND clusterConfig.clusterId=:clusterId"),
     @NamedQuery(name = "ClusterConfigEntity.findAllConfigsByStack", query = "SELECT clusterConfig
FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.stack=:stack"),
-    @NamedQuery(name = "ClusterConfigEntity.findLatestConfigsByStack", query = "SELECT clusterConfig
FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.timestamp
= (SELECT MAX(clusterConfig2.timestamp) FROM ClusterConfigEntity clusterConfig2 WHERE clusterConfig2.clusterId=:clusterId
AND clusterConfig2.stack=:stack AND clusterConfig2.type = clusterConfig.type)") })
+    @NamedQuery(name = "ClusterConfigEntity.findLatestConfigsByStack", query = "SELECT clusterConfig
FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.timestamp
= (SELECT MAX(clusterConfig2.timestamp) FROM ClusterConfigEntity clusterConfig2 WHERE clusterConfig2.clusterId=:clusterId
AND clusterConfig2.stack=:stack AND clusterConfig2.type = clusterConfig.type)"),
+    @NamedQuery(name = "ClusterConfigEntity.findClusterConfigMappingsByStack", query = "SELECT
clusterConfigMapping FROM ClusterConfigMappingEntity clusterConfigMapping WHERE clusterConfigMapping.clusterId=:clusterId
and clusterConfigMapping.tag in ( select clusterConfig.tag from ClusterConfigEntity clusterConfig
where clusterConfig.stack = :stack)")
+})
+
 public class ClusterConfigEntity {
 
   @Id

http://git-wip-us.apache.org/repos/asf/ambari/blob/c8595d6e/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index 4b20cba..eda2534 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -2855,27 +2855,29 @@ public class ClusterImpl implements Cluster {
     clusterGlobalLock.writeLock().lock();
     try {
       Collection<ClusterConfigMappingEntity> configMappingEntities = clusterEntity.getConfigMappingEntities();
-
+      
       // disable previous config
       for (ClusterConfigMappingEntity e : configMappingEntities) {
+        LOG.debug("{} with tag {} is unselected", e.getType(), e.getTag());
         e.setSelected(0);
       }
-
-      List<ClusterConfigEntity> clusterConfigsToMakeSelected = clusterDAO.getLatestConfigurations(
-          clusterEntity.getClusterId(), stackId);
-
-      for( ClusterConfigEntity clusterConfigToMakeSelected : clusterConfigsToMakeSelected
){
-        for (ClusterConfigMappingEntity configMappingEntity : configMappingEntities) {
-          String tag = configMappingEntity.getTag();
-          String type = configMappingEntity.getType();
-
-          if (clusterConfigToMakeSelected.getTag().equals(tag)
-              && clusterConfigToMakeSelected.getType().equals(type)) {
-            configMappingEntity.setSelected(1);
+      
+      List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(),
stackId);
+      Collection<ClusterConfigMappingEntity> latestConfigMappingByStack = getLatestConfigMapping(clusterConfigMappingEntities);
+      
+      for(ClusterConfigMappingEntity e: configMappingEntities){
+        String type = e.getType(); //loop thru all the config mappings
+        String tag =  e.getTag();
+        for (ClusterConfigMappingEntity latest : latestConfigMappingByStack) {
+          String t = latest.getType();
+          String tagLatest = latest.getTag();
+          if(type.equals(t) && tag.equals(tagLatest) ){//find the latest config of
a given mapping entity
+            LOG.info("{} with version tag {} is selected for stack {}", type, tag, stackId.toString());
+            e.setSelected(1);
           }
         }
       }
-
+      
       clusterEntity = clusterDAO.merge(clusterEntity);
 
       cacheConfigurations();
@@ -2884,6 +2886,25 @@ public class ClusterImpl implements Cluster {
     }
   }
 
+  public Collection<ClusterConfigMappingEntity> getLatestConfigMapping(List<ClusterConfigMappingEntity>
clusterConfigMappingEntities){
+    Map<String, ClusterConfigMappingEntity> temp = new HashMap<String, ClusterConfigMappingEntity>();
+    for (ClusterConfigMappingEntity e : clusterConfigMappingEntities) {
+      String type = e.getType();
+      if(temp.containsKey(type)){
+        ClusterConfigMappingEntity entityStored = (ClusterConfigMappingEntity)temp.get(type);
+        Long timestampStored = entityStored.getCreateTimestamp();
+        Long timestamp = e.getCreateTimestamp();
+        if(timestamp > timestampStored){
+          temp.put(type, e); //find a newer config for the given type
+        }
+      } else {
+        temp.put(type, e); //first time encounter a type, add it
+      }
+    }
+
+    return (Collection<ClusterConfigMappingEntity>) temp.values();
+  }
+
   /**
    * {@inheritDoc}
    */

http://git-wip-us.apache.org/repos/asf/ambari/blob/c8595d6e/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
index 4c186b5..7873abb 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ServiceConfigDAOTest.java
@@ -17,6 +17,11 @@
  */
 package org.apache.ambari.server.orm.dao;
 
+import static org.easymock.EasyMock.createMockBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 
 import junit.framework.Assert;
@@ -26,12 +31,16 @@ import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
+import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
+import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
 import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
 import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.state.StackId;
+import org.apache.ambari.server.state.cluster.ClusterImpl;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -49,6 +58,8 @@ public class ServiceConfigDAOTest {
   private ClusterDAO clusterDAO;
   private ResourceTypeDAO resourceTypeDAO;
   private StackDAO stackDAO;
+  private ConfigGroupDAO configGroupDAO;
+  private ConfigGroupConfigMappingDAO configGroupConfigMappingDAO;
 
   @Before
   public void setup() throws Exception {
@@ -62,6 +73,8 @@ public class ServiceConfigDAOTest {
     stackDAO = injector.getInstance(StackDAO.class);
     serviceConfigDAO = injector.getInstance(ServiceConfigDAO.class);
     resourceTypeDAO = injector.getInstance(ResourceTypeDAO.class);
+    configGroupDAO = injector.getInstance(ConfigGroupDAO.class);
+    configGroupConfigMappingDAO = injector.getInstance(ConfigGroupConfigMappingDAO.class);
   }
 
   @After
@@ -333,4 +346,335 @@ public class ServiceConfigDAOTest {
     serviceConfigs = serviceConfigDAO.getLatestServiceConfigs(clusterId, HDP_02);
     Assert.assertEquals(2, serviceConfigs.size());
   }
-}
+  
+  @Test
+  public void testConfiguration() throws Exception{
+    initClusterEntities();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+
+    Assert.assertTrue(!clusterEntity.getClusterConfigEntities().isEmpty());
+    Assert.assertTrue(!clusterEntity.getConfigMappingEntities().isEmpty());
+    
+    Assert.assertEquals(5, clusterEntity.getClusterConfigEntities().size());
+    Assert.assertEquals(3, clusterEntity.getConfigMappingEntities().size());
+  }
+  
+  @Test
+  public void testGetClusterConfigMappingByStack() throws Exception{
+    initClusterEntities();
+    
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    
+    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(),
HDP_01);
+    Assert.assertEquals(2, clusterConfigMappingEntities .size());
+
+    ClusterConfigMappingEntity e1 = clusterConfigMappingEntities.get(0);
+    String tag1 = e1.getTag();
+    Assert.assertEquals("version1", tag1);
+    String type1 = e1.getType();
+    Assert.assertEquals("oozie-site", type1);
+    
+    ClusterConfigMappingEntity e2 = clusterConfigMappingEntities.get(1);
+    String tag2 = e2.getTag();
+    Assert.assertEquals("version2", tag2);
+    String type2 = e2.getType();
+    Assert.assertEquals("oozie-site", type2);
+  }
+  
+  /**
+   * Test the get latest configuration query against clusterconfig table with configuration
groups inserted
+   * */
+  @Test
+  public void testGetClusterConfigMappingByStackCG() throws Exception{
+    initClusterEntitiesWithConfigGroups();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    
+    List<ConfigGroupEntity> configGroupEntities = configGroupDAO.findAllByTag("OOZIE");
+    
+    Assert.assertNotNull(configGroupEntities);
+    ConfigGroupEntity configGroupEntity = configGroupEntities.get(0);
+    Assert.assertNotNull(configGroupEntity);
+    Assert.assertEquals("c1", configGroupEntity.getClusterEntity().getClusterName());
+    Assert.assertEquals(Long.valueOf(1), configGroupEntity.getClusterEntity()
+      .getClusterId());
+    Assert.assertEquals("oozie_server", configGroupEntity.getGroupName());
+    Assert.assertEquals("OOZIE", configGroupEntity.getTag());
+    Assert.assertEquals("oozie server", configGroupEntity.getDescription());
+    
+    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(),
HDP_01);
+    Assert.assertEquals(2, clusterConfigMappingEntities .size());
+
+    ClusterConfigMappingEntity e1 = clusterConfigMappingEntities.get(0);
+    String tag1 = e1.getTag();
+    Assert.assertEquals("version1", tag1);
+    String type1 = e1.getType();
+    Assert.assertEquals("oozie-site", type1);
+    
+    ClusterConfigMappingEntity e2 = clusterConfigMappingEntities.get(1);
+    String tag2 = e2.getTag();
+    Assert.assertEquals("version2", tag2);
+    String type2 = e2.getType();
+    Assert.assertEquals("oozie-site", type2);
+  }
+  
+  /**
+   * Test  
+   *
+   * When the last configuration of a given configuration type to be stored into the clusterconfig
table is 
+   * for a configuration group, there is no corresponding entry generated in the clusterconfigmapping.
+   *
+   * Therefore, the getlatestconfiguration query should skip configuration groups stored
in the clusterconfig table.
+   *
+   * Test to determine the latest configuration of a given type whose version_tag 
+   * exists in the clusterconfigmapping table.
+   *
+   * */
+  @Test
+  public void testGetLatestClusterConfigMappingByStack() throws Exception{
+    ClusterImpl cluster =
+        createMockBuilder(ClusterImpl.class).
+          addMockedMethod("getSessionManager").
+          addMockedMethod("getClusterName").
+          addMockedMethod("getSessionAttributes").
+          createMock();
+    
+    initClusterEntities();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(),
HDP_01);
+    Collection<ClusterConfigMappingEntity> latestMapingEntities = cluster.getLatestConfigMapping(clusterConfigMappingEntities);
+    Assert.assertEquals(1, latestMapingEntities.size());
+    for(ClusterConfigMappingEntity e: latestMapingEntities){
+      Assert.assertEquals("version2", e.getTag());
+      Assert.assertEquals("oozie-site", e.getType());
+    }
+  }
+  
+  /**
+   * Test  
+   *
+   * When the last configuration of a given configuration type to be stored into the clusterconfig
table is 
+   * for a configuration group, there is no corresponding entry generated in the clusterconfigmapping.
+   *
+   * Therefore, the getlatestconfiguration query should skip configuration groups stored
in the clusterconfig table.
+   *
+   * Test to determine the latest configuration of a given type whose version_tag 
+   * exists in the clusterconfigmapping table.
+   *
+   * */
+  @Test
+  public void testGetLatestClusterConfigMappingByStackCG() throws Exception{
+    ClusterImpl cluster =
+        createMockBuilder(ClusterImpl.class).
+          addMockedMethod("getSessionManager").
+          addMockedMethod("getClusterName").
+          addMockedMethod("getSessionAttributes").
+          createMock();
+    
+    initClusterEntitiesWithConfigGroups();
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    List<ClusterConfigMappingEntity> clusterConfigMappingEntities = clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(),
HDP_01);
+    Collection<ClusterConfigMappingEntity> latestMapingEntities = cluster.getLatestConfigMapping(clusterConfigMappingEntities);
+    Assert.assertEquals(1, latestMapingEntities.size());
+    for(ClusterConfigMappingEntity e: latestMapingEntities){
+      Assert.assertEquals("version2", e.getTag());
+      Assert.assertEquals("oozie-site", e.getType());
+    }
+  }
+  
+  private void initClusterEntities() throws Exception{
+    String userName = "admin";
+    
+    ServiceConfigEntity oozieServiceConfigEntity = createServiceConfig("OOZIE", userName,
1L, 1L, System.currentTimeMillis(), null);
+    ClusterEntity  clusterEntity = oozieServiceConfigEntity.getClusterEntity();
+    
+    Long clusterId = clusterEntity.getClusterId();
+    
+    if(null == clusterId){
+      clusterId = 1L;
+      clusterEntity.setClusterId(clusterId);
+      clusterEntity = clusterDAO.merge(clusterEntity);
+    }
+    
+    StackEntity stackEntityHDP01 = stackDAO.find(HDP_01.getStackName(),HDP_01.getStackVersion());
+    StackEntity stackEntityHDP02 = stackDAO.find(HDP_02.getStackName(),HDP_02.getStackVersion());
+    
+    String oozieSite = "oozie-site";
+    
+    for (int i = 1; i < 6; i++){
+      ClusterConfigEntity entity = new ClusterConfigEntity();
+      entity.setClusterEntity(clusterEntity);
+      entity.setClusterId(clusterEntity.getClusterId());
+      entity.setType(oozieSite);
+      entity.setVersion(Long.valueOf(i));
+      entity.setTag("version"+i);
+      entity.setTimestamp(new Date().getTime());
+      if(i < 4)
+        entity.setStack(stackEntityHDP01);
+      else
+        entity.setStack(stackEntityHDP02);
+      entity.setData("");
+      clusterDAO.createConfig(entity);
+      clusterEntity.getClusterConfigEntities().add(entity);
+      clusterDAO.merge(clusterEntity);
+    }
+    
+    Collection<ClusterConfigMappingEntity> entities = clusterEntity.getConfigMappingEntities();
+    if(null == entities){
+      entities = new ArrayList<ClusterConfigMappingEntity>();
+      clusterEntity.setConfigMappingEntities(entities);
+    }  
+    
+    ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
+    e1.setClusterEntity(clusterEntity);
+    e1.setClusterId(clusterEntity.getClusterId());
+    e1.setCreateTimestamp(System.currentTimeMillis());
+    e1.setSelected(0);
+    e1.setUser(userName);
+    e1.setType(oozieSite);
+    e1.setTag("version1");
+    entities.add(e1);
+    clusterDAO.merge(clusterEntity);
+    
+    ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
+    e2.setClusterEntity(clusterEntity);
+    e2.setClusterId(clusterEntity.getClusterId());
+    e2.setCreateTimestamp(System.currentTimeMillis());
+    e2.setSelected(0);
+    e2.setUser(userName);
+    e2.setType(oozieSite);
+    e2.setTag("version2");
+    entities.add(e2);
+    clusterDAO.merge(clusterEntity);
+    
+    ClusterConfigMappingEntity e3 = new ClusterConfigMappingEntity();
+    e3.setClusterEntity(clusterEntity);
+    e3.setClusterId(clusterEntity.getClusterId());
+    e3.setCreateTimestamp(System.currentTimeMillis());
+    e3.setSelected(1);
+    e3.setUser(userName);
+    e3.setType(oozieSite);
+    e3.setTag("version4");
+    entities.add(e3);
+    clusterDAO.merge(clusterEntity);
+  } 
+  
+  private void initClusterEntitiesWithConfigGroups() throws Exception{
+    String userName = "admin";
+    
+    ServiceConfigEntity oozieServiceConfigEntity = createServiceConfig("OOZIE", userName,
1L, 1L, System.currentTimeMillis(), null);
+    ClusterEntity  clusterEntity = oozieServiceConfigEntity.getClusterEntity();
+    
+    Long clusterId = clusterEntity.getClusterId();
+    
+    if(null == clusterId){
+      clusterId = 1L;
+      clusterEntity.setClusterId(clusterId);
+      clusterEntity = clusterDAO.merge(clusterEntity);
+    }
+    
+    StackEntity stackEntityHDP01 = stackDAO.find(HDP_01.getStackName(),HDP_01.getStackVersion());
+    String oozieSite = "oozie-site";
+    
+    int count = 3;
+    for (int i = 1; i < count; i++){
+      ClusterConfigEntity entity = new ClusterConfigEntity();
+      entity.setClusterEntity(clusterEntity);
+      entity.setClusterId(clusterEntity.getClusterId());
+      entity.setType(oozieSite);
+      entity.setVersion(Long.valueOf(i));
+      entity.setTag("version"+i);
+      entity.setTimestamp(new Date().getTime());
+      entity.setStack(stackEntityHDP01);
+      entity.setData("");
+      clusterDAO.createConfig(entity);
+      clusterEntity.getClusterConfigEntities().add(entity);
+      clusterDAO.merge(clusterEntity);
+    }
+    
+    Collection<ClusterConfigMappingEntity> entities = clusterEntity.getConfigMappingEntities();
+    if(null == entities){
+      entities = new ArrayList<ClusterConfigMappingEntity>();
+      clusterEntity.setConfigMappingEntities(entities);
+    }  
+    
+    ClusterConfigMappingEntity e1 = new ClusterConfigMappingEntity();
+    e1.setClusterEntity(clusterEntity);
+    e1.setClusterId(clusterEntity.getClusterId());
+    e1.setCreateTimestamp(System.currentTimeMillis());
+    e1.setSelected(0);
+    e1.setUser(userName);
+    e1.setType(oozieSite);
+    e1.setTag("version1");
+    entities.add(e1);
+    clusterDAO.merge(clusterEntity);
+    
+    ClusterConfigMappingEntity e2 = new ClusterConfigMappingEntity();
+    e2.setClusterEntity(clusterEntity);
+    e2.setClusterId(clusterEntity.getClusterId());
+    e2.setCreateTimestamp(System.currentTimeMillis());
+    e2.setSelected(1);
+    e2.setUser(userName);
+    e2.setType(oozieSite);
+    e2.setTag("version2");
+    entities.add(e2);
+    clusterDAO.merge(clusterEntity);
+    
+    ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
+
+    ResourceTypeEntity resourceTypeEntity = resourceTypeDAO.findById(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE);
+    if (resourceTypeEntity == null) {
+      resourceTypeEntity = new ResourceTypeEntity();
+      resourceTypeEntity.setId(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE);
+      resourceTypeEntity.setName(ResourceTypeEntity.CLUSTER_RESOURCE_TYPE_NAME);
+      resourceTypeEntity = resourceTypeDAO.merge(resourceTypeEntity);
+    }
+
+    ResourceEntity resourceEntity = new ResourceEntity();
+    resourceEntity.setResourceType(resourceTypeEntity);
+
+    configGroupEntity.setClusterEntity(clusterEntity);
+    configGroupEntity.setClusterId(clusterEntity.getClusterId());
+    configGroupEntity.setGroupName("oozie_server");
+    configGroupEntity.setDescription("oozie server");
+    configGroupEntity.setTag("OOZIE");
+
+    ClusterConfigEntity configEntity = new ClusterConfigEntity();
+    configEntity.setType("oozie-site");
+    configEntity.setTag("version3");
+    configEntity.setData("someData");
+    configEntity.setAttributes("someAttributes");
+    configEntity.setStack(stackEntityHDP01);
+
+    List<ClusterConfigEntity> configEntities = new
+      ArrayList<ClusterConfigEntity>();
+    configEntities.add(configEntity);
+    
+    configGroupDAO.create(configGroupEntity);
+    
+    if (configEntities != null && !configEntities.isEmpty()) {
+      List<ConfigGroupConfigMappingEntity> configMappingEntities = new
+        ArrayList<ConfigGroupConfigMappingEntity>();
+
+      for (ClusterConfigEntity config : configEntities) {
+        config.setClusterEntity(clusterEntity);
+        config.setClusterId(clusterEntity.getClusterId());
+        clusterDAO.createConfig(config);
+
+        ConfigGroupConfigMappingEntity configMappingEntity = new
+          ConfigGroupConfigMappingEntity();
+        configMappingEntity.setClusterId(clusterEntity.getClusterId());
+        configMappingEntity.setClusterConfigEntity(config);
+        configMappingEntity.setConfigGroupEntity(configGroupEntity);
+        configMappingEntity.setConfigGroupId(configGroupEntity.getGroupId());
+        configMappingEntity.setVersionTag(config.getTag());
+        configMappingEntity.setConfigType(config.getType());
+        configMappingEntity.setTimestamp(System.currentTimeMillis());
+        configMappingEntities.add(configMappingEntity);
+        configGroupConfigMappingDAO.create(configMappingEntity);
+      }
+      
+      configGroupEntity.setConfigGroupConfigMappingEntities(configMappingEntities);
+      configGroupDAO.merge(configGroupEntity);
+    }
+  }
+}
\ No newline at end of file


Mime
View raw message