Search in sources :

Example 1 with OnmsHwEntity

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

the class EntityPhysicalTableTracker method rowCompleted.

/* (non-Javadoc)
     * @see org.opennms.netmgt.snmp.TableTracker#rowCompleted(org.opennms.netmgt.snmp.SnmpRowResult)
     */
@Override
public void rowCompleted(SnmpRowResult row) {
    OnmsHwEntity entity = ((EntityPhysicalTableRow) row).getOnmsHwEntity(vendorAttributes, replacementMap);
    LOG.debug("rowCompleted: found entity {}, index: {}, parent: {}", entity.getEntPhysicalName(), entity.getEntPhysicalIndex(), entity.getEntPhysicalContainedIn());
    if (entity.getEntPhysicalContainedIn() != null && entity.getEntPhysicalContainedIn() > 0) {
        OnmsHwEntity parent = getParent(entity.getEntPhysicalContainedIn().intValue());
        if (parent != null) {
            LOG.debug("rowCompleted: adding child index {} to parent index {}", entity.getEntPhysicalIndex(), parent.getEntPhysicalIndex());
            parent.addChildEntity(entity);
        }
    }
    entities.add(entity);
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity)

Example 2 with OnmsHwEntity

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

the class EventUtilDaoImpl method getHardwareFieldValue.

@Override
public String getHardwareFieldValue(String parm, long nodeId) {
    final Matcher matcher = HW_PARM_PATTERN.matcher(parm);
    if (!matcher.matches()) {
        LOG.warn("Unsupported hardware field parameter '{}'.", parm);
        return null;
    }
    final String hwFieldSelector = matcher.group(1);
    final String hwField = matcher.group(2).toLowerCase();
    // Retrieve the entity with a like query
    if (hwFieldSelector.startsWith("~")) {
        final String likeQuery = hwFieldSelector.substring(1);
        LOG.debug("Retrieving hardware field value {} on {} with like query {}", parm, nodeId, likeQuery);
        Criteria criteria = new Criteria(OnmsHwEntity.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", (int) nodeId)).addRestriction(new LikeRestriction("entPhysicalName", likeQuery)).setOrders(Arrays.asList(new Order[] { Order.desc("id") }));
        List<OnmsHwEntity> hwEntities = hwEntityDao.findMatching(criteria);
        System.err.println(hwEntities);
        if (hwEntities.size() < 1) {
            return null;
        }
        return getStringPropertyByName(hwField, hwEntities.get(0), hwEntityDescriptorsByName);
    }
    // Retrieve the entity by index if the select is an integer
    try {
        int index = Integer.parseInt(hwFieldSelector);
        OnmsHwEntity hwEntity = hwEntityDao.findEntityByIndex((int) nodeId, index);
        if (hwEntity == null) {
            // No entry with this index
            return null;
        }
        return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
    } catch (NumberFormatException e) {
    // pass
    }
    // Retrieve the entity by name
    OnmsHwEntity hwEntity = hwEntityDao.findEntityByName((int) nodeId, hwFieldSelector);
    if (hwEntity == null) {
        // No entry with this name
        return null;
    }
    return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
}
Also used : Order(org.opennms.core.criteria.Order) OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) Matcher(java.util.regex.Matcher) Alias(org.opennms.core.criteria.Alias) LikeRestriction(org.opennms.core.criteria.restrictions.LikeRestriction) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria)

Example 3 with OnmsHwEntity

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

the class EventUtilHibernateIT method getHardwareFieldValue.

@Test
public void getHardwareFieldValue() {
    OnmsNode node1 = m_populator.getNode1();
    OnmsHwEntity hwEntity = new OnmsHwEntity();
    hwEntity.setNode(node1);
    hwEntity.setEntPhysicalIndex(0);
    hwEntity.setEntPhysicalName("Chassis");
    hwEntity.setEntPhysicalDescr("some-physical-d3scr");
    m_hwEntityDao.save(hwEntity);
    // Access the field by index
    String hwfield = eventUtilDaoImpl.getHardwareFieldValue("hardware[0:entPhysicalDescr]", node1.getId());
    assertEquals("some-physical-d3scr", hwfield);
    // Access the field by name
    hwfield = eventUtilDaoImpl.getHardwareFieldValue("hardware[Chassis:entPhysicalDescr]", node1.getId());
    assertEquals("some-physical-d3scr", hwfield);
    // Access the field by regex
    hwfield = eventUtilDaoImpl.getHardwareFieldValue("hardware[~%Cha%:entPhysicalDescr]", node1.getId());
    assertEquals("some-physical-d3scr", hwfield);
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test)

Example 4 with OnmsHwEntity

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

the class EntityPhysicalTableRow method getOnmsHwEntity.

/**
 * Gets the hardware entity.
 *
 * @param vendorAttributes the vendor attributes
 * @param replacementMap the replacement map
 * @return the hardware entity
 */
