/** {@inheritDoc} */
  @Override
  public Requisition addInterfaceToNode(
      final String groupName, final String pathToNode, final String ipAddr) {
    m_writeLock.lock();
    try {
      final Requisition group = getProvisioningGroup(groupName);
      Assert.notNull(group, "Group should not be Null and is null groupName: " + groupName);
      final RequisitionNode node =
          PropertyUtils.getPathValue(group, pathToNode, RequisitionNode.class);
      Assert.notNull(node, "Node should not be Null and pathToNode: " + pathToNode);

      PrimaryType snmpPrimary = PrimaryType.PRIMARY;
      if (node.getInterfaceCount() > 0) {
        snmpPrimary = PrimaryType.SECONDARY;
      }

      // final int ifaceCount = node.getInterfaceCount();
      final RequisitionInterface iface = createInterface(ipAddr, snmpPrimary);
      node.putInterface(iface);
      // Assert.isTrue(node.getInterfaceCount() == (ifaceCount + 1), "Interface was not added
      // correctly");

      m_pendingForeignSourceRepository.save(group);
      m_pendingForeignSourceRepository.flush();
      return m_pendingForeignSourceRepository.getRequisition(groupName);
    } finally {
      m_writeLock.unlock();
    }
  }
    RequisitionAssetCollection getAssetParameters(final String foreignId) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : new RequisitionAssetCollection(node.getAssets());
    }
    RequisitionAsset getAssetParameter(final String foreignId, final String parameter) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : node.getAsset(parameter);
    }
    RequisitionCategoryCollection getCategories(final String foreignId) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : new RequisitionCategoryCollection(node.getCategories());
    }
    RequisitionCategory getCategory(final String foreignId, final String category) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : node.getCategory(category);
    }
    RequisitionInterface getInterfaceForNode(final String foreignId, final String ipAddress) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : node.getInterface(ipAddress);
    }
    RequisitionInterfaceCollection getInterfacesForNode(final String foreignId) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      return node == null ? null : new RequisitionInterfaceCollection(node.getInterfaces());
    }
 void updateInterface(
     final String foreignId, final String ipAddress, final MultivaluedMapImpl params) {
   final String foreignSource = m_foreignSource;
   LOG.debug(
       "updateInterface: Updating interface {} on node {}/{}",
       ipAddress,
       foreignSource,
       foreignId);
   if (params.isEmpty()) return;
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       final RequisitionInterface iface = node.getInterface(ipAddress);
       if (iface != null) {
         req.updateDateStamp();
         RestUtils.setBeanProperties(iface, params);
         save(req);
         LOG.debug(
             "updateInterface: Interface {} on node {}/{} updated",
             ipAddress,
             foreignSource,
             foreignId);
       }
     }
   }
 }
  /** {@inheritDoc} */
  @Override
  public Requisition addAssetFieldToNode(
      final String groupName,
      final String pathToNode,
      final String assetName,
      final String assetValue) {
    m_writeLock.lock();
    try {
      final Requisition group = getProvisioningGroup(groupName);
      final RequisitionNode node =
          PropertyUtils.getPathValue(group, pathToNode, RequisitionNode.class);

      // final int assetCount = node.getAssetCount();
      final RequisitionAsset asset = new RequisitionAsset();
      asset.setName(assetName);
      asset.setValue(assetValue);
      node.putAsset(asset);
      // Assert.isTrue(node.getCategoryCount() == (assetCount + 1), "Asset was not added
      // correctly");

      m_pendingForeignSourceRepository.save(group);
      m_pendingForeignSourceRepository.flush();
      return m_pendingForeignSourceRepository.getRequisition(groupName);
    } finally {
      m_writeLock.unlock();
    }
  }
 public RequisitionNode getRequisitionAssets() {
   RequisitionNode template = new RequisitionNode();
   template.putAsset(new RequisitionAsset("username", getUsername()));
   template.putAsset(new RequisitionAsset("password", getPassword()));
   template.putAsset(new RequisitionAsset("enable", getEnable()));
   template.putAsset(new RequisitionAsset("connection", getConnection()));
   template.putAsset(new RequisitionAsset("autoenable", getAutoenable()));
   return template;
 }
 protected RequisitionNode createNode(final String id) {
   RequisitionNode node = new RequisitionNode();
   node.setForeignId(id);
   node.setNodeLabel("node " + id);
   RequisitionInterface iface = new RequisitionInterface();
   iface.setIpAddr("172.16.0." + id);
   node.putInterface(iface);
   return node;
 }
    RequisitionMonitoredService getServiceForInterface(
        final String foreignId, final String ipAddress, final String service) {
      flush();

      final Requisition req = getActiveRequisition(false);
      final RequisitionNode node = req == null ? null : req.getNode(foreignId);
      final RequisitionInterface iface = node == null ? null : node.getInterface(ipAddress);

      return iface == null ? null : iface.getMonitoredService(service);
    }
 void addOrReplaceNodeCategory(final String foreignId, final RequisitionCategory category) {
   final Requisition req = getActiveRequisition(true);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.putCategory(category);
       save(req);
     }
   }
 }
 void addOrReplaceInterface(final String foreignId, final RequisitionInterface iface) {
   final Requisition req = getActiveRequisition(true);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.putInterface(iface);
       save(req);
     }
   }
 }
 void addOrReplaceNodeAssetParameter(final String foreignId, final RequisitionAsset asset) {
   final Requisition req = getActiveRequisition(true);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.putAsset(asset);
       save(req);
     }
   }
 }
 void addOrReplaceService(
     final String foreignId, final String ipAddress, final RequisitionMonitoredService service) {
   final Requisition req = getActiveRequisition(true);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       final RequisitionInterface iface = node.getInterface(ipAddress);
       if (iface != null) {
         req.updateDateStamp();
         iface.putMonitoredService(service);
         save(req);
       }
     }
   }
 }
 void deleteInterface(final String foreignId, final String ipAddress) {
   LOG.debug(
       "deleteInterface: Deleting interface {} from node {}/{}",
       ipAddress,
       getForeignSource(),
       foreignId);
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.deleteInterface(ipAddress);
       save(req);
     }
   }
 }
 void deleteAssetParameter(final String foreignId, final String parameter) {
   LOG.debug(
       "deleteAssetParameter: Deleting asset parameter {} from node {}/{}",
       parameter,
       getForeignSource(),
       foreignId);
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.deleteAsset(parameter);
       save(req);
     }
   }
 }
 void deleteCategory(final String foreignId, final String category) {
   LOG.debug(
       "deleteCategory: Deleting category {} from node {}/{}",
       category,
       getForeignSource(),
       foreignId);
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       req.updateDateStamp();
       node.deleteCategory(category);
       save(req);
     }
   }
 }
  /** {@inheritDoc} */
  @Override
  public Requisition addNewNodeToGroup(final String groupName, final String nodeLabel) {
    m_writeLock.lock();

    try {
      final Requisition group = getProvisioningGroup(groupName);

      final RequisitionNode node =
          createNode(nodeLabel, String.valueOf(System.currentTimeMillis()));
      node.setBuilding(groupName);
      group.insertNode(node);

      m_pendingForeignSourceRepository.save(group);
      m_pendingForeignSourceRepository.flush();
      return m_pendingForeignSourceRepository.getRequisition(groupName);
    } finally {
      m_writeLock.unlock();
    }
  }
 void deleteInterfaceService(
     final String foreignId, final String ipAddress, final String service) {
   LOG.debug(
       "deleteInterfaceService: Deleting service {} from interface {} on node {}/{}",
       service,
       ipAddress,
       getForeignSource(),
       foreignId);
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     final RequisitionNode node = req.getNode(foreignId);
     if (node != null) {
       final RequisitionInterface iface = node.getInterface(ipAddress);
       if (iface != null) {
         req.updateDateStamp();
         iface.deleteMonitoredService(service);
         save(req);
       }
     }
   }
 }
 private RequisitionNode createNode(final String nodeLabel, final String foreignId) {
   final RequisitionNode node = new RequisitionNode();
   node.setNodeLabel(nodeLabel);
   node.setForeignId(foreignId);
   return node;
 }
 /** trimWhitespace Removes leading and trailing whitespace from fields that should not have any */
 private void trimWhitespace(Requisition req) {
   for (RequisitionNode node : req.getNodes()) {
     if (node.getForeignId() != null) {
       node.setForeignId(node.getForeignId().trim());
     }
     if (node.getParentForeignSource() != null) {
       node.setParentForeignSource(node.getParentForeignSource().trim());
     }
     if (node.getParentForeignId() != null) {
       node.setParentForeignId(node.getParentForeignId().trim());
     }
     for (RequisitionInterface intf : node.getInterfaces()) {
       if (intf.getIpAddr() != null) {
         intf.setIpAddr(intf.getIpAddr().trim());
       }
     }
   }
 }