private MXBeanMapping makeTabularMapping(Type objType, boolean sortedMap, Type keyType, Type valueType, MXBeanMappingFactory factory) throws OpenDataException { final String objTypeName = typeName(objType); final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory); final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory); final OpenType<?> keyOpenType = keyMapping.getOpenType(); final OpenType<?> valueOpenType = valueMapping.getOpenType(); final CompositeType rowType = new CompositeType(objTypeName, objTypeName, keyValueArray, keyValueArray, new OpenType<?>[] {keyOpenType, valueOpenType}); final TabularType tabularType = new TabularType(objTypeName, objTypeName, rowType, keyArray); return new TabularMapping(objType, sortedMap, tabularType, keyMapping, valueMapping); }
private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) { if (ot1 instanceof CompositeType) { if (! (ot2 instanceof CompositeType)) return false; if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2)) return false; } else if (ot1 instanceof TabularType) { if (! (ot2 instanceof TabularType)) return false; if (!isTypeMatched((TabularType) ot1, (TabularType) ot2)) return false; } else if (ot1 instanceof ArrayType) { if (! (ot2 instanceof ArrayType)) return false; if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) { return false; } } else if (!ot1.equals(ot2)) { return false; } return true; }
private ArrayType<?> getVersionedArrayType(ArrayType<?> type, String version) throws OpenDataException { if (type.isPrimitiveArray()) { return type; } OpenType<?> ot = getVersionedType( type.getElementOpenType(), version ); if (ot instanceof SimpleType) { return new ArrayType<>((SimpleType<?>)ot, type.isPrimitiveArray()); } else { return new ArrayType<>(type.getDimension(), ot); } }
@Override public OpenType<?> getOpenType() { final TypeDefinition<?> baseTypeDefinition = getBaseType(this.typeProviderWrapper, this.typeDefinition); final Type baseType = this.typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition); if (isArray()) { return getArrayType(); } else if (isEnum()) { return getEnumType(baseTypeDefinition); } else if (isUnion()) { return getCompositeTypeForUnion(baseTypeDefinition); } else if (isDerivedType(baseType, getType())) { return getCompositeType(baseType, baseTypeDefinition); } else if (isIdentityRef()) { return getCompositeTypeForIdentity(); } return getSimpleType(getType()); }
private OpenType<?> getCompositeType(final Type baseType, final TypeDefinition<?> baseTypeDefinition) { final SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType); final String innerItemName = this.typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition); final String[] itemNames = new String[]{innerItemName}; final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); final OpenType<?>[] itemTypes = new OpenType[]{innerItemType}; try { return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); } catch (final OpenDataException e) { throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type " + itemTypes, e); } }
private void resolveConfiguration(final InstanceConfigElementResolved mappedConfig, final ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) { // TODO make field, resolvingStrategies can be instantiated only once Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver( depTracker).prepareResolving(yangToAttrConfig, enumResolver); for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) { AttributeConfigElement value = configDefEntry.getValue(); String attributeName = configDefEntry.getKey(); try { AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies .get(attributeName); LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy); value.resolveValue(attributeResolvingStrategy, attributeName); value.setJmxName(yangToAttrConfig.get(attributeName).getUpperCaseCammelCase()); } catch (final DocumentedException e) { throw new IllegalStateException("Unable to resolve value " + value + " to attribute " + attributeName, e); } } }
@Override public CompositeType getOpenType() { final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); final FunctionImpl functionImpl = new FunctionImpl(); final Map<String, AttributeIfc> jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap(); final OpenType<?>[] itemTypes = Collections2.transform( jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray( new OpenType<?>[] {}); final String[] itemNames = functionImpl.getItemNames(); try { // TODO add package name to create fully qualified name for this // type final CompositeType compositeType = new CompositeType( getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); return compositeType; } catch (final OpenDataException e) { throw new RuntimeException("Unable to create CompositeType for " + this, e); } }
public static OpenType<?> getOpenType(Object o) { if(o instanceof Long) { return SimpleType.LONG; } else if(o instanceof String) { return SimpleType.STRING; } else if(o instanceof Date) { return SimpleType.DATE; } else if(o instanceof Integer) { return SimpleType.INTEGER; } else if(o instanceof Boolean) { return SimpleType.BOOLEAN; } else if(o instanceof Double) { return SimpleType.DOUBLE; } else if(o instanceof Float) { return SimpleType.FLOAT; } else { throw new IllegalArgumentException(); } }
@Override protected AttributeReadingStrategy caseJavaIdentityRefAttribute(final OpenType<?> openType) { Preconditions.checkState(openType instanceof CompositeType); Set<String> keys = ((CompositeType) openType).keySet(); Preconditions.checkState(keys.size() == 1, "Unexpected number of elements for open type %s, should be 1", openType); String mappingKey = keys.iterator().next(); return new SimpleIdentityRefAttributeReadingStrategy(getLastAttribute().getNullableDefault(), mappingKey, identityMap); }
static synchronized OpenType[] getBaseGcInfoItemTypes() { if (baseGcInfoItemTypes == null) { OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType(); baseGcInfoItemTypes = new OpenType<?>[] { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, memoryUsageOpenType, memoryUsageOpenType, }; } return baseGcInfoItemTypes; }
/** * Compares two CompositeTypes and returns true if * all items in type1 exist in type2 and their item types * are the same. */ protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) { if (type1 == type2) return true; // We can't use CompositeType.isValue() since it returns false // if the type name doesn't match. Set<String> allItems = type1.keySet(); // Check all items in the type1 exist in type2 if (!type2.keySet().containsAll(allItems)) return false; for (String item: allItems) { OpenType<?> ot1 = type1.getType(item); OpenType<?> ot2 = type2.getType(item); if (ot1 instanceof CompositeType) { if (! (ot2 instanceof CompositeType)) return false; if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2)) return false; } else if (ot1 instanceof TabularType) { if (! (ot2 instanceof TabularType)) return false; if (!isTypeMatched((TabularType) ot1, (TabularType) ot2)) return false; } else if (!ot1.equals(ot2)) { return false; } } return true; }
private static Descriptor typeDescriptor(OpenType<?> openType, Type originalType) { return new ImmutableDescriptor( new String[] {"openType", "originalType"}, new Object[] {openType, originalTypeString(originalType)}); }
private MXBeanMapping makeArrayOrCollectionMapping(Type collectionType, Type elementType, MXBeanMappingFactory factory) throws OpenDataException { final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory); final OpenType<?> elementOpenType = elementMapping.getOpenType(); final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType); final Class<?> elementOpenClass = elementMapping.getOpenClass(); final Class<?> openArrayClass; final String openArrayClassName; if (elementOpenClass.isArray()) openArrayClassName = "[" + elementOpenClass.getName(); else openArrayClassName = "[L" + elementOpenClass.getName() + ";"; try { openArrayClass = Class.forName(openArrayClassName); } catch (ClassNotFoundException e) { throw openDataException("Cannot obtain array class", e); } if (collectionType instanceof ParameterizedType) { return new CollectionMapping(collectionType, openType, openArrayClass, elementMapping); } else { if (isIdentity(elementMapping)) { return new IdentityMapping(collectionType, openType); } else { return new ArrayMapping(collectionType, openType, openArrayClass, elementMapping); } } }
/** * Will call all getter methods on payload that are defined in the given interfaces */ public static Map makeCallerChain ( Object payload, Class... ifaces ) throws OpenDataException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException { CompositeType rt = new CompositeType("a", "b", new String[] { "a" }, new String[] { "a" }, new OpenType[] { javax.management.openmbean.SimpleType.INTEGER }); TabularType tt = new TabularType("a", "b", rt, new String[] { "a" }); TabularDataSupport t1 = new TabularDataSupport(tt); TabularDataSupport t2 = new TabularDataSupport(tt); // we need to make payload implement composite data // it's very likely that there are other proxy impls that could be used AdvisedSupport as = new AdvisedSupport(); as.setTarget(payload); InvocationHandler delegateInvocationHandler = (InvocationHandler) Reflections .getFirstCtor("org.springframework.aop.framework.JdkDynamicAopProxy").newInstance(as); InvocationHandler cdsInvocationHandler = Gadgets.createMemoizedInvocationHandler(Gadgets.createMap("getCompositeType", rt)); CompositeInvocationHandlerImpl invocationHandler = new CompositeInvocationHandlerImpl(); invocationHandler.addInvocationHandler(CompositeData.class, cdsInvocationHandler); invocationHandler.setDefaultHandler(delegateInvocationHandler); final CompositeData cdsProxy = Gadgets.createProxy(invocationHandler, CompositeData.class, ifaces); JSONObject jo = new JSONObject(); Map m = new HashMap(); m.put("t", cdsProxy); Reflections.setFieldValue(jo, "properties", m); Reflections.setFieldValue(jo, "properties", m); Reflections.setFieldValue(t1, "dataMap", jo); Reflections.setFieldValue(t2, "dataMap", jo); return Gadgets.makeMap(t1, t2); }
private static OpenTypeConverter makeTabularConverter(Type objType, boolean sortedMap, Type keyType, Type valueType) throws OpenDataException { final String objTypeName = objType.toString(); final OpenTypeConverter keyConverter = toConverter(keyType); final OpenTypeConverter valueConverter = toConverter(valueType); final OpenType keyOpenType = keyConverter.getOpenType(); final OpenType valueOpenType = valueConverter.getOpenType(); final CompositeType rowType = new CompositeType(objTypeName, objTypeName, keyValueArray, keyValueArray, new OpenType[] {keyOpenType, valueOpenType}); final TabularType tabularType = new TabularType(objTypeName, objTypeName, rowType, keyArray); return new TableConverter(objType, sortedMap, tabularType, keyConverter, valueConverter); }
public Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> prepareResolving( final Map<String, AttributeIfc> configDefinition, final EnumResolver enumResolver) { this.enumResolver = enumResolver; Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap(); for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) { strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue())); } return strategies; }
public CompositeType generateCompositeType() { try { int nNbItems = m_arrItemDesc.size(); OpenType [] openTypes = new OpenType [nNbItems]; String [] itemTypeNames = new String [nNbItems]; String [] itemTypeDescriptions = new String [nNbItems]; for(int n=0; n<nNbItems; n++) { CompositeTypeDescItem itemDesc = m_arrItemDesc.get(n); openTypes[n] = itemDesc.m_openType; itemTypeNames[n] = itemDesc.m_csName; itemTypeDescriptions[n] = itemDesc.m_csDescription; } CompositeType compositeType = new CompositeType( m_csName, m_csDescription, itemTypeNames, itemTypeDescriptions, openTypes); return compositeType; } catch (OpenDataException e) { e.printStackTrace(); } return null; }
private static Class<?> makeOpenClass(Type javaType, OpenType<?> openType) { if (javaType instanceof Class<?> && ((Class<?>) javaType).isPrimitive()) return (Class<?>) javaType; try { String className = openType.getClassName(); return Class.forName(className, false, MXBeanMapping.class.getClassLoader()); } catch (ClassNotFoundException e) { throw new RuntimeException(e); // should not happen } }
static CompositeType make(String className, String description, String[] itemNames, String[] itemDescriptions, OpenType[] itemTypes) { try { return new CompositeType(className, description, itemNames, itemDescriptions, itemTypes); } catch (OpenDataException e) { throw new Error(e); } }
public CompositeAttributeMappingStrategy(final CompositeType compositeType, final Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies, final Map<String, String> jmxToJavaNameMapping) { super(compositeType); this.innerStrategies = innerStrategies; this.jmxToJavaNameMapping = jmxToJavaNameMapping; }
static synchronized OpenType<?>[] getBaseGcInfoItemTypes() { if (baseGcInfoItemTypes == null) { OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType(); baseGcInfoItemTypes = new OpenType<?>[] { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, memoryUsageOpenType, memoryUsageOpenType, }; } return baseGcInfoItemTypes; }
/** * Retrieves the specified version of a {@linkplain CompositeType} instance. * @param type The current (latest) version of {@linkplain CompositeType} * @param version The version identifier (eg. {@linkplain TypeVersionMapper#V5}) * @return Returns the {@linkplain CompositeType} corresponding to the requested * version. * @throws OpenDataException */ CompositeType getVersionedCompositeType(CompositeType type, String version) throws OpenDataException { Predicate<String> filter = getFilter(type.getTypeName(), version); if (filter == null) { return type; } List<String> itemNames = new ArrayList<>(); List<String> itemDesc = new ArrayList<>(); List<OpenType<?>> itemTypes = new ArrayList<>(); for(String item : type.keySet()) { if (filter.test(item)) { itemNames.add(item); itemDesc.add(type.getDescription(item)); itemTypes.add(getVersionedType( type.getType(item), version )); } } return new CompositeType( type.getTypeName(), version != null ? version + " " + type.getDescription() : type.getDescription(), itemNames.toArray(new String[itemNames.size()]), itemDesc.toArray(new String[itemDesc.size()]), itemTypes.toArray(new OpenType<?>[itemTypes.size()]) ); }
private OpenType<?> getVersionedType(OpenType<?> type, String version) throws OpenDataException { if (type instanceof ArrayType) { return getVersionedArrayType((ArrayType)type, version); } if (type instanceof CompositeType) { return getVersionedCompositeType((CompositeType)type, version); } if (type instanceof TabularType) { return getVersionedTabularType((TabularType)type, version); } return type; }
CompositeAttributeResolvingStrategy( final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes, final CompositeType openType, final Map<String, String> yangToJavaAttrMapping) { super(openType); this.innerTypes = innerTypes; this.yangToJavaAttrMapping = yangToJavaAttrMapping; }
static ArrayType make(int dims, OpenType baseType) { try { return new ArrayType(dims, baseType); } catch (OpenDataException e) { throw new Error(e); } }
private OpenType<?> getEnumType(final TypeDefinition<?> baseType) { final String fullyQualifiedName = this.typeProviderWrapper.getType(this.node, getTypeDefinition()).getFullyQualifiedName(); final String[] items = {"instance"}; final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); try { return new CompositeType(fullyQualifiedName, description, items, items, new OpenType[]{SimpleType.STRING}); } catch (final OpenDataException e) { throw new RuntimeException("Unable to create enum type" + fullyQualifiedName + " as open type", e); } }
protected Optional<?> mapInnerAttribute(final CompositeDataSupport compositeData, final String jmxName, final String description) { Object innerValue = compositeData.get(jmxName); AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = innerStrategies.get(jmxName); return attributeMappingStrategy.mapAttribute(innerValue); }
public OpenType<?> getCompositeTypeForIdentity() { final String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME}; final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); final OpenType<?>[] itemTypes = new OpenType[]{SimpleType.STRING}; try { return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); } catch (final OpenDataException e) { throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type " + itemTypes, e); } }
private OpenType<?> getArrayOpenTypeForSimpleType(final String innerTypeFullyQName, final SimpleType<?> innerSimpleType) { try { final ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true) : new ArrayType<>(1, innerSimpleType); return arrayType; } catch (final OpenDataException e) { throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type " + innerSimpleType, e); } }
@SuppressWarnings("IllegalCatch") private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) { // TODO make field, mappingStrategies can be instantiated only once Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper() .prepareMapping(jmxToAttrConfig, enumResolver); Map<String, Object> toXml = Maps.newHashMap(); for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) { // Skip children runtime beans as they are mapped by InstanceRuntime if (configDefEntry.getValue() instanceof RuntimeBeanEntry) { continue; } Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey()); try { AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies .get(configDefEntry.getKey()); Optional<?> attribute = attributeMappingStrategy.mapAttribute(value); if (!attribute.isPresent()) { continue; } toXml.put(configDefEntry.getValue().getAttributeYangName(), attribute.get()); } catch (final RuntimeException e) { throw new IllegalStateException("Unable to map value " + value + " to attribute " + configDefEntry.getKey(), e); } } return toXml; }