Search in sources :

Example 21 with OnmsAssetRecord

use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.

the class AssetRecordDaoIT method testCreateAndGets.

@Test
@Transactional
public void testCreateAndGets() {
    OnmsNode onmsNode = new OnmsNode(m_locationDao.getDefaultLocation(), "myNode");
    m_nodeDao.save(onmsNode);
    OnmsAssetRecord assetRecord = onmsNode.getAssetRecord();
    assetRecord.setAssetNumber("imported-id: 7");
    m_assetRecordDao.update(assetRecord);
    m_assetRecordDao.flush();
    //Test findAll method
    Collection<OnmsAssetRecord> assetRecords = m_assetRecordDao.findAll();
    assertEquals(7, assetRecords.size());
    //Test countAll method
    assertEquals(7, m_assetRecordDao.countAll());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with OnmsAssetRecord

use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.

the class AssetSuggestionsRestService method getAssetSuggestions.

/**
     * Gets the asset suggestions.
     *
     * @return the asset suggestions
     */
@GET
@Path("suggestions")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
public Suggestions getAssetSuggestions() {
    final Suggestions suggestions = new Suggestions();
    final List<OnmsAssetRecord> distinctAssetProperties = m_assetDao.getDistinctProperties();
    final List<String> attributes = PropertyUtils.getProperties(new OnmsAssetRecord()).stream().filter(a -> !BLACK_LIST.contains(a)).collect(Collectors.toList());
    distinctAssetProperties.forEach(record -> {
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(record);
        attributes.forEach(attribute -> {
            if (!suggestions.containsKey(attribute)) {
                suggestions.put(attribute, new SuggestionList());
            }
            final Object value = wrapper.getPropertyValue(attribute);
            if (value != null) {
                final SuggestionList list = suggestions.get(attribute);
                if (!list.contains(value.toString())) {
                    list.add(value.toString());
                }
            }
        });
    });
    return suggestions;
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) Path(javax.ws.rs.Path) PropertyAccessorFactory(org.springframework.beans.PropertyAccessorFactory) BeanWrapper(org.springframework.beans.BeanWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) AssetRecordDao(org.opennms.netmgt.dao.api.AssetRecordDao) JaxbListWrapper(org.opennms.core.config.api.JaxbListWrapper) InitializingBean(org.springframework.beans.factory.InitializingBean) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) PropertyUtils(org.opennms.web.svclayer.support.PropertyUtils) Collection(java.util.Collection) Set(java.util.Set) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) Collections(java.util.Collections) XmlElement(javax.xml.bind.annotation.XmlElement) Assert(org.springframework.util.Assert) BeanWrapper(org.springframework.beans.BeanWrapper) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 23 with OnmsAssetRecord

use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.

the class AssetRecordResource method updateAssetRecord.

/**
     * <p>updateAssetRecord</p>
     *
     * @param nodeCriteria a {@link java.lang.String} object.
     * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
     * @return a {@link javax.ws.rs.core.Response} object.
     */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateAssetRecord(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params) {
    OnmsNode node = m_nodeDao.get(nodeCriteria);
    if (node == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Can't find node " + nodeCriteria);
    }
    OnmsAssetRecord assetRecord = getAssetRecord(node);
    if (assetRecord == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Node " + node + " could not update ");
    }
    if (assetRecord.getGeolocation() == null) {
        assetRecord.setGeolocation(new OnmsGeolocation());
    }
    LOG.debug("updateAssetRecord: updating asset {}", assetRecord);
    boolean modified = false;
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(assetRecord);
    wrapper.registerCustomEditor(Date.class, new ISO8601DateEditor());
    for (String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            String stringValue = params.getFirst(key);
            Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    if (modified) {
        LOG.debug("updateAssetRecord: assetRecord {} updated", assetRecord);
        m_assetRecordDao.saveOrUpdate(assetRecord);
        try {
            sendEvent(EventConstants.ASSET_INFO_CHANGED_EVENT_UEI, node.getId());
        } catch (EventProxyException e) {
            throw getException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
        }
        return Response.noContent().build();
    }
    return Response.notModified().build();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) ISO8601DateEditor(org.opennms.web.api.ISO8601DateEditor) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)23 OnmsNode (org.opennms.netmgt.model.OnmsNode)18 Test (org.junit.Test)8 OnmsGeolocation (org.opennms.netmgt.model.OnmsGeolocation)5 Transactional (org.springframework.transaction.annotation.Transactional)5 BeanWrapper (org.springframework.beans.BeanWrapper)4 InetAddress (java.net.InetAddress)3 NodeDao (org.opennms.netmgt.dao.api.NodeDao)3 OnmsCategory (org.opennms.netmgt.model.OnmsCategory)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Before (org.junit.Before)2 MockCollectionAgent (org.opennms.core.collection.test.MockCollectionAgent)2 RrdPersisterFactory (org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory)2 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)2 AssetField (org.opennms.netmgt.config.snmpAsset.adapter.AssetField)2 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)2 JRobinRrdStrategy (org.opennms.netmgt.rrd.jrobin.JRobinRrdStrategy)2 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)2