af6e3db8e9406827868cfe214ee498bc2ac0c08c
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / resolving / ObjectNameAttributeResolvingStrategy.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.resolving;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
13 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
14 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services.ServiceInstance;
16 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import javax.management.ObjectName;
21 import javax.management.openmbean.SimpleType;
22
23 public class ObjectNameAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<ObjectName, SimpleType<?>> {
24
25     private final Services serviceTracker;
26     private static final Logger logger = LoggerFactory.getLogger(ObjectNameAttributeResolvingStrategy.class);
27
28     ObjectNameAttributeResolvingStrategy(Services serviceTracker) {
29         super(SimpleType.OBJECTNAME);
30         this.serviceTracker = serviceTracker;
31     }
32
33     @Override
34     public Optional<ObjectName> parseAttribute(String attrName, Object value) {
35         if (value == null) {
36             return Optional.absent();
37         }
38
39         Util.checkType(value, ObjectNameAttributeMappingStrategy.MappedDependency.class);
40
41         ObjectNameAttributeMappingStrategy.MappedDependency mappedDep = (ObjectNameAttributeMappingStrategy.MappedDependency) value;
42         String serviceName = mappedDep.getServiceName();
43         if (serviceName.contains(":")) {
44             // hack for yuma
45             serviceName = serviceName.substring(serviceName.indexOf(":") + 1);
46         }
47         String refName = mappedDep.getRefName();
48         logger.trace("Getting service instance by service name {} and ref name {}", serviceName, refName);
49         ServiceInstance byRefName = serviceTracker.getByServiceAndRefName(serviceName, refName);
50         ObjectName on = ObjectNameUtil.createReadOnlyModuleON(byRefName.getModuleName(), byRefName.getInstanceName());
51         logger.debug("Attribute {} : {} parsed to type {}", attrName, value, getOpenType());
52         return Optional.of(on);
53     }
54
55 }