Search in sources :

Example 51 with OnmsResource

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

the class DefaultResourceDao method getChildResource.

/**
 * <p>getChildResource</p>
 *
 * @param parentResource a {@link org.opennms.netmgt.model.OnmsResource} object.
 * @param resourceType a {@link java.lang.String} object.
 * @param resource a {@link java.lang.String} object.
 * @return a {@link org.opennms.netmgt.model.OnmsResource} object.
 */
protected OnmsResource getChildResource(OnmsResource parentResource, String resourceType, String resource) {
    final OnmsResourceType targetType = m_resourceTypes.get(resourceType);
    if (targetType == null) {
        throw new ObjectRetrievalFailureException(OnmsResource.class, resourceType + "/" + resource, "Unsupported resource type: " + resourceType, null);
    }
    final OnmsResource childResource = targetType.getChildByName(parentResource, resource);
    if (childResource != null) {
        LOG.debug("getChildResource: returning resource {}", childResource);
        return childResource;
    }
    throw new ObjectRetrievalFailureException(OnmsResource.class, resourceType + "/" + resource, "Could not find child resource '" + resource + "' with resource type '" + resourceType + "' on resource '" + resource + "'", null);
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Example 52 with OnmsResource

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

the class DistributedStatusResourceType method getResourcesForParent.

/**
 * {@inheritDoc}
 */
@Override
public List<OnmsResource> getResourcesForParent(OnmsResource parent) {
    if (!NodeResourceType.isNode(parent)) {
        return Collections.emptyList();
    }
    final OnmsNode node = ResourceTypeUtils.getNodeFromResource(parent);
    final List<OnmsResource> resources = Lists.newLinkedList();
    final Collection<LocationMonitorIpInterface> statuses = m_locationMonitorDao.findStatusChangesForNodeForUniqueMonitorAndInterface(node.getId());
    for (LocationMonitorIpInterface status : statuses) {
        String definitionName = status.getLocationMonitor().getLocation();
        String id = status.getLocationMonitor().getId();
        final OnmsIpInterface ipInterface = status.getIpInterface();
        String ipAddr = InetAddressUtils.str(ipInterface.getIpAddress());
        if (m_resourceStorageDao.exists(getRelativeInterfacePath(id, ipAddr), 0)) {
            resources.add(createResource(definitionName, id, ipAddr));
        }
    }
    return OnmsResource.sortIntoResourceList(resources);
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) LocationMonitorIpInterface(org.opennms.netmgt.model.LocationMonitorIpInterface)

Example 53 with OnmsResource

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

the class DistributedStatusResourceType method createResource.

private OnmsResource createResource(String definitionName, String locationMonitorId, String ipAddress) {
    String monitor = definitionName + "-" + locationMonitorId;
    String label = ipAddress + " from " + monitor;
    final ResourcePath path = getRelativeInterfacePath(locationMonitorId, ipAddress);
    final LazyResourceAttributeLoader loader = new LazyResourceAttributeLoader(m_resourceStorageDao, path);
    final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(loader);
    return new OnmsResource(getResourceName(locationMonitorId, ipAddress), label, this, set, path);
}
Also used : LazySet(org.opennms.core.collections.LazySet) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute)

Example 54 with OnmsResource

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

the class GenericIndexResourceType method getResourcesForParent.

/**
 * {@inheritDoc}
 */
