2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
14 import java.util.Map.Entry;
15 import javax.management.openmbean.ArrayType;
16 import javax.management.openmbean.CompositeType;
17 import javax.management.openmbean.OpenType;
18 import javax.management.openmbean.SimpleType;
19 import org.opendaylight.controller.config.facade.xml.mapping.attributes.AttributeIfcSwitchStatement;
20 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
27 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
29 private EnumResolver enumResolver;
31 @SuppressWarnings("checkstyle:hiddenField")
32 public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
33 final Map<String, AttributeIfc> configDefinition, final EnumResolver enumResolver) {
34 this.enumResolver = Preconditions.checkNotNull(enumResolver);
35 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
37 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
38 strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
43 public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(final AttributeIfc attributeIfc) {
45 if (attributeIfc instanceof DependencyAttribute) {
46 namespaceOfDepAttr = ((DependencyAttribute) attributeIfc).getDependency().getSie().getQName().getNamespace()
48 } else if (attributeIfc instanceof ListDependenciesAttribute) {
49 namespaceOfDepAttr = ((ListDependenciesAttribute) attributeIfc).getDependency().getSie().getQName()
50 .getNamespace().toString();
52 return switchAttribute(attributeIfc);
55 private Map<String, String> createJmxToYangMapping(final TOAttribute attributeIfc) {
56 Map<String, String> retVal = Maps.newHashMap();
57 for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
58 retVal.put(entry.getKey(), entry.getValue().getAttributeYangName());
64 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(final SimpleType<?> openType) {
65 return new SimpleAttributeMappingStrategy(openType);
69 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
70 return new EnumAttributeMappingStrategy((CompositeType) openType, enumResolver);
74 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(final ArrayType<?> openType) {
76 AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
77 (SimpleType<?>) openType.getElementOpenType());
78 return new ArrayAttributeMappingStrategy(openType, innerStrategy);
82 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(
83 final CompositeType openType) {
84 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
86 Map<String, String> attributeMapping = Maps.newHashMap();
88 for (String innerAttributeKey : openType.keySet()) {
90 innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
91 attributeMapping.put(innerAttributeKey, innerAttributeKey);
93 return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
97 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) {
98 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
100 Map<String, String> attributeMapping = Maps.newHashMap();
102 CompositeType compositeType = (CompositeType) openType;
103 for (String innerAttributeKey : compositeType.keySet()) {
105 innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
106 attributeMapping.put(innerAttributeKey, innerAttributeKey);
108 return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
111 private String namespaceOfDepAttr;
114 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(final SimpleType<?> openType) {
115 return new ObjectNameAttributeMappingStrategy(openType, namespaceOfDepAttr);
119 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) {
120 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
122 Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
123 TOAttribute lastTO = (TOAttribute) getLastAttribute();
125 for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute) getLastAttribute())
126 .getJmxPropertiesToTypesMap().entrySet()) {
127 innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
130 return new CompositeAttributeMappingStrategy(openType, innerStrategies, createJmxToYangMapping(lastTO));
134 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(final ArrayType<?> openType) {
135 Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
136 return new ArrayAttributeMappingStrategy(openType,
137 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
141 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(
142 final ArrayType<?> openType) {
143 Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
144 return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));