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);
       }
     }
   }
 }
    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);
    }
    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);
    }
    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);
    }
    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());
    }
    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());
    }
  protected URL createSnapshot(final String foreignSource) throws MalformedURLException {
    System.err.println("--- creating snapshot for " + foreignSource + " ---");
    Requisition pending = m_pending.getRequisition(foreignSource);
    Requisition deployed = m_active.getRequisition(foreignSource);

    final Date deployedDate = deployed == null ? null : deployed.getDate();
    final Date pendingDate = pending == null ? null : pending.getDate();

    if (deployedDate == null)
      return RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pending.getDate())
          .toURI()
          .toURL();
    if (pendingDate == null) return m_active.getRequisitionURL(foreignSource);

    final URL url;
    if (deployedDate.before(pendingDate)) {
      url =
          RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pendingDate)
              .toURI()
              .toURL();
    } else {
      url = m_active.getRequisitionURL(foreignSource);
    }

    System.err.println("deployedDate = " + deployedDate);
    System.err.println("pendingDate  = " + pendingDate);
    System.err.println("url          = " + url);

    return url;
  }
 void addOrReplaceNode(final RequisitionNode node) {
   final Requisition req = getActiveRequisition(true);
   if (req != null) {
     req.updateDateStamp();
     req.putNode(node);
     save(req);
   }
 }
 @Test
 public void testRequisition() throws Exception {
   createRequisition();
   Requisition r = m_foreignSourceRepository.getRequisition(m_defaultForeignSourceName);
   TestVisitor v = new TestVisitor();
   r.visit(v);
   assertEquals("number of nodes visited", 2, v.getNodeReqs().size());
   assertEquals("node name matches", "apknd", v.getNodeReqs().get(0).getNodeLabel());
 }
 void deleteNode(final String foreignId) {
   LOG.debug(
       "deleteNode: Deleting node {} from foreign source {}", foreignId, getForeignSource());
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     req.updateDateStamp();
     req.deleteNode(foreignId);
     save(req);
   }
 }
    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 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 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 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);
     }
   }
 }
  /** {@inheritDoc} */
  @Override
  public synchronized Requisition importResourceRequisition(final Resource resource)
      throws ForeignSourceRepositoryException {
    final Requisition requisition =
        m_deployedForeignSourceRepository.importResourceRequisition(resource);
    final String foreignSource = requisition.getForeignSource();

    cleanUpDeployedForeignSources(foreignSource);
    cleanUpSnapshots(requisition);

    return requisition;
  }
 void updateRequisition(final MultivaluedMapImpl params) {
   final String foreignSource = m_foreignSource;
   LOG.debug("updateRequisition: Updating requisition with foreign source {}", foreignSource);
   if (params.isEmpty()) return;
   final Requisition req = getActiveRequisition(false);
   if (req != null) {
     req.updateDateStamp();
     RestUtils.setBeanProperties(req, params);
     save(req);
     LOG.debug("updateRequisition: Requisition with foreign source {} updated", foreignSource);
   }
 }
  /** {@inheritDoc} */
  @Override
  public Requisition createProvisioningGroup(final String name) {
    m_writeLock.lock();
    try {
      final Requisition group = new Requisition();
      group.setForeignSource(name);

      m_pendingForeignSourceRepository.save(group);
      m_pendingForeignSourceRepository.flush();
      return m_pendingForeignSourceRepository.getRequisition(name);
    } finally {
      m_writeLock.unlock();
    }
  }
 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);
       }
     }
   }
 }
  @Test
  public void integrationTest() {
    /*
     * First, the user creates a requisition in the UI, or RESTful
     * interface.
     */
    Requisition pendingReq = new Requisition("test");
    pendingReq.putNode(createNode("1"));
    m_pending.save(pendingReq);
    m_pending.flush();

    /*
     * Then, the user makes a foreign source configuration to go along
     * with that requisition.
     */
    ForeignSource pendingSource = m_repository.getForeignSource("test");
    assertTrue(pendingSource.isDefault());
    pendingSource.setDetectors(new ArrayList<PluginConfig>());
    m_pending.save(pendingSource);
    m_pending.flush();

    /*
     * Now we got an import event, so we import that requisition file,
     * and save it.  The ForeignSource in the pending repository should
     * match the one in the active one, now.
     */
    Requisition activeReq =
        m_repository.importResourceRequisition(
            new UrlResource(m_pending.getRequisitionURL("test")));
    ForeignSource activeSource = m_active.getForeignSource("test");
    // and the foreign source should be the same as the one we made earlier, only this time it's
    // active

    assertEquals(activeSource.getName(), pendingSource.getName());
    assertEquals(activeSource.getDetectorNames(), pendingSource.getDetectorNames());
    assertEquals(activeSource.getScanInterval(), pendingSource.getScanInterval());
    assertRequisitionsMatch("active and pending requisitions should match", activeReq, pendingReq);

    /*
     * Since it's been officially deployed, the requisition and foreign
     * source should no longer be in the pending repo.
     */
    assertNull(
        "the requisition should be null in the pending repo", m_pending.getRequisition("test"));
    assertTrue(
        "the foreign source should be default since there's no specific in the pending repo",
        m_pending.getForeignSource("test").isDefault());
  }
 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 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 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);
     }
   }
 }
  private void cleanUpSnapshots(final Requisition requisition) {
    final String foreignSource = requisition.getForeignSource();
    final Date pendingDate = m_pendingForeignSourceRepository.getRequisitionDate(foreignSource);

    final List<File> pendingSnapshots =
        RequisitionFileUtils.findSnapshots(m_pendingForeignSourceRepository, foreignSource);

    if (pendingDate != null) {
      /* determine whether to delete the pending requisition */
      boolean deletePendingRequisition = true;
      if (pendingSnapshots.size() > 0) {
        for (final File pendingSnapshotFile : pendingSnapshots) {
          if (isNewer(pendingSnapshotFile, pendingDate)) {
            // the pending file is newer than an in-process snapshot, don't delete it
            deletePendingRequisition = false;
            break;
          }
        }
      }
      if (deletePendingRequisition) {
        m_pendingForeignSourceRepository.delete(requisition);
      }
    }

    /* determine whether this requisition was imported from a snapshot, and if so, delete its snapshot file */
    RequisitionFileUtils.deleteResourceIfSnapshot(requisition);

    final Date deployedDate = m_deployedForeignSourceRepository.getRequisitionDate(foreignSource);
    if (deployedDate != null) {
      RequisitionFileUtils.deleteSnapshotsOlderThan(
          getPendingForeignSourceRepository(), foreignSource, deployedDate);
    }
  }
 protected String getSummaryForRequisition(final File file) {
   final Requisition requisition =
       JaxbUtils.unmarshal(Requisition.class, new FileSystemResource(file));
   final StringBuilder sb = new StringBuilder();
   if (requisition.getNodeCount() > 0) {
     sb.append("(");
     final Iterator<RequisitionNode> nodeIterator = requisition.getNodes().iterator();
     while (nodeIterator.hasNext()) {
       sb.append(nodeIterator.next().getNodeLabel());
       if (nodeIterator.hasNext()) sb.append(", ");
     }
     sb.append(")");
   }
   final String requisitionSummary =
       file.getPath() + sb.toString() + ": " + requisition.getDate().getTime();
   return requisitionSummary;
 }
  @Test
  public void multipleSnapshotTest() throws URISyntaxException, InterruptedException {
    Requisition pendingReq = new Requisition("test");
    pendingReq.putNode(createNode("1"));
    m_pending.save(pendingReq);
    m_pending.flush();
    final String foreignSource = pendingReq.getForeignSource();
    pendingReq = m_pending.getRequisition(foreignSource);
    final File pendingSnapshotA =
        RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pendingReq.getDate());

    // Now, start a new pending update after the original snapshot is "in progress"
    pendingReq.updateDateStamp();
    m_pending.save(pendingReq);
    m_pending.flush();

    final File pendingSnapshotB =
        RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pendingReq.getDate());

    // "import" the A snapshot
    m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshotA));

    assertFalse(pendingSnapshotA.exists());
    assertTrue(pendingSnapshotB.exists());

    // since there's still a newer snapshot in-progress, it is safe to delete the pending test.xml
    URL pendingUrl = m_pending.getRequisitionURL(foreignSource);
    assertNotNull(pendingUrl);
    assertFalse(new File(pendingUrl.toURI()).exists());

    // then, "import" the B snapshot
    final Requisition bReq =
        m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshotB));

    assertFalse(pendingSnapshotA.exists());
    assertFalse(pendingSnapshotB.exists());

    // now the pending test.xml should be gone
    pendingUrl = m_pending.getRequisitionURL(foreignSource);
    assertNotNull(pendingUrl);
    assertFalse(new File(pendingUrl.toURI()).exists());

    // the last (B) pending import should match the deployed
    final Requisition deployedRequisition = m_active.getRequisition(foreignSource);
    assertEquals(deployedRequisition.getDate().getTime(), bReq.getDate().getTime());
  }
 /**
  * getProvisioningGroupNames
  *
  * @return a {@link java.util.Collection} object.
  */
 @Override
 public Collection<String> getProvisioningGroupNames() {
   m_readLock.lock();
   try {
     m_deployedForeignSourceRepository.flush();
     final Set<String> names = new TreeSet<String>();
     for (final Requisition r : m_deployedForeignSourceRepository.getRequisitions()) {
       names.add(r.getForeignSource());
     }
     m_pendingForeignSourceRepository.flush();
     for (final Requisition r : m_pendingForeignSourceRepository.getRequisitions()) {
       names.add(r.getForeignSource());
     }
     return names;
   } finally {
     m_readLock.unlock();
   }
 }
  /** {@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();
    }
  }
    URL createSnapshot() throws MalformedURLException {
      flush();

      final Requisition pending =
          getPendingForeignSourceRepository().getRequisition(getForeignSource());
      final Requisition deployed =
          getDeployedForeignSourceRepository().getRequisition(getForeignSource());

      final URL activeUrl =
          pending == null
                  || (deployed != null
                      && deployed.getDateStamp().compare(pending.getDateStamp()) > -1)
              ? getDeployedForeignSourceRepository().getRequisitionURL(getForeignSource())
              : RequisitionFileUtils.createSnapshot(
                      getPendingForeignSourceRepository(), getForeignSource(), pending.getDate())
                  .toURI()
                  .toURL();

      return activeUrl;
    }
 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);
       }
     }
   }
 }