ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vbrodets...@apache.org
Subject ambari git commit: AMBARI-16728. Ambari APIs should allow using case insensitive host names(for SmartSense).(vbrodetskyi)
Date Fri, 20 May 2016 19:36:44 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 2425189d7 -> ae6d01067


AMBARI-16728. Ambari APIs should allow using case insensitive host names(for SmartSense).(vbrodetskyi)


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

Branch: refs/heads/trunk
Commit: ae6d0106751467a1a38fb6e60213fb0f09ba692c
Parents: 2425189
Author: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Authored: Fri May 20 20:20:42 2016 +0300
Committer: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Committed: Fri May 20 20:20:42 2016 +0300

----------------------------------------------------------------------
 .../server/api/predicate/QueryParser.java       | 39 ++++++++++++--
 .../resources/ResourceInstanceFactoryImpl.java  | 21 ++++++++
 .../server/api/predicate/QueryParserTest.java   | 56 ++++++++++++++++++--
 .../ResourceInstanceFactoryImplTest.java        | 20 +++++--
 4 files changed, 126 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ae6d0106/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/QueryParser.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/QueryParser.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/QueryParser.java
index 3af90ba..7386813 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/QueryParser.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/QueryParser.java
@@ -18,13 +18,23 @@
 
 package org.apache.ambari.server.api.predicate;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
 import org.apache.ambari.server.api.predicate.expressions.Expression;
 import org.apache.ambari.server.api.predicate.expressions.LogicalExpressionFactory;
 import org.apache.ambari.server.api.predicate.expressions.RelationalExpression;
-import org.apache.ambari.server.api.predicate.operators.*;
+import org.apache.ambari.server.api.predicate.operators.LogicalOperator;
+import org.apache.ambari.server.api.predicate.operators.LogicalOperatorFactory;
+import org.apache.ambari.server.api.predicate.operators.Operator;
+import org.apache.ambari.server.api.predicate.operators.RelationalOperator;
+import org.apache.ambari.server.api.predicate.operators.RelationalOperatorFactory;
 import org.apache.ambari.server.controller.spi.Predicate;
