Exemple #1
0
  /**
   * Generated resource id for specified resource First check the available counter, otherwise
   * increment counter and return
   *
   * @param connection - DB Connection instance
   * @param freeCounterBean - Bean corresponding to os_free_counter_tbl
   * @return - generated resource-id, -1 is error occurred
   * @throws SQLException
   */
  public int getResourceId(Connection connection, FreeCounterBean freeCounterBean)
      throws SQLException {
    LOG.trace("Start ResourceIdManager#getResourceId()");
    int resourceCounter = -1;
    /*
     * Check if any resource counter is available that can be used
     */
    final FreeCounterDao freeCounterDao = new FreeCounterDao();
    resourceCounter = freeCounterDao.getCounter(connection, freeCounterBean);

    freeCounterBean.setResourceCounter(resourceCounter);

    /*
     * resource counter is available in free resource counter pool then use
     * the same after deleting that from free resource counter pool. On the
     * other hand increment counter and return.
     */
    if (resourceCounter != -1) {
      if (freeCounterDao.deleteCounter(connection, freeCounterBean) != 1) {
        LOG.error("Error in deletion of resource counter from os_free_counter_tbl.");
        resourceCounter = -1;
      }
    } else {
      LOG.info("Resource counter is not available in os_free_counter_tbl.");

      LOG.debug(
          "Resource counter required to be generated for  : "
              + freeCounterBean.getResourceId()
              + " and vtn_name : "
              + freeCounterBean.getVtnName());

      if (freeCounterBean
          .getResourceId()
          .equalsIgnoreCase(VtnServiceOpenStackConsts.TENANT_RES_ID)) {
        LOG.debug("Resource generation for VTN.");
        final VtnDao vtnDao = new VtnDao();
        resourceCounter = vtnDao.getNextId(connection);
      } else if (freeCounterBean
          .getResourceId()
          .equalsIgnoreCase(VtnServiceOpenStackConsts.NETWORK_RES_ID)) {
        LOG.debug("Resource generation for vBridge.");
        final VBridgeDao vBridgeDao = new VBridgeDao();
        resourceCounter = vBridgeDao.getNextId(connection, freeCounterBean.getVtnName());
      } else if (freeCounterBean
          .getResourceId()
          .equalsIgnoreCase(VtnServiceOpenStackConsts.PORT_RES_ID)) {
        LOG.debug("Resource generation for port/router interface.");
        final VBridgeInterfaceDao vbrInterfaceDao = new VBridgeInterfaceDao();
        int portResourceCounter =
            vbrInterfaceDao.getNextId(connection, freeCounterBean.getVtnName());
        final VRouterInterfaceDao vrtInterfaceDao = new VRouterInterfaceDao();
        int interfaceResourceCounter =
            vrtInterfaceDao.getNextId(connection, freeCounterBean.getVtnName());
        resourceCounter = portResourceCounter + interfaceResourceCounter - 1;
      }
    }
    LOG.info("Resource counter that will be used : " + resourceCounter);
    LOG.trace("Complete ResourceIdManager#getResourceId()");
    return resourceCounter;
  }
Exemple #2
0
  /**
   * Manages the counters for all kind of resources available for OpenStack operations
   *
   * @param connection
   * @param freeCounterBean
   * @param resourceBean
   * @param ifIds2
   * @return
   * @throws SQLException
   */
  private boolean manageCounter(
      Connection connection,
      FreeCounterBean freeCounterBean,
      Object resourceBean,
      List<Integer> ifIds)
      throws SQLException {
    boolean result = false;
    final FreeCounterDao freeCounterDao = new FreeCounterDao();

    /*
     * delete child counters in case of parent entity is deleted
     */
    if (freeCounterBean.getResourceId().equalsIgnoreCase(VtnServiceOpenStackConsts.TENANT_RES_ID)) {
      FreeCounterBean localBean = new FreeCounterBean();
      localBean.setVtnName(((VtnBean) resourceBean).getVtnName());
      freeCounterDao.deleteVtnChilds(connection, localBean);
    } else if (freeCounterBean
            .getResourceId()
            .equalsIgnoreCase(VtnServiceOpenStackConsts.NETWORK_RES_ID)
        || freeCounterBean
            .getResourceId()
            .equalsIgnoreCase(VtnServiceOpenStackConsts.ROUTER_RES_ID)) {
      if (ifIds.size() > 0) {
        // insert unused counter into os_free_resource_counter table
        FreeCounterBean localBean = new FreeCounterBean();
        localBean.setResourceId(VtnServiceOpenStackConsts.PORT);
        localBean.setVtnName(freeCounterBean.getVtnName());
        for (int i = 0; i < ifIds.size(); i++) {
          localBean.setResourceCounter(ifIds.get(i));
          freeCounterDao.insertCounter(connection, localBean);
        }
      }
    }

    /*
     * if resource counter is 0, then no need to insert resource counter in
     * pool
     */
    if (freeCounterBean.getResourceCounter() == 0) {
      LOG.debug(
          "Resource counter is not auto-generated, insertion not required in os_res_counter table.");
      result = true;
    } else {
      LOG.debug("Resource is auto-generated, insertion required in os_res_counter table.");
      result = freeCounterDao.insertCounter(connection, freeCounterBean) == 1;
    }
    return result;
  }