use of cern.laser.business.data.CategoryImpl in project ACS by ACS-Community.
the class ACSCategoryDAOImpl method addRootCategory.
/**
* Add the ROOT category
*
* This avoid the user to add this entry in the CDB
*/
private void addRootCategory() {
CategoryImpl ci = new CategoryImpl();
// will get filled in later
ci.setAlarmIds(new HashSet());
ci.setCategoryId(new Integer(nextCatID++));
ci.setChildrenIds(new HashSet());
ci.setDescription("ROOT category");
ci.setName("ROOT");
ci.setParentId(null);
ci.setPath("ROOT");
ci.setAlarmIds(new HashSet());
// Stores the categories
categories.put(ci.getCategoryId(), ci);
catPathToCategory.put(ci.getPath(), ci);
}
use of cern.laser.business.data.CategoryImpl in project ACS by ACS-Community.
the class ACSCategoryDAOImpl method loadCategories.
/**
* Load the categories from the CDB.
* <P>
* Loads all the category from the CDB and build an internal
* representation of category.
* The category is also added to all the alarms having the
* fault family specified in the XML.
* <P>
* All the categories derive from ROOT that is built here
* as default (in this way the user does ot need to add the ROOT
* entry in the CDB).
*
* @return list of Category entries read from CDB
* @throws Exception In case of error reading the values from the CDB
*/
public alma.acs.alarmsystem.generated.Category[] loadCategories() throws Exception {
if (conf == null) {
throw new IllegalStateException("Missing dal");
}
categories.clear();
String xml;
try {
xml = conf.getConfiguration(CATEGORY_DEFINITION_PATH);
} catch (Throwable t) {
throw new RuntimeException("Couldn't read alarm list", t);
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (Exception e) {
throw new Exception("Error building the DocumentBuilder from the DocumentBuilderFactory", e);
}
StringReader stringReader = new StringReader(xml);
InputSource inputSource = new InputSource(stringReader);
Document doc;
try {
doc = builder.parse(inputSource);
if (doc == null) {
throw new Exception("The builder returned a null Document after parsing");
}
} catch (Exception e) {
throw new Exception("Error parsing XML: " + xml, e);
}
NodeList docChilds = doc.getChildNodes();
if (docChilds == null || docChilds.getLength() != 1) {
throw new Exception("Malformed xml: only one node (categories) expected");
}
Node categoriesNode = docChilds.item(0);
Unmarshaller FF_unmarshaller = new Unmarshaller(Categories.class);
FF_unmarshaller.setValidation(false);
FF_unmarshaller.setWhitespacePreserve(true);
Categories daoCategories;
try {
daoCategories = (Categories) FF_unmarshaller.unmarshal(categoriesNode);
logger.log(AcsLogLevel.DEBUG, "Categories definition read");
} catch (Exception e) {
throw new Exception("Error parsing " + CATEGORY_DEFINITION_PATH, e);
}
alma.acs.alarmsystem.generated.Category[] daoCategory = daoCategories.getCategory();
if (daoCategory == null || daoCategory.length == 0) {
logger.log(AcsLogLevel.DEBUG, "No category defined");
}
// Add the root Category
addRootCategory();
// Goes through all the Categories read from the CDB
for (alma.acs.alarmsystem.generated.Category category : daoCategory) {
cern.laser.business.definition.data.CategoryDefinition definition = new cern.laser.business.definition.data.CategoryDefinition(category.getPath(), category.getDescription());
CategoryImpl ci = new CategoryImpl();
// will get filled in later
ci.setAlarmIds(new HashSet());
ci.setCategoryId(new Integer(nextCatID++));
ci.setChildrenIds(new HashSet<Integer>());
ci.setDescription(definition.getDescription());
ci.setName(definition.getPath());
ci.setPath(definition.getPath());
ci.setAlarmIds(new HashSet());
setParentID(ci);
// Stores the categories
categories.put(ci.getCategoryId(), ci);
catPathToCategory.put(ci.getPath(), ci);
logger.log(AcsLogLevel.DEBUG, "Category " + ci.getName() + " added with ID=" + ci.getCategoryId());
// Check if the category is defined as default.
if (category.hasIsDefault() && category.getIsDefault() == true) {
if (defaultCategory != null) {
StringBuilder str = new StringBuilder("CDB misconfiguration: default category defined more then once (actual default: ");
str.append(defaultCategory.getPath());
str.append(", new default: ");
str.append(category.getPath());
logger.log(AcsLogLevel.WARNING, str.toString());
} else {
defaultCategory = ci;
}
}
// A category contains a set of child ids.
// This method adjusts the references of categories between the parent
// and the child
adjustParentIDs(ci.getName(), ci.getCategoryId());
// Connect alarms to this category
for (alma.acs.alarmsystem.generated.Category cat : daoCategories.getCategory()) {
if (cat.getPath().equals(ci.getPath())) {
String[] families = cat.getAlarms().getFaultFamily();
for (String faultFamily : families) {
assignCategoryToAlarms(ci, faultFamily);
}
}
}
}
// Assign core alarms to ROOT category
assignCategoryOfCoreAlarms();
// Log a message if no category has been defined in the CDB
if (defaultCategory == null) {
logger.log(AcsLogLevel.WARNING, "No default category defined in CDB");
} else {
// Check if there are alarms without category to assign to the default
assignDefaultCategory(defaultCategory);
}
return daoCategory;
}
use of cern.laser.business.data.CategoryImpl in project ACS by ACS-Community.
the class ACSCategoryDAOImpl method flushCategory.
public void flushCategory() {
if (conf == null || !conf.isWriteable())
throw new IllegalStateException("no writable configuration accessor");
CategoryDefinitions acds = new CategoryDefinitions();
CategoryDefinitionListType ctc = new CategoryDefinitionListType();
acds.setCategoriesToCreate(ctc);
AlarmCategoryDefinitions linksTop = new AlarmCategoryDefinitions();
AlarmCategoryLinkDefinitionListType cltc = new AlarmCategoryLinkDefinitionListType();
linksTop.setCategoryLinksToCreate(cltc);
Iterator<Category> i = catPathToCategory.values().iterator();
while (i.hasNext()) {
CategoryDefinition cd = new CategoryDefinition();
CategoryImpl ci = (CategoryImpl) i.next();
cd.setDescription(ci.getDescription());
cd.setPath(ci.getPath());
ctc.addCategoryDefinition(cd);
Iterator aidsi = ci.getAlarmIds().iterator();
while (aidsi.hasNext()) {
String aid = (String) aidsi.next();
Alarm a = alarmDao.getAlarm(aid);
if (a == null)
throw new RuntimeException("Category has a link to a non-existent alarm");
AlarmCategoryLinkType link = new AlarmCategoryLinkType();
alma.alarmsystem.alarmmessage.generated.Alarm linkAlarm = new alma.alarmsystem.alarmmessage.generated.Alarm();
alma.alarmsystem.alarmmessage.generated.Category linkCat = new alma.alarmsystem.alarmmessage.generated.Category();
link.setAlarm(linkAlarm);
link.setCategory(linkCat);
AlarmDefinition linkAlarmDef = new AlarmDefinition();
CategoryDefinition linkCatDef = new CategoryDefinition();
linkAlarm.setAlarmDefinition(linkAlarmDef);
linkCat.setCategoryDefinition(linkCatDef);
linkAlarmDef.setFaultCode(a.getTriplet().getFaultCode().intValue());
linkAlarmDef.setFaultFamily(a.getTriplet().getFaultFamily());
linkAlarmDef.setFaultMember(a.getTriplet().getFaultMember());
linkCatDef.setPath(ci.getPath());
cltc.addAlarmCategoryLink(link);
}
}
StringWriter catList = new StringWriter();
try {
acds.marshal(catList);
} catch (Exception e) {
throw new RuntimeException("Failed to encode categories", e);
}
StringWriter linkList = new StringWriter();
try {
acds.marshal(linkList);
} catch (Exception e) {
throw new RuntimeException("Failed to encode link", e);
}
try {
conf.setConfiguration(CATEGORY_DEFINITION_PATH, catList.toString().replaceFirst("xsi:type=\".*\"", ""));
conf.setConfiguration(ALARM_CATEGORY_DEFINITION_PATH, linkList.toString().replaceFirst("xsi:type=\".*\"", ""));
} catch (Exception e) {
throw new RuntimeException("Failed to store configuration", e);
}
}
use of cern.laser.business.data.CategoryImpl in project ACS by ACS-Community.
the class ACSCategoryDAOImpl method getAlarms.
public String[] getAlarms(Integer categoryId) {
Category c = getCategory(categoryId);
if (c == null)
return new String[0];
Set ids = ((CategoryImpl) c).getAlarmIds();
String[] result = new String[ids.size()];
ids.toArray(result);
return result;
}
Aggregations