All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opennms.web.svclayer.support.DefaultSiteStatusViewService Maven / Gradle / Ivy

/*
 * Licensed to The OpenNMS Group, Inc (TOG) under one or more
 * contributor license agreements.  See the LICENSE.md file
 * distributed with this work for additional information
 * regarding copyright ownership.
 *
 * TOG licenses this file to You under the GNU Affero General
 * Public License Version 3 (the "License") or (at your option)
 * any later version.  You may not use this file except in
 * compliance with the License.  You may obtain a copy of the
 * License at:
 *
 *      https://www.gnu.org/licenses/agpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied.  See the License for the specific
 * language governing permissions and limitations under the
 * License.
 */
package org.opennms.web.svclayer.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.opennms.netmgt.config.siteStatusViews.Category;
import org.opennms.netmgt.config.siteStatusViews.RowDef;
import org.opennms.netmgt.config.siteStatusViews.View;
import org.opennms.netmgt.dao.api.CategoryDao;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.dao.api.SiteStatusViewConfigDao;
import org.opennms.netmgt.model.AggregateStatusDefinition;
import org.opennms.netmgt.model.AggregateStatusView;
import org.opennms.netmgt.model.OnmsCategory;
import org.opennms.netmgt.model.OnmsNode;
import org.opennms.web.api.Util;
import org.opennms.web.svclayer.SiteStatusViewService;
import org.opennms.web.svclayer.model.AggregateStatus;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.orm.ObjectRetrievalFailureException;

/**
 * This service layer class creates a collection that represents the current
 * status of devices per site (a column from the asset table such as building,
 * floor, etc.)  The status per site is broken down into rows of categories from
 * the categories table.
 *
 * example:
 *
 *              site: HQBLDB
 *
 *  |Routers/Switches |   1 of  20 |
 *  |Servers          |   0 of 200 |
 *  |Hubs/APs         |   5 of  30 |
 *
 * @author David Hustace
 * @author DJ Gregor
 * @author Jeff Gehlbach
 * @author David Hustace
 * @author DJ Gregor
 * @author Jeff Gehlbach
 * @author David Hustace
 * @author DJ Gregor
 * @author Jeff Gehlbach
 * @version $Id: $
 * @since 1.8.1
 */
public class DefaultSiteStatusViewService implements SiteStatusViewService {
    
    private NodeDao m_nodeDao;
    private CategoryDao m_categoryDao;
    private SiteStatusViewConfigDao m_siteStatusViewConfigDao;
    
    
    /**
     * {@inheritDoc}
     *
     * This creator looks up a configured status view by name and calls the creator that
     * accepts the AggregateStatusView model object.
     * @see org.opennms.web.svclayer.SiteStatusViewService#createAggregateStatusView(java.lang.String)
     */
    @Override
    public AggregateStatusView createAggregateStatusView(String statusViewName) {
        AggregateStatusView statusView = new AggregateStatusView();
        statusViewName = (statusViewName == null ? m_siteStatusViewConfigDao.getDefaultView().getName() : statusViewName);
        
        View view = m_siteStatusViewConfigDao.getView(statusViewName);
        
        statusView.setName(statusViewName);
        statusView.setColumnName(view.getColumnName());
        statusView.setColumnValue(view.getColumnValue().orElse(null));
        statusView.setTableName(view.getTableName());
        
        Set statusDefs =
            getAggregateStatusDefinitionsForView(view);
        statusView.setStatusDefinitions(statusDefs);
        return statusView;
    }


    private Set getAggregateStatusDefinitionsForView(View view) {
        Set statusDefs = new LinkedHashSet<>();
        //Loop over the defined site status rows
        for (RowDef rowDef : view.getRows()) {
            AggregateStatusDefinition def = new AggregateStatusDefinition();
            def.setName(rowDef.getLabel());
            def.setReportCategory(rowDef.getReportCategory());
            
            Set categories = getCategoriesForRowDef(rowDef);
            def.setCategories(categories);
            statusDefs.add(def);
        }
        return statusDefs;
    }


    private Set getCategoriesForRowDef(RowDef rowDef) {
        Set categories = new LinkedHashSet<>();
        
        //Loop over the defined categories and create model categories (OnmsCategory)
        List cats = rowDef.getCategories();
        for (Category cat : cats) {
            OnmsCategory category = m_categoryDao.findByName(cat.getName());
            
            if (category == null) {
                throw new ObjectRetrievalFailureException(OnmsCategory.class, cat.getName(), "Unable to locate OnmsCategory named: "+cat.getName()+" as specified in the site status view configuration file", null);
            }
            
            categories.add(category);
        }
        return categories;
    }


    /**
     * {@inheritDoc}
     *
     * Use the node id to find the value associated with column defined in the view.  The view defines a column
     * and column value to be used by default.  This method determines the column value using the value associated
     * with the asset record for the given nodeid.
     * @see org.opennms.web.svclayer.SiteStatusViewService#createAggregateStatusesUsingNodeId(int, java.lang.String)
     */
    @Override
    public Collection createAggregateStatusesUsingNodeId(int nodeId, String viewName) {

        OnmsNode node = m_nodeDao.load(nodeId);
        
        //TODO this is a hack.  need to use reflection to get the right column instead of building.
        return createAggregateStatuses(createAggregateStatusView(viewName), node.getAssetRecord().getBuilding());
    }



