2 * Copyright (c) 2015, 2017 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
9 package org.opendaylight.controller.config.facade.xml.mapping.config;
11 import com.google.common.base.Preconditions;
12 import java.util.HashMap;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.ObjectName;
16 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
17 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
18 import org.opendaylight.yangtools.yang.common.QName;
20 public class ServiceRegistryWrapper {
22 private final ServiceReferenceReadableRegistry configServiceRefRegistry;
24 public ServiceRegistryWrapper(final ServiceReferenceReadableRegistry configServiceRefRegistry) {
25 this.configServiceRefRegistry = configServiceRefRegistry;
28 public ObjectName getByServiceAndRefName(final String namespace, final String serviceType, final String refName) {
29 Map<String, Map<String, Map<String, String>>> mappedServices = getMappedServices();
30 Map<String, Map<String, String>> serviceNameToRefNameToInstance = mappedServices.get(namespace);
32 Preconditions.checkArgument(serviceNameToRefNameToInstance != null,
33 "No service mapped to %s:%s:%s. Wrong namespace, available namespaces: %s", namespace, serviceType,
34 refName, mappedServices.keySet());
36 Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceType);
37 Preconditions.checkArgument(refNameToInstance != null,
38 "No service mapped to %s:%s:%s. Wrong service type, available service types: %s", namespace,
39 serviceType, refName, serviceNameToRefNameToInstance.keySet());
41 String instanceId = refNameToInstance.get(refName);
42 Preconditions.checkArgument(instanceId != null,
43 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s", namespace, serviceType,
44 refName, refNameToInstance.keySet());
46 Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
47 Preconditions.checkArgument(serviceInstance != null,
48 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s", namespace, serviceType,
49 refName, refNameToInstance.keySet());
51 String serviceName = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceType);
54 * Remove transaction name as this is redundant - will be stripped in
55 * DynamicWritableWrapper, and makes it hard to compare with service references
56 * got from MXBean attributes
59 .withoutTransactionName(configServiceRefRegistry.getServiceReference(serviceName, refName));
60 } catch (final InstanceNotFoundException e) {
61 throw new IllegalArgumentException("No serviceInstance mapped to " + refName + " under service name "
62 + serviceType + " , " + refNameToInstance.keySet(), e);
67 public Map<String, Map<String, Map<String, String>>> getMappedServices() {
68 Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
70 Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
71 for (Map.Entry<String, Map<String, ObjectName>> nameToRefEntry : serviceMapping.entrySet()) {
72 for (String refName : nameToRefEntry.getValue().keySet()) {
74 ObjectName on = nameToRefEntry.getValue().get(refName);
75 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
77 QName qname = QName.create(nameToRefEntry.getKey());
78 String namespace = qname.getNamespace().toString();
79 Map<String, Map<String, String>> serviceToRefs = retVal.computeIfAbsent(namespace,
80 k -> new HashMap<>());
82 String localName = qname.getLocalName();
83 Map<String, String> refsToSis = serviceToRefs.computeIfAbsent(localName, k -> new HashMap<>());
85 Preconditions.checkState(!refsToSis.containsKey(refName),
86 "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
88 refsToSis.put(refName, si.toString());