-
-import java.util.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Parser which produces a predicate instance from an array of tokens,
@@ -33,6 +43,12 @@ import java.util.*;
 public class QueryParser {
 
   /**
+   * The logger.
+   */
+  private final static Logger LOG =
+          LoggerFactory.getLogger(QueryParser.class);
+
+  /**
    * Map of token type to token handlers.
    */
   private static final Map<Token.TYPE, TokenHandler> TOKEN_HANDLERS =
@@ -77,12 +93,29 @@ public class QueryParser {
     ParseContext ctx = parseExpressions(tokens);
 
     List<Expression> listExpressions       = ctx.getExpressions();
+    changeHostNameToLowerCase(listExpressions);
     List<Expression> listMergedExpressions = mergeExpressions(listExpressions, ctx.getMaxPrecedence());
 
     return listMergedExpressions.isEmpty() ? null :
         listMergedExpressions.get(0).toPredicate();
   }
 
+  private void changeHostNameToLowerCase(List<Expression> listExpressions) {
+    try {
+      for (Expression expression : listExpressions) {
+        Object keyObject = expression.getLeftOperand();
+        if (keyObject != null) {
+          String key = keyObject.toString();
+          if (key.endsWith("/host_name")) {
+            expression.setRightOperand(expression.getRightOperand().toString().toLowerCase());
+          }
+        }
+      }
+    } catch(Exception e) {
+      LOG.error("Lowercase host_name value in expression failed with error:" + e);
+    }
+  }
+
   /**
    * Create parse context from an array of tokens. The parse context contains a list of expressions
    * and other information about the expressions an parsed tokens.

http://git-wip-us.apache.org/repos/asf/ambari/blob/ae6d0106/ambari-server/src/main/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImpl.java
index 9c864b6..cf7c391 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImpl.java
@@ -28,6 +28,8 @@ import org.apache.ambari.server.api.query.QueryImpl;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.utilities.ClusterControllerHelper;
 import org.apache.ambari.server.view.ViewRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Factory for creating resource instances.
@@ -36,6 +38,12 @@ public class ResourceInstanceFactoryImpl implements ResourceInstanceFactory
{
 
 
   /**
+   * The logger.
+   */
+  private final static Logger LOG =
+          LoggerFactory.getLogger(ResourceInstanceFactoryImpl.class);
+
+  /**
    * Map of external resource definitions (added through views).
    */
   private final static Map<Resource.Type, ResourceDefinition> resourceDefinitions =
@@ -48,6 +56,19 @@ public class ResourceInstanceFactoryImpl implements ResourceInstanceFactory
{
     /**
      * The resource definition for the specified type.
      */
+
+    // this code changes hot name to lower case
+    try {
+      if (mapIds.containsKey(Resource.Type.Host)) {
+        String hostName = mapIds.get(Resource.Type.Host);
+        if (hostName != null) {
+          mapIds.put(Resource.Type.Host, hostName.toLowerCase());
+        }
+      }
+    } catch(Exception e) {
+      LOG.error("Lowercase host name value in resource failed with error:" + e);
+    }
+
     ResourceDefinition resourceDefinition = getResourceDefinition(type, mapIds);
 
     return new QueryImpl(mapIds, resourceDefinition, ClusterControllerHelper.getClusterController());

http://git-wip-us.apache.org/repos/asf/ambari/blob/ae6d0106/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
index 90ad522..cf332e7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
@@ -19,14 +19,24 @@
 
 package org.apache.ambari.server.api.predicate;
 
-import org.apache.ambari.server.controller.predicate.*;
-import org.apache.ambari.server.controller.spi.Predicate;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import static org.junit.Assert.*;
+import org.apache.ambari.server.controller.predicate.AndPredicate;
+import org.apache.ambari.server.controller.predicate.CategoryIsEmptyPredicate;
+import org.apache.ambari.server.controller.predicate.EqualsPredicate;
+import org.apache.ambari.server.controller.predicate.FilterPredicate;
+import org.apache.ambari.server.controller.predicate.GreaterEqualsPredicate;
+import org.apache.ambari.server.controller.predicate.LessEqualsPredicate;
+import org.apache.ambari.server.controller.predicate.LessPredicate;
+import org.apache.ambari.server.controller.predicate.NotPredicate;
+import org.apache.ambari.server.controller.predicate.OrPredicate;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.junit.Test;
 
 /**
  * QueryParser unit tests.
@@ -174,6 +184,44 @@ public class QueryParserTest {
   }
 
   @Test
+  public void testParse_InOp__HostName() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+    // foo.in(one,two,3)
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR_FUNC, ".in("));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "HostRoles/host_name"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "Host1,HOST2,HoSt3"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+
+    EqualsPredicate ep1 = new EqualsPredicate("HostRoles/host_name", "host1");
+    EqualsPredicate ep2 = new EqualsPredicate("HostRoles/host_name", "host2");
+    EqualsPredicate ep3 = new EqualsPredicate("HostRoles/host_name", "host3");
+
+    OrPredicate orPredicate = new OrPredicate(ep1, ep2, ep3);
+
+    assertEquals(orPredicate, p);
+  }
+
+  @Test
+  public void testParse_EquOp_HostName() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+    //a=1&!b=2
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "HostRoles/host_name"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "HOST1"));
+
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+    EqualsPredicate equalsPred = new EqualsPredicate<String>("HostRoles/host_name",
"host1");
+
+
+    assertEquals(equalsPred, p);
+  }
+
+  @Test
   public void testParse_InOp__exception() throws Exception {
     List<Token> listTokens = new ArrayList<Token>();
     // foo.in()

http://git-wip-us.apache.org/repos/asf/ambari/blob/ae6d0106/ambari-server/src/test/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImplTest.java
index dfe8be7..fa0b4c5 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/ResourceInstanceFactoryImplTest.java
@@ -18,12 +18,15 @@
 
 package org.apache.ambari.server.api.resources;
 
-import org.apache.ambari.server.controller.spi.Resource;
-import org.junit.Test;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
 /**
  * ResourceInstanceFactoryImpl unit tests.
  */
@@ -50,6 +53,17 @@ public class ResourceInstanceFactoryImplTest {
   }
 
   @Test
+  public void testGetHostDefinition() {
+    ResourceInstanceFactoryImpl resourceInstanceFactory = new ResourceInstanceFactoryImpl();
+    Map<Resource.Type, String> mapIds = new HashMap<>();
+    mapIds.put(Resource.Type.Host, "TeSTHost1");
+    ResourceInstance resourceInstance = resourceInstanceFactory.createResource(
+            Resource.Type.Host, mapIds);
+
+    assertEquals(mapIds.get(Resource.Type.Host), "testhost1");
+  }
+
+  @Test
   public void testGetHostKerberosIdentityDefinition() {
     ResourceDefinition resourceDefinition = ResourceInstanceFactoryImpl.getResourceDefinition(
         Resource.Type.HostKerberosIdentity, null);


Mime
View raw message