b827a5b03965d7f7471731a646b80e44ee835266
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ObjectNameAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import javax.management.ObjectName;
14 import javax.management.openmbean.SimpleType;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
17
18 public class ObjectNameAttributeMappingStrategy extends
19         AbstractAttributeMappingStrategy<ObjectNameAttributeMappingStrategy.MappedDependency, SimpleType<?>> {
20
21     private final String serviceName;
22     private final String namespace;
23
24     public ObjectNameAttributeMappingStrategy(SimpleType<?> openType,  String serviceName, String namespace) {
25         super(openType);
26         this.serviceName = serviceName;
27         this.namespace = namespace;
28     }
29
30     @Override
31     public Optional<MappedDependency> mapAttribute(Object value) {
32         if (value == null){
33             return Optional.absent();
34         }
35
36         String expectedClass = getOpenType().getClassName();
37         String realClass = value.getClass().getName();
38         Preconditions.checkArgument(realClass.equals(expectedClass), "Type mismatch, expected " + expectedClass
39                 + " but was " + realClass);
40         Util.checkType(value, ObjectName.class);
41
42         ObjectName on = (ObjectName) value;
43
44         String refName = ObjectNameUtil.getReferenceName(on);
45
46         return Optional.of(new MappedDependency(namespace, serviceName, refName));
47     }
48
49     public static class MappedDependency {
50         private final String namespace, serviceName, refName;
51
52         public MappedDependency(String namespace, String serviceName, String refName) {
53             this.serviceName = serviceName;
54             this.refName = refName;
55             this.namespace = namespace;
56         }
57
58         public String getServiceName() {
59             return serviceName;
60         }
61
62         public String getRefName() {
63             return refName;
64         }
65
66         public String getNamespace() {
67             return namespace;
68         }
69
70         @Override
71         public String toString() {
72             final StringBuffer sb = new StringBuffer("MappedDependency{");
73             sb.append("namespace='").append(namespace).append('\'');
74             sb.append(", serviceName='").append(serviceName).append('\'');
75             sb.append(", refName='").append(refName).append('\'');
76             sb.append('}');
77             return sb.toString();
78         }
79     }
80
81 }