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
8 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
13 import javax.management.InstanceNotFoundException;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
16 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import org.opendaylight.yangtools.yang.common.QName;
19 public class ServiceRegistryWrapper {
21 private final ServiceReferenceReadableRegistry configServiceRefRegistry;
23 public ServiceRegistryWrapper(ServiceReferenceReadableRegistry configServiceRefRegistry) {
24 this.configServiceRefRegistry = configServiceRefRegistry;
27 public ObjectName getByServiceAndRefName(String namespace, String serviceName, String refName) {
28 Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
30 Preconditions.checkNotNull(serviceNameToRefNameToInstance, "No serviceInstances mapped to " + namespace);
32 Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
33 Preconditions.checkNotNull(refNameToInstance, "No serviceInstances mapped to " + serviceName + " , "
34 + serviceNameToRefNameToInstance.keySet());
36 String instanceId = refNameToInstance.get(refName);
37 Preconditions.checkArgument(instanceId != null, "No serviceInstances mapped to " + serviceName + ":"
38 + refName + ", " + serviceNameToRefNameToInstance.keySet());
40 Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
41 Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
42 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
44 String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceName);
47 Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
48 and makes it hard to compare with service references got from MXBean attributes
50 return ObjectNameUtil.withoutTransactionName(
51 configServiceRefRegistry.getServiceReference(qNameOfService, refName));
52 } catch (InstanceNotFoundException e) {
53 throw new IllegalArgumentException("No serviceInstance mapped to " + refName
54 + " under service name " + serviceName + " , " + refNameToInstance.keySet(), e);
59 public Map<String, Map<String, Map<String, String>>> getMappedServices() {
60 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
62 Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
63 for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
64 for (String refName : qNameToRefNameEntry.getValue().keySet()) {
66 ObjectName on = qNameToRefNameEntry.getValue().get(refName);
67 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
69 QName qname = QName.create(qNameToRefNameEntry.getKey());
70 String namespace = qname.getNamespace().toString();
71 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
72 if(serviceToRefs==null) {
73 serviceToRefs = Maps.newHashMap();
74 retVal.put(namespace, serviceToRefs);
77 String localName = qname.getLocalName();
78 Map<String, String> refsToSis = serviceToRefs.get(localName);
80 refsToSis = Maps.newHashMap();
81 serviceToRefs.put(localName, refsToSis);
84 Preconditions.checkState(!refsToSis.containsKey(refName),
85 "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
87 refsToSis.put(refName, si.toString());