use of com.emc.storageos.db.client.model.Name in project coprhd-controller by CoprHD.
the class DataObjectChangeAnalyzer method lookForChanges.
/**
* Scans the methods looking for ones annotated with the Name annotation.
* When found (if not excluded), invokes the method on each of the DataObjects
* and then compares the results.
*
* @param left
* @param right
* @param changes
* @param included -- If not null, only fields in included are checked.
* @param excluded -- Fields that are excluded are not checked. Must not be null.
* @param contained -- If not null, values for fields in contained are checked.
*/
private static void lookForChanges(DataObject left, DataObject right, HashMap<String, Change> changes, Set<String> included, Set<String> excluded, Set<String> contained) {
Class refClass = left.getClass();
Method[] methods = refClass.getMethods();
for (Method method : methods) {
boolean contain = false;
// We only analyze methods that have the "Name" annotation
Name nameAnn = method.getAnnotation(Name.class);
if (nameAnn == null) {
continue;
}
String key = nameAnn.value();
// If contained is not null and it contains the key set contain flag to true
if (contained != null && contained.contains(key)) {
contain = true;
} else // If included is not null, and does not contain the name, exclude it.
if (included != null && !included.contains(key)) {
continue;
}
// Skip any excluded annotation names
if (excluded.contains(key)) {
continue;
}
Class type = method.getReturnType();
try {
Object obja = method.invoke(left);
Object objb = method.invoke(right);
if (type == StringSet.class) {
if (contain) {
analyzeNewStringSetContainsOldStringSetValues((StringSet) obja, (StringSet) objb, key, changes);
} else {
analyzeStringSets((StringSet) obja, (StringSet) objb, key, changes);
}
} else if (type == StringMap.class) {
analyzeStringMaps((StringMap) obja, (StringMap) objb, key, changes);
} else if (type == StringSetMap.class) {
analyzeStringSetMaps((StringSetMap) obja, (StringSetMap) objb, key, changes);
} else {
if (!isEqual(obja, objb)) {
Change change = new Change(key, obja, objb, nameAnn.value());
changes.put(key, change);
}
}
} catch (IllegalAccessException ex) {
throw new ServiceCodeException(ServiceCode.UNFORSEEN_ERROR, ex, ex.getMessage(), new String[] {});
} catch (InvocationTargetException ex) {
throw new ServiceCodeException(ServiceCode.UNFORSEEN_ERROR, ex, ex.getMessage(), new String[] {});
}
}
}
use of com.emc.storageos.db.client.model.Name in project coprhd-controller by CoprHD.
the class DbCli method readXMLAndPersist.
/**
* Load xml file and save model object into Cassandra.
*
* @Param fileName
*/
private <T extends DataObject> void readXMLAndPersist(String fileName, DbCliOperation operation) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(fileName);
// Read root node
Element root = doc.getDocumentElement();
Element dataObjectNode = (Element) root.getElementsByTagName("data_object_schema").item(0);
// Get column family's name
String cfName = dataObjectNode.getAttribute("name");
System.out.println("Column Family based on XML: " + cfName);
NodeList recordNodes = dataObjectNode.getElementsByTagName("record");
Class<T> clazz = _cfMap.get(cfName);
if (clazz == null) {
System.out.println("Unknown Column Family: " + cfName);
return;
}
// Get class info
BeanInfo bInfo;
try {
bInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException ex) {
throw new RuntimeException("Unexpected exception getting bean info", ex);
}
PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
// get position of xml node
InputStream xmlIs = new FileInputStream(new File(fileName));
Document docForPosition = PositionalXMLReader.readXML(xmlIs);
xmlIs.close();
for (int i = 0; i < recordNodes.getLength(); i++) {
Element record = (Element) recordNodes.item(i);
T object = null;
String idStr = null;
if (operation == DbCliOperation.LOAD) {
// query record based id
String recordId = record.getAttribute("id");
System.out.println(String.format("Object id:\t%s", recordId));
idStr = recordId;
object = queryObject(URI.create(recordId), clazz);
} else if (operation == DbCliOperation.CREATE) {
// create new id for create record
URI id = URIUtil.createId(clazz);
object = clazz.newInstance();
object.setId(id);
System.out.println(String.format("Create new data object id:\t%s", object.getId()));
idStr = object.getId().toString();
}
HashMap<String, String> fieldValueMap = new HashMap<String, String>();
HashMap<String, Class> fieldTypeMap = new HashMap<String, Class>();
HashMap<String, String> fieldLocationMap = new HashMap<String, String>();
HashMap<String, Node> fieldNodeMap = new HashMap<String, Node>();
NodeList fields = record.getElementsByTagName("field");
// get field info from xml file
for (int j = 0; j < fields.getLength(); j++) {
Element field = (Element) fields.item(j);
if (DEBUG) {
System.out.println(field.getAttribute("name") + "\t" + field.getAttribute("type") + "\t" + field.getAttribute("value"));
}
fieldValueMap.put(field.getAttribute("name"), field.getAttribute("value"));
fieldTypeMap.put(field.getAttribute("name"), Class.forName(field.getAttribute("type")));
fieldLocationMap.put(field.getAttribute("name"), ((Element) docForPosition.getElementsByTagName("record").item(i)).getElementsByTagName("field").item(j).getUserData("lineNumber").toString());
if (field.getElementsByTagName("wrapper").item(0) != null) {
fieldNodeMap.put(field.getAttribute("name"), field.getElementsByTagName("wrapper").item(0));
}
}
Iterator locationIt = fieldLocationMap.entrySet().iterator();
while (locationIt.hasNext()) {
Entry entry = (Entry) locationIt.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (DEBUG) {
System.out.println("key:\t" + key + "\tvalue\t" + value);
}
}
// update object's fields
for (PropertyDescriptor pd : pds) {
// skip class property, id property
if (pd.getName().equals("class") || pd.getName().equals("id")) {
continue;
}
Name name = pd.getReadMethod().getAnnotation(Name.class);
if (name == null) {
log.info("Ignore data object fields without @Name annotation, fieldName={}.", pd.getName());
continue;
}
String objKey = name.value();
String fieldValue = fieldValueMap.get(objKey);
if (fieldValue == null) {
// To support xml file that the old version dumped, it used method name not @Name value
objKey = pd.getName();
}
fieldValue = fieldValueMap.get(objKey);
Class fieldClass = fieldTypeMap.get(objKey);
String fieldLocation = fieldLocationMap.get(objKey);
Node fieldNode = fieldNodeMap.get(objKey);
if (fieldValue != null) {
Class type = pd.getPropertyType();
if (DEBUG) {
System.out.print("\t" + objKey + " = " + type);
}
try {
if (type == URI.class) {
pd.getWriteMethod().invoke(object, URI.create(fieldValue));
} else if (type == NamedURI.class) {
pd.getWriteMethod().invoke(object, NamedURI.fromString(fieldValue));
} else if (type == Date.class) {
// Can not find records with value which owns this type. Remains to be verified correct or not.
// System.out.println("\ttype: Date ");
} else if (type == Calendar.class) {
Calendar calendar = FieldType.toCalendar(fieldValue);
if (!verifyField(calendar)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, calendar);
} else if (type == StringMap.class) {
StringMap newStringMap = FieldType.convertType(fieldNode, StringMapWrapper.class);
if (!verifyField(newStringMap)) {
throw new Exception("field format exception");
}
StringMap sMap = (StringMap) pd.getReadMethod().invoke(object);
if (sMap == null) {
sMap = new StringMap();
}
sMap.clear();
Set<String> keys = newStringMap.keySet();
for (String key : keys) {
sMap.put(key, newStringMap.get(key));
}
pd.getWriteMethod().invoke(object, sMap);
} else if (type == StringSet.class) {
StringSet stringSet = FieldType.convertType(fieldNode, StringSetWrapper.class);
if (!verifyField(stringSet)) {
throw new Exception("field format exception");
}
StringSet updateSet = (StringSet) pd.getReadMethod().invoke(object);
if (updateSet != null) {
updateSet.clear();
updateSet.addAll(stringSet);
} else {
pd.getWriteMethod().invoke(object, stringSet);
}
} else if (type == OpStatusMap.class) {
OpStatusMap opStatusMap = FieldType.convertType(fieldNode, OpStatusMapWrapper.class);
if (!verifyField(opStatusMap)) {
throw new Exception("field format exception");
}
} else if (type == StringSetMap.class) {
StringSetMap newSetMap = FieldType.convertType(fieldNode, StringSetMapWrapper.class);
if (!verifyField(newSetMap)) {
throw new Exception("field format exception");
}
StringSetMap sMap = (StringSetMap) pd.getReadMethod().invoke(object);
if (sMap == null) {
sMap = new StringSetMap();
}
Set<String> keys = sMap.keySet();
for (String key : keys) {
sMap.remove(key);
}
keys = newSetMap.keySet();
for (String key : keys) {
sMap.put(key, newSetMap.get(key));
}
} else if (type == FSExportMap.class) {
FSExportMap fSExportMap = FieldType.convertType(fieldNode, FSExportMapWrapper.class);
if (!verifyField(fSExportMap)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, fSExportMap);
} else if (type == SMBShareMap.class) {
SMBShareMap sMBShareMap = FieldType.convertType(fieldNode, SMBShareMapWrapper.class);
if (!verifyField(sMBShareMap)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, sMBShareMap);
} else if (type == ScopedLabelSet.class) {
ScopedLabelSet scopedLabelSet = FieldType.convertType(fieldNode, ScopedLabelSetWrapper.class);
if (!verifyField(scopedLabelSet)) {
throw new Exception("field format exception");
}
ScopedLabelSet updateSet = (ScopedLabelSet) pd.getReadMethod().invoke(object);
if (updateSet != null) {
updateSet.clear();
updateSet.addAll(scopedLabelSet);
} else {
pd.getWriteMethod().invoke(object, scopedLabelSet);
}
} else if (type == String.class) {
pd.getWriteMethod().invoke(object, fieldClass.cast(fieldValue));
} else if (type.isEnum()) {
Object enumTypeObject = null;
try {
enumTypeObject = Enum.valueOf(type, fieldValue);
} catch (Exception e) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, enumTypeObject);
} else if (type == Integer.class) {
Integer intNum = FieldType.toInteger(fieldValue);
if (!verifyField(intNum)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, intNum);
} else if (type == Boolean.class) {
Boolean boolVal = FieldType.toBoolean(fieldValue);
if (!verifyField(boolVal)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, boolVal);
} else if (type == Long.class) {
Long longNum = FieldType.toLong(fieldValue);
if (!verifyField(longNum)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, longNum);
} else if (type == Short.class) {
Short shortNum = FieldType.toShort(fieldValue);
if (!verifyField(shortNum)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, shortNum);
} else if (type == Double.class) {
Double doubleNum = FieldType.toDouble(fieldValue);
if (!verifyField(doubleNum)) {
throw new Exception("field format exception");
}
pd.getWriteMethod().invoke(object, doubleNum);
} else {
pd.getWriteMethod().invoke(object, fieldValue);
}
} catch (Exception e) {
System.out.println(String.format("Exception in getting field:%s in xml file line:%s.", pd.getName(), fieldLocation));
log.error("Exception in getting field value in xml file line:{}.", fieldLocation, e);
throw new Exception(String.format("Exception in getting field value in line:%s.", fieldLocation));
}
if (DEBUG) {
Object fieldValue1 = pd.getReadMethod().invoke(object);
System.out.println("write " + fieldValue1 + "\ttype: " + type + " success");
}
}
}
if (operation == DbCliOperation.CREATE) {
// Save model object.
_dbClient.createObject(object);
} else if (operation == DbCliOperation.LOAD) {
_dbClient.persistObject(object);
}
log.info(String.format("Successfully update Column family:%s, \tdata object id:%s \tinto Cassandra, based on xml file %s", cfName, idStr, fileName));
}
}
use of com.emc.storageos.db.client.model.Name in project coprhd-controller by CoprHD.
the class DbCli method dumpBeanProperties.
/**
* Dump the contents in xml format
*
* @param pds
* @param object
* @throws Exception
*/
private <T extends DataObject> void dumpBeanProperties(PropertyDescriptor[] pds, T object) throws Exception {
Element record = doc.createElement("record");
record.setAttribute("id", object.getId().toString());
schemaNode.appendChild(record);
// Add readOnlyField node.
Element readOnlyElement = doc.createElement("readOnlyField");
record.appendChild(readOnlyElement);
System.out.println("id: " + object.getId().toString());
Object objValue;
Class type;
for (PropertyDescriptor pd : pds) {
objValue = pd.getReadMethod().invoke(object);
if (objValue == null) {
continue;
}
// Skip password property.
if (pd.getName().toLowerCase().matches("[a-zA-Z\\d]*password[a-zA-Z\\d]*")) {
continue;
}
// Skip some properties.
if (pd.getName().equals("class") || pd.getName().equals("id")) {
Element readOnlyfieldNode = doc.createElement("field");
// delete the prefix string "class "
readOnlyfieldNode.setAttribute("type", pd.getPropertyType().toString().substring(6));
readOnlyfieldNode.setAttribute("name", pd.getName().toString());
readOnlyfieldNode.setAttribute("value", objValue.toString());
readOnlyElement.appendChild(readOnlyfieldNode);
continue;
}
// Skip the fields without @Name annotation
Name name = pd.getReadMethod().getAnnotation(Name.class);
if (name == null) {
log.info("Ignore data object fields without @Name annotation, fieldName={}.", pd.getName());
continue;
}
// use value from @Name instead of mtehod name
String objKey = name.value();
type = pd.getPropertyType();
if (DEBUG) {
System.out.print("\t" + pd.getPropertyType() + "\t" + objKey + " = ");
}
Element fieldNode = doc.createElement("field");
// delete the prefix string "class "
fieldNode.setAttribute("type", type.toString().substring(6));
fieldNode.setAttribute("name", objKey);
if (type == StringSetMap.class) {
StringSetMap stringSetMap = (StringSetMap) objValue;
FieldType.marshall(stringSetMap, fieldNode, StringSetMapWrapper.class);
} else if (type == StringSet.class) {
StringSet stringSet = (StringSet) objValue;
FieldType.marshall(stringSet, fieldNode, StringSetWrapper.class);
} else if (type == ScopedLabelSet.class) {
ScopedLabelSet scopedLabelSet = (ScopedLabelSet) objValue;
FieldType.marshall(scopedLabelSet, fieldNode, ScopedLabelSetWrapper.class);
} else if (type == OpStatusMap.class) {
OpStatusMap opStatusMap = (OpStatusMap) objValue;
FieldType.marshall(opStatusMap, fieldNode, OpStatusMapWrapper.class);
} else if (type == StringMap.class) {
StringMap stringMap = (StringMap) objValue;
FieldType.marshall(stringMap, fieldNode, StringMapWrapper.class);
} else if (type == FSExportMap.class) {
FSExportMap fSExportMap = (FSExportMap) objValue;
FieldType.marshall(fSExportMap, fieldNode, FSExportMapWrapper.class);
} else if (type == SMBShareMap.class) {
SMBShareMap sMBShareMap = (SMBShareMap) objValue;
FieldType.marshall(sMBShareMap, fieldNode, SMBShareMapWrapper.class);
} else {
fieldNode.setAttribute("value", objValue.toString());
}
record.appendChild(fieldNode);
}
}
use of com.emc.storageos.db.client.model.Name in project coprhd-controller by CoprHD.
the class DbCli method printBeanProperties.
/**
* Print the contents in human readable format
*
* @param pds
* @param object
* @throws Exception
*/
private <T extends DataObject> void printBeanProperties(PropertyDescriptor[] pds, T object) throws Exception {
System.out.println("id: " + object.getId().toString());
Object objValue;
Class type;
for (PropertyDescriptor pd : pds) {
// skip class property
if (pd.getName().equals("class") || pd.getName().equals("id")) {
continue;
}
Name nameAnnotation = pd.getReadMethod().getAnnotation(Name.class);
String objKey;
if (nameAnnotation == null) {
objKey = pd.getName();
} else {
objKey = nameAnnotation.value();
}
objValue = pd.getReadMethod().invoke(object);
if (objValue == null) {
continue;
}
System.out.print("\t" + objKey + " = ");
Encrypt encryptAnnotation = pd.getReadMethod().getAnnotation(Encrypt.class);
if (encryptAnnotation != null) {
System.out.println("*** ENCRYPTED CONTENT ***");
continue;
}
type = pd.getPropertyType();
if (type == URI.class) {
System.out.println("URI: " + objValue);
} else if (type == StringMap.class) {
System.out.println("StringMap " + objValue);
} else if (type == StringSet.class) {
System.out.println("StringSet " + objValue);
} else if (type == StringSetMap.class) {
System.out.println("StringSetMap " + objValue);
} else if (type == OpStatusMap.class) {
System.out.println("OpStatusMap " + objValue);
} else {
System.out.println(objValue);
}
}
System.out.println();
}
use of com.emc.storageos.db.client.model.Name in project coprhd-controller by CoprHD.
the class DBClient method printBeanProperties.
/**
* @param clazz
* @param object
* @param criterias
* Filter with some verify simple criteria
* @return Whether this record is print out
* @throws Exception
*/
private <T extends DataObject> boolean printBeanProperties(Class<T> clazz, T object, Map<String, String> criterias) throws Exception {
Map<String, String> localCriterias = new HashMap<>(criterias);
StringBuilder record = new StringBuilder();
record.append("id: " + object.getId().toString() + "\n");
boolean isPrint = true;
BeanInfo bInfo;
try {
bInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException ex) {
log.error("Unexpected exception getting bean info", ex);
throw new RuntimeException("Unexpected exception getting bean info", ex);
}
PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
Object objValue;
Class type;
Set<String> ignoreList = new HashSet<>();
for (PropertyDescriptor pd : pds) {
// skip class property
if (pd.getName().equals("class") || pd.getName().equals("id")) {
continue;
}
Name nameAnnotation = pd.getReadMethod().getAnnotation(Name.class);
String objKey;
if (nameAnnotation == null) {
objKey = pd.getName();
} else {
objKey = nameAnnotation.value();
}
objValue = pd.getReadMethod().invoke(object);
if (!localCriterias.isEmpty()) {
if (localCriterias.containsKey(objKey)) {
if (!localCriterias.get(objKey).equalsIgnoreCase(String.valueOf(objValue))) {
isPrint = false;
break;
} else {
localCriterias.remove(objKey);
}
}
}
if (objValue == null) {
ignoreList.add(objKey);
continue;
}
if (isEmptyStr(objValue)) {
ignoreList.add(objKey);
continue;
}
record.append("\t" + objKey + " = ");
Encrypt encryptAnnotation = pd.getReadMethod().getAnnotation(Encrypt.class);
if (encryptAnnotation != null) {
record.append("*** ENCRYPTED CONTENT ***\n");
continue;
}
type = pd.getPropertyType();
if (type == URI.class) {
record.append("URI: " + objValue + "\n");
} else if (type == StringMap.class) {
record.append("StringMap " + objValue + "\n");
} else if (type == StringSet.class) {
record.append("StringSet " + objValue + "\n");
} else if (type == OpStatusMap.class) {
record.append("OpStatusMap " + objValue + "\n");
} else {
record.append(objValue + "\n");
}
}
if (this.showModificationTime) {
Column<CompositeColumnName> latestField = _dbClient.getLatestModifiedField(TypeMap.getDoType(clazz), object.getId(), ignoreList);
if (latestField != null) {
record.append(String.format("The latest modified time is %s on Field(%s).\n", new Date(latestField.getTimestamp() / 1000), latestField.getName().getOne()));
}
}
if (isPrint) {
if (!localCriterias.isEmpty()) {
String errMsg = String.format("The filters %s are not available for the CF %s", localCriterias.keySet(), clazz);
throw new IllegalArgumentException(errMsg);
}
System.out.println(record.toString());
}
return isPrint;
}
Aggregations