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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 import javax.annotation.Nullable;
26 import javax.management.ObjectName;
27 import java.util.Collection;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
35 public final class Services {
36 private static final Logger logger = LoggerFactory.getLogger(Services.class);
38 private static final String PROVIDER_KEY = "provider";
39 private static final String NAME_KEY = "name";
40 public static final String TYPE_KEY = "type";
41 public static final String SERVICE_KEY = "service";
43 private long suffix = 1;
45 private final Map<ServiceInstance, String> instanceToRef = Maps.newHashMap();
46 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
49 public String addServiceEntry(String namespace, String serviceName, ObjectName on) {
51 String moduleName = on.getKeyProperty("moduleFactoryName");
52 String instanceName = on.getKeyProperty("instanceName");
54 String refName = addServiceEntry(namespace, serviceName, moduleName, instanceName);
55 logger.trace("Added service entry to tracker. Service name {}, ref name {}, module name {}, instance name {}",
56 serviceName, refName, moduleName, instanceName);
61 public String addServiceEntry(String namespace, String serviceName, String moduleName, String instanceName) {
62 ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
63 serviceInstance.setServiceName(serviceName);
65 String refName = instanceToRef.get(serviceInstance);
67 Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance.get(namespace);
68 if (serviceNameToRefNameToInstance == null) {
69 serviceNameToRefNameToInstance = Maps.newHashMap();
70 namespaceToServiceNameToRefNameToInstance.put(namespace, serviceNameToRefNameToInstance);
73 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
74 if (refNameToInstance == null) {
75 refNameToInstance = Maps.newHashMap();
76 serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
79 if (refName != null) {
80 if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
81 refNameToInstance.put(refName, serviceInstance);
85 refName = "ref_" + instanceName;
87 final Set<String> refNamesAsSet = toSet(instanceToRef.values());
88 if (refNamesAsSet.contains(refName)) {
89 refName = findAvailableRefName(refName, refNamesAsSet);
92 instanceToRef.put(serviceInstance, refName);
93 refNameToInstance.put(refName, serviceInstance);
99 private Set<String> toSet(Collection<String> values) {
100 Set<String> refNamesAsSet = Sets.newHashSet();
102 for (String refName : values) {
103 boolean resultAdd = refNamesAsSet.add(refName);
104 Preconditions.checkState(resultAdd,
105 "Error occurred building services element, reference name {} was present twice", refName);
108 return refNamesAsSet;
111 public ServiceInstance getByServiceAndRefName(String namespace, String serviceName, String refName) {
112 Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance.get(namespace);
113 Preconditions.checkArgument(serviceNameToRefNameToInstance != null, "No serviceInstances mapped to " + namespace + " , "
114 + serviceNameToRefNameToInstance.keySet());
116 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
117 Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
118 + serviceNameToRefNameToInstance.keySet());
120 ServiceInstance serviceInstance = refNameToInstance.get(refName);
121 Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
122 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
123 return serviceInstance;
126 // TODO hide getMappedServices, call it explicitly in toXml
128 public Map<String, Map<String, Map<String, String>>> getMappedServices() {
129 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
131 for (String namespace : namespaceToServiceNameToRefNameToInstance.keySet()) {
133 Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance
135 Map<String, Map<String, String>> innerRetVal = Maps.newHashMap();
137 for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
139 Map<String, String> innerInnerRetVal = Maps.transformValues(
140 serviceNameToRefNameToInstance.get(serviceName), new Function<ServiceInstance, String>() {
143 public String apply(@Nullable ServiceInstance serviceInstance) {
144 return serviceInstance.toString();
147 innerRetVal.put(serviceName, innerInnerRetVal);
149 retVal.put(namespace, innerRetVal);
155 // TODO hide resolveServices, call it explicitly in fromXml
157 public static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
158 Services tracker = new Services();
160 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
161 String namespace = namespaceEntry.getKey();
163 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
165 String serviceName = serviceEntry.getKey();
166 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
168 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
169 if (namespaceToServices == null) {
170 namespaceToServices = Maps.newHashMap();
171 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
174 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
176 if (refNameToInstance == null) {
177 refNameToInstance = Maps.newHashMap();
178 namespaceToServices.put(serviceName, refNameToInstance);
181 String refName = refEntry.getKey();
182 Preconditions.checkState(false == refNameToInstance.containsKey(refName),
183 "Duplicate reference name to service " + refName + " under service " + serviceName);
184 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
185 refNameToInstance.put(refName, serviceInstance);
187 tracker.instanceToRef.put(serviceInstance, refEntry.getKey());
194 public static Map<String, Map<String, Map<String, String>>> fromXml(XmlElement xml) {
195 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
197 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
198 xml.checkUnrecognisedElements(services);
200 for (XmlElement service : services) {
202 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
203 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
205 Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
207 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
208 if(namespaceToServices == null) {
209 namespaceToServices = Maps.newHashMap();
210 retVal.put(prefixNamespace.getValue(), namespaceToServices);
213 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
215 Map<String, String> innerMap = Maps.newHashMap();
216 namespaceToServices.put(serviceName, innerMap);
218 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
219 service.checkUnrecognisedElements(instances, typeElement);
221 for (XmlElement instance : instances) {
222 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
223 String refName = nameElement.getTextContent();
225 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
226 String providerName = providerElement.getTextContent();
228 instance.checkUnrecognisedElements(nameElement, providerElement);
230 innerMap.put(refName, providerName);
237 private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
238 String intitialRefName = refName;
241 refName = intitialRefName + "_" + suffix++;
242 if (refNamesAsSet.contains(refName) == false)
247 public Element toXml(Map<String, Map<String, Map<String, String>>> mappedServices, Document document) {
248 Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
249 XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
251 for (String namespace : mappedServices.keySet()) {
253 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
254 Element serviceElement = document.createElement(SERVICE_KEY);
255 root.appendChild(serviceElement);
257 Element typeElement = XmlUtil.createPrefixedTextElement(document, TYPE_KEY, XmlNetconfConstants.PREFIX,
258 serviceEntry.getKey());
259 XmlUtil.addPrefixedNamespaceAttr(typeElement, XmlNetconfConstants.PREFIX, namespace);
260 serviceElement.appendChild(typeElement);
262 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
263 Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
264 serviceElement.appendChild(instanceElement);
266 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
267 instanceElement.appendChild(nameElement);
269 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
270 instanceElement.appendChild(providerElement);
278 public static final class ServiceInstance {
279 public ServiceInstance(String moduleName, String instanceName) {
280 this.moduleName = moduleName;
281 this.instanceName = instanceName;
284 public static ServiceInstance fromString(String instanceId) {
285 instanceId = instanceId.trim();
286 Matcher matcher = p.matcher(instanceId);
287 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
288 + " but was " + instanceId);
289 String factoryName = matcher.group(1);
290 String instanceName = matcher.group(2);
291 return new ServiceInstance(factoryName, instanceName);
294 private final String moduleName, instanceName;
295 private String serviceName;
297 public String getServiceName() {
301 public void setServiceName(String serviceName) {
302 this.serviceName = serviceName;
305 public String getModuleName() {
309 public String getInstanceName() {
313 private static final String blueprint = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
314 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
315 + XmlNetconfConstants.NAME_KEY + "='%s']/" + XmlNetconfConstants.INSTANCE_KEY + "["
316 + XmlNetconfConstants.NAME_KEY + "='%s']";
318 private static final String blueprintR = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
319 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
320 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
321 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
323 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
326 public String toString() {
327 return String.format(blueprint, moduleName, instanceName);
331 public int hashCode() {
332 final int prime = 31;
334 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
335 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
340 public boolean equals(Object obj) {
345 if (getClass() != obj.getClass())
347 ServiceInstance other = (ServiceInstance) obj;
348 if (instanceName == null) {
349 if (other.instanceName != null)
351 } else if (!instanceName.equals(other.instanceName))
353 if (moduleName == null) {
354 if (other.moduleName != null)
356 } else if (!moduleName.equals(other.moduleName))