@Override
public List<OnmsResource> getResourcesForParent(OnmsResource parent) {
    if (parent == null) {
        return Collections.emptyList();
    }
    List<OnmsResource> resources = Lists.newArrayList();
    List<String> indexes = getQueryableIndexes(new ResourcePath(parent.getPath(), m_name));
    for (String index : indexes) {
        resources.add(getResourceByPath(new ResourcePath(parent.getPath(), m_name, index), parent));
    }
    return OnmsResource.sortIntoResourceList(resources);
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourcePath(org.opennms.netmgt.model.ResourcePath)

Example 55 with OnmsResource

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

the class GenericIndexResourceType method getResourceByPath.

public OnmsResource getResourceByPath(final ResourcePath path, final OnmsResource parent) {
    final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(new LazyResourceAttributeLoader(m_resourceStorageDao, path));
    final String index = path.getName();
    String label;
    if (m_resourceLabelExpression == null) {
        label = index;
    } else {
        SymbolTable symbolTable = new SymbolTable() {

            private int lastN;

            private boolean lastNSet = false;

            @Override
            public String getSymbolValue(String symbol) {
                if (symbol.equals("index")) {
                    return index;
                }
                Matcher subIndexMatcher = SUB_INDEX_PATTERN.matcher(symbol);
                if (subIndexMatcher.matches()) {
                    Matcher subIndexArgumentsMatcher = SUB_INDEX_ARGUMENTS_PATTERN.matcher(subIndexMatcher.group(1));
                    if (!subIndexArgumentsMatcher.matches()) {
                        // Invalid arguments
                        return null;
                    }
                    List<String> indexElements = tokenizeIndex(index);
                    int start;
                    int offset;
                    if ("n".equals(subIndexArgumentsMatcher.group(1)) && lastNSet) {
                        start = lastN;
                        lastNSet = false;
                    } else if ("n".equals(subIndexArgumentsMatcher.group(1))) {
                        // Invalid use of "n" when lastN is not set
                        return null;
                    } else {
                        offset = Integer.parseInt(subIndexArgumentsMatcher.group(1));
                        if (offset < 0) {
                            start = indexElements.size() + offset;
                        } else {
                            start = offset;
                        }
                    }
                    int end;
                    if ("n".equals(subIndexArgumentsMatcher.group(2))) {
                        end = start + Integer.parseInt(indexElements.get(start)) + 1;
                        start++;
                        lastN = end;
                        lastNSet = true;
                    } else {
                        if (subIndexArgumentsMatcher.group(2) == null) {
                            end = indexElements.size();
                        } else {
                            end = start + Integer.parseInt(subIndexArgumentsMatcher.group(2));
                        }
                    }
                    if (start < 0 || start >= indexElements.size()) {
                        // Bogus index start
                        return null;
                    }
                    if (end < 0 || end > indexElements.size()) {
                        // Bogus index end
                        return null;
                    }
                    final StringBuilder indexSubString = new StringBuilder();
                    for (int i = start; i < end; i++) {
                        if (indexSubString.length() != 0) {
                            indexSubString.append(".");
                        }
                        indexSubString.append(indexElements.get(i));
                    }
                    return indexSubString.toString();
                }
                Matcher hexMatcher = HEX_PATTERN.matcher(symbol);
                if (hexMatcher.matches()) {
                    String subSymbol = getSymbolValue(hexMatcher.group(1));
                    List<String> indexElements = tokenizeIndex(subSymbol);
                    final StringBuilder hexString = new StringBuilder();
                    for (String indexElement : indexElements) {
                        if (hexString.length() > 0) {
                            hexString.append(":");
                        }
                        try {
                            hexString.append(String.format("%02X", Integer.parseInt(indexElement)));
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    }
                    return hexString.toString();
                }
                Matcher stringMatcher = STRING_PATTERN.matcher(symbol);
                if (stringMatcher.matches()) {
                    String subSymbol = getSymbolValue(stringMatcher.group(1));
                    List<String> indexElements = tokenizeIndex(subSymbol);
                    StringBuffer stringString = new StringBuffer();
                    for (String indexElement : indexElements) {
                        stringString.append(String.format("%c", Integer.parseInt(indexElement)));
                    }
                    return stringString.toString();
                }
                for (OnmsAttribute attr : set) {
                    if (symbol.equals(attr.getName())) {
                        if (StringPropertyAttribute.class.isAssignableFrom(attr.getClass())) {
                            StringPropertyAttribute stringAttr = (StringPropertyAttribute) attr;
                            return stringAttr.getValue();
                        }
                        if (ExternalValueAttribute.class.isAssignableFrom(attr.getClass())) {
                            ExternalValueAttribute extAttr = (ExternalValueAttribute) attr;
                            return extAttr.getValue();
                        }
                    }
                }
                return null;
            }

            private List<String> tokenizeIndex(final String index) {
                List<String> indexElements = new ArrayList<>();
                StringTokenizer t = new StringTokenizer(index, ".");
                while (t.hasMoreTokens()) {
                    indexElements.add(t.nextToken());
                }
                return indexElements;
            }
        };
        label = PropertiesUtils.substitute(m_resourceLabelExpression, symbolTable);
    }
    final OnmsResource resource = new OnmsResource(index, label, this, set, path);
    resource.setParent(parent);
    return resource;
}
Also used : LazySet(org.opennms.core.collections.LazySet) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SymbolTable(org.opennms.core.utils.PropertiesUtils.SymbolTable) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) StringTokenizer(java.util.StringTokenizer) OnmsResource(org.opennms.netmgt.model.OnmsResource) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute)

Aggregations

OnmsResource (org.opennms.netmgt.model.OnmsResource)143 Test (org.junit.Test)54 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)40 MockResourceType (org.opennms.netmgt.mock.MockResourceType)31 OnmsNode (org.opennms.netmgt.model.OnmsNode)28 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)23 File (java.io.File)22 ResourcePath (org.opennms.netmgt.model.ResourcePath)19 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)14 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)12 HashMap (java.util.HashMap)11 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)11 ResourceId (org.opennms.netmgt.model.ResourceId)11 Date (java.util.Date)10 Source (org.opennms.netmgt.measurements.model.Source)8 LinkedList (java.util.LinkedList)7 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)7 ExternalValueAttribute (org.opennms.netmgt.model.ExternalValueAttribute)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7