public Object apply()

in metron-analytics/metron-maas-common/src/main/java/org/apache/metron/maas/functions/MaaSFunctions.java [106:191]


    public Object apply(List<Object> args, Context context) throws ParseException {
      if(args.size() < 2) {
        throw new ParseException("Unable to execute model_apply. " +
                                 "Expected arguments: endpoint_map:map, " +
                                 " [endpoint method:string], model_args:map"
                                 );
      }
      if(!isInitialized) {
        return null;
      }
      int i = 0;
      if(args.size() == 0) {
        return null;
      }
      Object endpointObj = args.get(i++);
      Map endpoint = null;
      String modelName;
      String modelVersion;
      String modelUrl;
      if(endpointObj instanceof Map) {
        endpoint = (Map)endpointObj;
        modelName = endpoint.get("name") + "";
        modelVersion = endpoint.get("version") + "";
        modelUrl = endpoint.get("url") + "";
      }
      else {
        return null;
      }
      String modelFunction = "apply";
      Map<String, String> modelArgs = new HashMap<>();
      if(args.get(i) instanceof String) {
        String func = (String)args.get(i);
        if(endpoint.containsKey("endpoint:" + func)) {
          modelFunction = "" + endpoint.get("endpoint:" + func);
        }
        else {
          modelFunction = func;
        }
        i++;
      }

      if(args.get(i) instanceof Map) {
        if(endpoint.containsKey("endpoint:apply")) {
          modelFunction = "" + endpoint.get("endpoint:apply");
        }
        modelArgs = (Map)args.get(i);
      }
      if( modelName == null
       || modelVersion == null
       || modelFunction == null
        ) {
        return null;
      }
      ModelCacheKey cacheKey = new ModelCacheKey(modelName, modelVersion, modelFunction, modelArgs);
      Map<String, Object> ret = resultCache.getIfPresent(cacheKey);
      if(ret != null) {
        return ret;
      }
      else {
        String url = modelUrl;
        if (url.endsWith("/")) {
          url = url.substring(0, url.length() - 1);
        }
        if (modelFunction.startsWith("/")) {
          modelFunction = modelFunction.substring(1);
        }
        try {
          URL u = new URL(url + "/" + modelFunction);

          String results = RESTUtil.INSTANCE.getRESTJSONResults(u, modelArgs);
          ret = JSONUtils.INSTANCE.load(results, JSONUtils.MAP_SUPPLIER);
          resultCache.put(cacheKey, ret);
          return ret;
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
          if (discoverer != null) {
            try {
              URL u = new URL(modelUrl);
              discoverer.blacklist(u);
            } catch (MalformedURLException e1) {
            }
          }
        }
      }
      return null;
    }