Simplify code with new Map features
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / config / ServiceRegistryWrapper.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.mapping.config;
10
11 import com.google.common.base.Preconditions;
12 import java.util.HashMap;
13 import java.util.Map;
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;
19
20 public class ServiceRegistryWrapper {
21
22     private final ServiceReferenceReadableRegistry configServiceRefRegistry;
23
24     public ServiceRegistryWrapper(final ServiceReferenceReadableRegistry configServiceRefRegistry) {
25         this.configServiceRefRegistry = configServiceRefRegistry;
26     }
27
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);
31
32         Preconditions.checkArgument(serviceNameToRefNameToInstance != null,
33                 "No service mapped to %s:%s:%s. Wrong namespace, available namespaces: %s",
34                 namespace, serviceType, refName, mappedServices.keySet());
35
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"
39                 , namespace, serviceType, refName, serviceNameToRefNameToInstance.keySet());
40
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"
44                 ,namespace, serviceType, refName, refNameToInstance.keySet());
45
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"
49                 ,namespace, serviceType, refName, refNameToInstance.keySet());
50
51         String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceType);
52         try {
53             /*
54              Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
55              and makes it hard to compare with service references got from MXBean attributes
56             */
57             return ObjectNameUtil.withoutTransactionName(
58                     configServiceRefRegistry.getServiceReference(qNameOfService, refName));
59         } catch (final InstanceNotFoundException e) {
60             throw new IllegalArgumentException("No serviceInstance mapped to " + refName
61                     + " under service name " + serviceType + " , " + refNameToInstance.keySet(), e);
62
63         }
64     }
65
66     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
67         Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
68
69         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
70         for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
71             for (String refName : qNameToRefNameEntry.getValue().keySet()) {
72
73                 ObjectName on = qNameToRefNameEntry.getValue().get(refName);
74                 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
75
76                 QName qname = QName.create(qNameToRefNameEntry.getKey());
77                 String namespace = qname.getNamespace().toString();
78                 Map<String, Map<String, String>> serviceToRefs =
79                         retVal.computeIfAbsent(namespace, k -> new HashMap<>());
80
81                 String localName = qname.getLocalName();
82                 Map<String, String> refsToSis = serviceToRefs.computeIfAbsent(localName, k -> new HashMap<>());
83
84                 Preconditions.checkState(!refsToSis.containsKey(refName),
85                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
86                         localName, on);
87                 refsToSis.put(refName, si.toString());
88             }
89         }
90
91         return retVal;
92     }
93 }

©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.