    /**
     * {@inheritDoc}
     *
     * This creator is used when wanting to use a different value than the defined column value defined
     * for the requested view.
     * @see org.opennms.web.svclayer.SiteStatusViewService#createAggregateStatuses(org.opennms.netmgt.model.AggregateStatusView, java.lang.String)
     */
    @Override
    public Collection createAggregateStatuses(AggregateStatusView statusView, String statusSite) {
        if (statusView == null) {
            throw new IllegalArgumentException("statusView argument cannot be null");
        }
        statusView.setColumnValue(statusSite);
        return createAggregateStatusUsingAssetColumn(statusView);
    }

    
    /**
     * 

createAggregateStatusUsingAssetColumn

* * @param statusView a {@link org.opennms.netmgt.model.AggregateStatusView} object. * @return a {@link java.util.Collection} object. */ public Collection createAggregateStatusUsingAssetColumn(AggregateStatusView statusView) { if (statusView == null) { throw new IllegalArgumentException("statusView argument cannot be null"); } /* * We'll return this collection populated with all the aggregated statuss for the * devices in the building (site) by for each group of categories. */ Collection stati = new ArrayList<>(); /* * Iterate over the status definitions and create aggregated statuss */ for (AggregateStatusDefinition statusDef : statusView.getStatusDefinitions()) { Collection nodes = m_nodeDao.findAllByVarCharAssetColumnCategoryList(statusView.getColumnName(), statusView.getColumnValue(), statusDef.getCategories()); AggregateStatus status = new AggregateStatus(new HashSet(nodes)); status.setLabel(statusDef.getName()); status.setLink(createNodePageUrl(statusView, status)); stati.add(status); } return stati; } private String createNodePageUrl(AggregateStatusView statusView, AggregateStatus status) { if (status.getDownEntityCount() == 0) { final StringBuilder buf = new StringBuilder("element/nodeList.htm?"); buf.append("statusViewName="); buf.append(Util.encode(statusView.getName())); buf.append('&'); buf.append("statusSite="); buf.append(Util.encode(statusView.getColumnValue())); buf.append('&'); buf.append("statusRowLabel="); buf.append(Util.encode(status.getLabel())); return buf.toString(); } else if (status.getDownEntityCount() == 1) { OnmsNode node = status.getDownNodes().iterator().next(); StringBuffer buf = new StringBuffer("element/node.jsp?"); buf.append("node="); buf.append(node.getId()); return buf.toString(); } else { StringBuffer buf = new StringBuffer("element/nodeList.htm?"); buf.append("statusViewName="); buf.append(Util.encode(statusView.getName())); buf.append('&'); buf.append("statusSite="); buf.append(Util.encode(statusView.getColumnValue())); buf.append('&'); buf.append("statusRowLabel="); buf.append(Util.encode(status.getLabel())); buf.append('&'); buf.append("nodesWithDownAggregateStatus=true"); return buf.toString(); } } /** *

getNodeDao

* * @return a {@link org.opennms.netmgt.dao.api.NodeDao} object. */ public NodeDao getNodeDao() { return m_nodeDao; } /** *

setNodeDao

* * @param nodeDao a {@link org.opennms.netmgt.dao.api.NodeDao} object. */ public void setNodeDao(NodeDao nodeDao) { m_nodeDao = nodeDao; } /** *

setCategoryDao

* * @param dao a {@link org.opennms.netmgt.dao.api.CategoryDao} object. */ public void setCategoryDao(CategoryDao dao) { m_categoryDao = dao; } /** *

setSiteStatusViewConfigDao

* * @param dao a {@link org.opennms.netmgt.dao.api.SiteStatusViewConfigDao} object. */ public void setSiteStatusViewConfigDao(SiteStatusViewConfigDao dao) { m_siteStatusViewConfigDao = dao; } /** {@inheritDoc} */ @Override public Collection createAggregateStatuses(AggregateStatusView statusView) { if (! "assets".equalsIgnoreCase("assets")) { throw new IllegalArgumentException("statusView only currently supports asset table columns"); } return createAggregateStatusUsingAssetColumn(statusView); } /** {@inheritDoc} */ @Override public AggregateStatus getAggregateStatus(String statusViewName, String statusSite, String rowLabel) { AggregateStatusView statusView = createAggregateStatusView(statusViewName); Collection stati = createAggregateStatuses(statusView, statusSite); for (AggregateStatus status : stati) { if (status.getLabel().equals(rowLabel)) { return status; } } throw new DataRetrievalFailureException("Unable to locate row: "+rowLabel+" for status view: "+statusViewName); } /** {@inheritDoc} */ @Override public Collection getNodes(String statusViewName, String statusSite, String rowLabel) { if (statusViewName == null) { statusViewName = m_siteStatusViewConfigDao.getDefaultView().getName(); } View view = m_siteStatusViewConfigDao.getView(statusViewName); RowDef rowDef = getRowDef(view, rowLabel); Set categories = getCategoriesForRowDef(rowDef); return m_nodeDao.findAllByVarCharAssetColumnCategoryList(view.getColumnName(), statusSite, categories); } private RowDef getRowDef(View view, String rowLabel) { for (final RowDef rowDef : view.getRows()) { if (rowDef.getLabel().equals(rowLabel)) { return rowDef; } } throw new DataRetrievalFailureException("Unable to locate row: "+rowLabel+" for status view: "+view.getName()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy