BUG 2596: Use base service name in service serialization.
[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 import org.opendaylight.yangtools.yang.common.QName;
18
19 public class ObjectNameAttributeMappingStrategy extends
20         AbstractAttributeMappingStrategy<ObjectNameAttributeMappingStrategy.MappedDependency, SimpleType<?>> {
21
22     private final String namespace;
23
24     public ObjectNameAttributeMappingStrategy(SimpleType<?> openType, String namespace) {
25         super(openType);
26         this.namespace = namespace;
27     }
28
29     @Override
30     public Optional<MappedDependency> mapAttribute(Object value) {
31         if (value == null){
32             return Optional.absent();
33         }
34
35         String expectedClass = getOpenType().getClassName();
36         String realClass = value.getClass().getName();
37         Preconditions.checkArgument(realClass.equals(expectedClass), "Type mismatch, expected " + expectedClass
38                 + " but was " + realClass);
39         Util.checkType(value, ObjectName.class);
40
41         ObjectName on = (ObjectName) value;
42
43         String refName = ObjectNameUtil.getReferenceName(on);
44
45         //we want to use the exact service name that was configured in xml so services that are referencing it can be resolved
46         return Optional.of(new MappedDependency(namespace,
47                 QName.create(ObjectNameUtil.getServiceQName(on)).getLocalName(), refName));
48     }
49
50     public static class MappedDependency {
51         private final String namespace, serviceName, refName;
52
53         public MappedDependency(String namespace, String serviceName, String refName) {
54             this.serviceName = serviceName;
55             this.refName = refName;
56             this.namespace = namespace;
57         }
58
59         public String getServiceName() {
60             return serviceName;
61         }
62
63         public String getRefName() {
64             return refName;
65         }
66
67         public String getNamespace() {
68             return namespace;
69         }
70
71         @Override
72         public String toString() {
73             final StringBuffer sb = new StringBuffer("MappedDependency{");
74             sb.append("namespace='").append(namespace).append('\'');
75             sb.append(", serviceName='").append(serviceName).append('\'');
76             sb.append(", refName='").append(refName).append('\'');
77             sb.append('}');
78             return sb.toString();
79         }
80     }
81
82 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.