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