Ejemplo n.º 1
0
  /*
   * Update a specific LB
   * only allows changing name or algorithm
   */
  @PUT
  @Path("/{id}")
  @Consumes("application/json")
  public void updateLb(@PathParam("id") String id, String content) {
    logger.info("PUT loadbalancer : " + id);
    LoadBalancerDataModel model = new LoadBalancerDataModel();

    // attempt to read lb top be updated
    Integer lbId = new Integer(id);
    LoadBalancer lb = model.getLoadBalancer(lbId);
    if (lb == null) {
      throw new LBaaSException("could not find id : " + id, 404); //  not found	
    }

    String name, algorithm;
    JSONObject jsonObject = null;

    // decode JSON
    try {
      jsonObject = new JSONObject(content);
    } catch (JSONException e) {
      throw new LBaaSException("bad json request", 400); //  bad request
    }

    // look for name
    try {
      name = (String) jsonObject.get("name");
      lb.setName(name);
      logger.info("   name = " + name);
    } catch (JSONException e) {
      name = null;
    }

    // look for algorithm
    try {
      algorithm = (String) jsonObject.get("algorithm");
      lb.setName(name);
      logger.info("   algorithm = " + algorithm);
    } catch (JSONException e) {
      algorithm = null;
    }

    // must have one of these fields
    if ((name == null) && (algorithm == null)) {
      throw new LBaaSException("name and algorithm missing", 400); //  bad request		
    }

    if (name != null) lb.setName(name);

    if (algorithm != null) lb.setAlgorithm(algorithm);

    // mark as change pending
    lb.setStatus(LoadBalancer.STATUS_PENDING_UPDATE);

    // write changes to DB
    model.setLoadBalancer(lb);

    // have the device process the job
    try {
      lbaasTaskManager.sendJob(lb.getDevice(), LbToJson(lb, ACTION_UPDATE));
    } catch (JSONException jsone) {
      throw new LBaaSException(
          "internal server error JSON exception :" + jsone.toString(), 500); //  internal error
    }
  }