public OnmsHwEntity getOnmsHwEntity(Map<SnmpObjId, HwEntityAttributeType> vendorAttributes, Map<String, String> replacementMap) {
    SnmpValue v = null;
    final OnmsHwEntity entity = new OnmsHwEntity();
    entity.setEntPhysicalIndex(getEntPhysicalIndex());
    entity.setEntPhysicalClass(getEntPhysicalClass());
    v = getValue(entPhysicalDescr);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalDescr(v.toDisplayString().trim());
    v = getValue(entPhysicalVendorType);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalVendorType(v.toDisplayString().trim());
    v = getValue(entPhysicalContainedIn);
    if (v != null)
        entity.setEntPhysicalContainedIn(v.toInt());
    v = getValue(entPhysicalParentRelPos);
    if (v != null)
        entity.setEntPhysicalParentRelPos(v.toInt());
    v = getValue(entPhysicalName);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalName(v.toDisplayString().trim().trim());
    v = getValue(entPhysicalHardwareRev);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalHardwareRev(v.toDisplayString().trim());
    v = getValue(entPhysicalFirmwareRev);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalFirmwareRev(v.toDisplayString().trim());
    v = getValue(entPhysicalSoftwareRev);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalSoftwareRev(v.toDisplayString().trim());
    v = getValue(entPhysicalSerialNum);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalSerialNum(v.toDisplayString().trim());
    v = getValue(entPhysicalMfgName);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalMfgName(v.toDisplayString().trim());
    v = getValue(entPhysicalModelName);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalModelName(v.toDisplayString().trim());
    v = getValue(entPhysicalAlias);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalAlias(v.toDisplayString().trim());
    v = getValue(entPhysicalAssetID);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalAssetID(v.toDisplayString().trim());
    v = getValue(entPhysicalIsFRU);
    if (v != null)
        entity.setEntPhysicalIsFRU(v.toInt() == 1 ? true : false);
    v = getValue(entPhysicalUris);
    if (v != null && !v.toDisplayString().trim().isEmpty())
        entity.setEntPhysicalUris(v.toDisplayString());
    if (vendorAttributes != null && vendorAttributes.size() > 0) {
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity);
        for (Map.Entry<SnmpObjId, HwEntityAttributeType> entry : vendorAttributes.entrySet()) {
            v = getValue(entry.getKey());
            if (v != null && !v.toDisplayString().trim().isEmpty()) {
                String typeName = entry.getValue().getName();
                if (replacementMap.containsKey(typeName)) {
                    String property = replacementMap.get(typeName);
                    if (wrapper.isWritableProperty(property)) {
                        wrapper.setPropertyValue(property, v.toDisplayString().trim());
                    }
                } else {
                    entity.addAttribute(entry.getValue(), v.toDisplayString().trim());
                }
            }
        }
    }
    return entity;
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) BeanWrapper(org.springframework.beans.BeanWrapper) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) HwEntityAttributeType(org.opennms.netmgt.model.HwEntityAttributeType) Map(java.util.Map)

Example 5 with OnmsHwEntity

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

the class HardwareInventoryResource method updateTypes.

/**
 * Update types.
 *
 * @param typesMap the types map
 * @param entity the entity
 */
private void updateTypes(Map<String, HwEntityAttributeType> typesMap, OnmsHwEntity entity) {
    for (OnmsHwEntityAttribute a : entity.getHwEntityAttributes()) {
        final String typeName = a.getTypeName();
        if (!typesMap.containsKey(typeName)) {
            HwEntityAttributeType t = m_hwEntityAttribTypeDao.findTypeByName(typeName);
            if (t == null) {
                t = a.getType();
                m_hwEntityAttribTypeDao.save(t);
            }
            typesMap.put(t.getName(), t);
        }
        a.setType(typesMap.get(typeName));
    }
    for (OnmsHwEntity child : entity.getChildren()) {
        updateTypes(typesMap, child);
    }
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) OnmsHwEntityAttribute(org.opennms.netmgt.model.OnmsHwEntityAttribute) HwEntityAttributeType(org.opennms.netmgt.model.HwEntityAttributeType)

Aggregations

OnmsHwEntity (org.opennms.netmgt.model.OnmsHwEntity)16 OnmsNode (org.opennms.netmgt.model.OnmsNode)8 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Test (org.junit.Test)3 HwEntityAttributeType (org.opennms.netmgt.model.HwEntityAttributeType)3 FileWriter (java.io.FileWriter)2 POST (javax.ws.rs.POST)2 OnmsHwEntityAttribute (org.opennms.netmgt.model.OnmsHwEntityAttribute)2 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)2 BeanWrapper (org.springframework.beans.BeanWrapper)2 Transactional (org.springframework.transaction.annotation.Transactional)2 InetAddress (java.net.InetAddress)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Produces (javax.ws.rs.Produces)1