CDS: Add stress test RPC to the cars model
[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 com.google.common.collect.Maps;
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(ServiceReferenceReadableRegistry configServiceRefRegistry) {
25         this.configServiceRefRegistry = configServiceRefRegistry;
26     }
27
28     public ObjectName getByServiceAndRefName(String namespace, String serviceType, 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 (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 = Maps.newHashMap();
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 = retVal.get(namespace);
79                 if(serviceToRefs==null) {
80                     serviceToRefs = Maps.newHashMap();
81                     retVal.put(namespace, serviceToRefs);
82                 }
83
84                 String localName = qname.getLocalName();
85                 Map<String, String> refsToSis = serviceToRefs.get(localName);
86                 if(refsToSis==null) {
87                     refsToSis = Maps.newHashMap();
88                     serviceToRefs.put(localName, refsToSis);
89                 }
90
91                 Preconditions.checkState(!refsToSis.containsKey(refName),
92                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
93                         localName, on);
94                 refsToSis.put(refName, si.toString());
95             }
96         }
97
98         return retVal;
99     }
100 }