2 * Copyright (c) 2013 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.netconf.confignetconfconnector.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.yangjmxgenerator.attribute.AttributeIfc;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
26 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
29 public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
30 Map<String, AttributeIfc> configDefinition) {
31 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
33 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
34 strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
40 public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
42 if(attributeIfc instanceof DependencyAttribute) {
43 serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
44 namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
47 return switchAttribute(attributeIfc);
50 private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
51 Map<String, String> retVal = Maps.newHashMap();
52 for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
53 retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
59 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
60 return new SimpleAttributeMappingStrategy(openType);
64 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
66 AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
67 (SimpleType<?>) openType.getElementOpenType());
68 return new ArrayAttributeMappingStrategy(openType, innerStrategy);
72 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
73 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
75 Map<String, String> attributeMapping = Maps.newHashMap();
77 for (String innerAttributeKey : openType.keySet()) {
79 innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
80 attributeMapping.put(innerAttributeKey, innerAttributeKey);
83 return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
87 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(OpenType<?> openType) {
88 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
90 Map<String, String> attributeMapping = Maps.newHashMap();
92 CompositeType compositeType = (CompositeType) openType;
93 for (String innerAttributeKey : compositeType.keySet()) {
95 innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
96 attributeMapping.put(innerAttributeKey, innerAttributeKey);
99 return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
102 private String serviceNameOfDepAttr;
103 private String namespaceOfDepAttr;
106 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
107 SimpleType<?> openType) {
108 return new ObjectNameAttributeMappingStrategy(openType,
109 serviceNameOfDepAttr, namespaceOfDepAttr);
113 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
114 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
116 Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
117 TOAttribute lastTO = (TOAttribute) getLastAttribute();
119 for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)getLastAttribute()).getJmxPropertiesToTypesMap().entrySet()) {
120 innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
123 return new CompositeAttributeMappingStrategy(openType, innerStrategies,
124 createJmxToYangMapping(lastTO));
128 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
129 Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
130 return new ArrayAttributeMappingStrategy(openType,
131 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
135 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
136 Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
137 return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));