Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

private boolean nextHopSwitchExistsByName(String switchName) {
    DBCollection table = database.getCollection(DatabaseNames.getSwitchEntryTableName());

    // Run a query to look for the SwitchEntry name
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", switchName);
    DBObject dbObj = table.findOne(searchQuery);

    return (dbObj != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

private boolean captureDeviceExistsByName(String name) {
    DBCollection table = database.getCollection(DatabaseNames.getCaptureDevTableName());

    // Run a query to look for the SwitchEntry name
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", name);
    DBObject dbObj = table.findOne(searchQuery);

    return (dbObj != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void modifyCaptureDev(final String captureDevId, CaptureDev captureDev) throws NotFoundException {

    DBCollection table = database.getCollection(DatabaseNames.getCaptureDevTableName());

    // Create a query to look for the existing CaptureDev by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(captureDevId));

    // Check whether the CaptureDev exists
    DBObject dbObj = table.findOne(searchQuery);
    if (dbObj == null)
        throw new NotFoundException();

    // Create a document containing the new switch Entry
    BasicDBObject newDocument = captureDev.getAsDocument();

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    // Update the document in the database
    table.update(searchQuery, updateObj);

    logger.info("Updated CaptureDev id " + captureDevId);
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void deleteCaptureDev(String objectId) throws NotFoundException, ObjectInUseException {
    DBCollection table = database.getCollection(DatabaseNames.getCaptureDevTableName());

    // Create a query to look for the existing CaptureDev by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(objectId));
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    if ((long) dbObj.get("refCount") != 0) {
        throw new ObjectInUseException();
    }//w w w  . ja  v a  2 s .  c  o  m

    // Get the existing captureDev
    String captureDevName = (String) dbObj.get("name");

    // Remove the captureDev from the database
    table.remove(searchQuery);

    logger.info("Removed Capture Device  " + captureDevName);

    // Notify the application that the capture device changed
    // TODO - add objectID
    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.CAPTURE_DEVICE);
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

private boolean portChainExistsByName(String name) {
    DBCollection table = database.getCollection(DatabaseNames.getPortChainTableName());

    // Run a query to look for the PortChain name
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", name);
    DBObject dbObj = table.findOne(searchQuery);

    return (dbObj != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void modifyPortChain(final String portChainId, PortChain portChain) throws NotFoundException {

    DBCollection table = database.getCollection(DatabaseNames.getPortChainTableName());

    // Create a query to look for the existing PortChain by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(portChainId));

    // Check whether the PortChain exists
    DBObject dbObj = table.findOne(searchQuery);
    if (dbObj == null)
        throw new NotFoundException();

    // Create a document containing the new portChain
    BasicDBObject newDocument = portChain.getAsDocument();

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    // Update the document in the database
    table.update(searchQuery, updateObj);

    logger.info("Updated PortChain ID " + portChainId);
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void deletePortChain(String objectId) throws NotFoundException, ObjectInUseException {
    DBCollection table = database.getCollection(DatabaseNames.getPortChainTableName());

    // Create a query to look for the existing TapPolicy by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(objectId));
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    if ((long) dbObj.get("refCount") != 0) {
        throw new ObjectInUseException();
    }/*from   w  ww .  j  av  a 2  s.  c om*/

    // Get the existing PortChain
    String portChainName = (String) dbObj.get("name");

    // Remove the PortChain from the database
    table.remove(searchQuery);

    logger.info("Removed Port Chain " + portChainName);

    // Notify the application that the port chaun changed
    // TODO - add objectID
    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.PORT_CHAIN);
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public TapPolicy getTapPolicyById(String tapPolicyId) throws NotFoundException {

    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    // Create a query to look for the existing TapPolicy by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(tapPolicyId));

    // Check whether the policy exists
    DBObject dbObj = table.findOne(searchQuery);
    if (dbObj == null)
        throw new NotFoundException();

    // Construct a new tap policy from the DBobject and add it to list
    TapPolicy tapPolicy = new TapPolicy(dbObj);

    return tapPolicy;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public MatchCriteria getMatchCriteriaById(String matchCriteriaId) throws NotFoundException {
    DBCollection table = database.getCollection(DatabaseNames.getMatchCriteriaTableName());

    // Create a query to look for the existing MatchCriteria by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(matchCriteriaId));

    // Check whether the policy exists
    DBObject dbObj = table.findOne(searchQuery);
    if (dbObj == null)
        throw new NotFoundException();

    // Construct a new match criteria from the DBobject and add it to list
    MatchCriteria matchCriteria = new MatchCriteria(dbObj);

    return matchCriteria;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public SwitchEntry getSwitchEntryById(String switchId) throws NotFoundException {
    DBCollection table = database.getCollection(DatabaseNames.getSwitchEntryTableName());

    // Create a query to look for the existing SwitchEntry by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(switchId));

    // Check whether the policy exists
    DBObject dbObj = table.findOne(searchQuery);
    if (dbObj == null)
        throw new NotFoundException();

    // Construct a new switch entry from the DBobject and return it
    SwitchEntry switchEntry = new SwitchEntry(dbObj, this);
    logger.info("SwitchEntry " + switchEntry);

    return switchEntry;
}