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;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
20 import javax.management.openmbean.ArrayType;
21 import javax.management.openmbean.CompositeType;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
25 import java.util.Map.Entry;
27 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
29 private final Services dependencyTracker;
31 public ObjectMapper(Services depTracker) {
32 this.dependencyTracker = depTracker;
35 public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
36 Map<String, AttributeIfc> configDefinition) {
37 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
39 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
40 strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
46 public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
48 if(attributeIfc instanceof DependencyAttribute) {
49 serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
50 namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
53 return switchAttribute(attributeIfc);
56 private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
57 Map<String, String> retVal = Maps.newHashMap();
58 for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
59 retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
65 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
66 return new SimpleAttributeMappingStrategy(openType);
70 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
72 AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
73 (SimpleType<?>) openType.getElementOpenType());
74 return new ArrayAttributeMappingStrategy(openType, innerStrategy);
78 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
79 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
81 Map<String, String> attributeMapping = Maps.newHashMap();
83 for (String innerAttributeKey : openType.keySet()) {
85 innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
86 attributeMapping.put(innerAttributeKey, innerAttributeKey);
89 return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
92 private String serviceNameOfDepAttr;
93 private String namespaceOfDepAttr;
96 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
97 SimpleType<?> openType) {
98 return new ObjectNameAttributeMappingStrategy(openType, dependencyTracker,
99 serviceNameOfDepAttr, namespaceOfDepAttr);
103 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
104 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
106 Preconditions.checkState(lastAttribute instanceof TOAttribute);
107 TOAttribute lastTO = (TOAttribute) lastAttribute;
109 for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)lastAttribute).getJmxPropertiesToTypesMap().entrySet()) {
110 innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
113 return new CompositeAttributeMappingStrategy(openType, innerStrategies,
114 createJmxToYangMapping(lastTO));
118 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
119 Preconditions.checkState(lastAttribute instanceof ListAttribute);
120 return new ArrayAttributeMappingStrategy(openType,
121 prepareStrategy(((ListAttribute) lastAttribute).getInnerAttribute()));