/** * 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; }
/** * Delete resource information from corresponding database table and update free resource counter * pool, if resource was auto-generated * * @param connection - DB Connection instance * @param freeCounterBean - Bean corresponding to os_free_counter_tbl * @param resourceBean - Bean corresponding to resource specific table * @return - true is operation executed successfully * @throws SQLException */ public boolean deleteResourceId( Connection connection, FreeCounterBean freeCounterBean, Object resourceBean) throws SQLException { LOG.trace("Start ResourceIdManager#deleteResourceId()"); List<Integer> ifIds = null; boolean result = false; int deletionStatus = -1; if (resourceBean instanceof VtnBean) { final VtnDao vtnDao = new VtnDao(); deletionStatus = vtnDao.delete(connection, (VtnBean) resourceBean); } else if (resourceBean instanceof VBridgeBean) { final VBridgeInterfaceDao vrtInterfaceDao = new VBridgeInterfaceDao(); VBridgeInterfaceBean vInterfaceBean = new VBridgeInterfaceBean(); vInterfaceBean.setVbrName(((VBridgeBean) resourceBean).getVbrName()); ifIds = vrtInterfaceDao.getVbrIfIds(connection, vInterfaceBean); final VBridgeDao vBridgeDao = new VBridgeDao(); deletionStatus = vBridgeDao.delete(connection, (VBridgeBean) resourceBean); } else if (resourceBean instanceof VRouterBean) { final VRouterInterfaceDao vrtInterfaceDao = new VRouterInterfaceDao(); VRouterInterfaceBean vInterfaceBean = new VRouterInterfaceBean(); vInterfaceBean.setVtnName(freeCounterBean.getVtnName()); vInterfaceBean.setVrtName(((VRouterBean) resourceBean).getVrtName()); ifIds = vrtInterfaceDao.getVrtIfIds(connection, vInterfaceBean); final VRouterDao vRouterDao = new VRouterDao(); deletionStatus = vRouterDao.delete(connection, (VRouterBean) resourceBean); } else if (resourceBean instanceof VRouterInterfaceBean) { final VRouterInterfaceDao vInterfaceDao = new VRouterInterfaceDao(); deletionStatus = vInterfaceDao.delete(connection, (VRouterInterfaceBean) resourceBean); } else if (resourceBean instanceof VBridgeInterfaceBean) { final VBridgeInterfaceDao vInterfaceDao = new VBridgeInterfaceDao(); deletionStatus = vInterfaceDao.delete(connection, (VBridgeInterfaceBean) resourceBean); } else if (resourceBean instanceof StaticRouteBean) { final StaticRouteDao staticRouteDao = new StaticRouteDao(); deletionStatus = staticRouteDao.delete(connection, (StaticRouteBean) resourceBean); } if (deletionStatus == 1) { result = manageCounter(connection, freeCounterBean, resourceBean, ifIds); } LOG.debug("Status of operation : " + result); LOG.trace("Complete ResourceIdManager#deleteResourceId()"); return result; }