Fix checkstyle issues to enforce it
[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, 2017 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", namespace, serviceType,
34                 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", namespace,
39                 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", namespace, serviceType,
44                 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", namespace, serviceType,
49                 refName, refNameToInstance.keySet());
50
51         String serviceName = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceType);
52         try {
53             /*
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
57              */
58             return ObjectNameUtil
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);
63
64         }
65     }
66
67     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
68         Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
69
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()) {
73
74                 ObjectName on = nameToRefEntry.getValue().get(refName);
75                 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
76
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<>());
81
82                 String localName = qname.getLocalName();
83                 Map<String, String> refsToSis = serviceToRefs.computeIfAbsent(localName, k -> new HashMap<>());
84
85                 Preconditions.checkState(!refsToSis.containsKey(refName),
86                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
87                         localName, on);
88                 refsToSis.put(refName, si.toString());
89             }
90         }
91
92         return retVal;
93     }
94 }