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.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 import javax.annotation.Nullable;
25 import javax.management.ObjectName;
26 import java.util.Collection;
27 import java.util.List;
29 import java.util.Map.Entry;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 public final class Services {
35 private static final Logger logger = LoggerFactory.getLogger(Services.class);
37 private static final String PROVIDER_KEY = "provider";
38 private static final String NAME_KEY = "name";
39 public static final String TYPE_KEY = "type";
40 public static final String SERVICE_KEY = "service";
42 private long suffix = 1;
44 private final Map<ServiceInstance, String> instanceToRef = Maps.newHashMap();
45 private final Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>> serviceNameToRefNameToInstance = Maps
48 public String addServiceEntry(String serviceName, ObjectName on) {
50 String moduleName = on.getKeyProperty("moduleFactoryName");
51 String instanceName = on.getKeyProperty("instanceName");
53 String refName = addServiceEntry(serviceName, moduleName, instanceName);
54 logger.trace("Added service entry to tracker. Service name {}, ref name {}, module name {}, instance name {}",
55 serviceName, refName, moduleName, instanceName);
60 public String addServiceEntry(String serviceName, String moduleName, String instanceName) {
61 ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
62 serviceInstance.setServiceName(serviceName);
64 String refName = instanceToRef.get(serviceInstance);
66 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
67 if (refNameToInstance == null) {
68 refNameToInstance = Maps.newHashMap();
69 serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
72 if (refName != null) {
73 if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
74 refNameToInstance.put(refName, serviceInstance);
78 refName = "ref_" + instanceName;
80 final Set<String> refNamesAsSet = toSet(instanceToRef.values());
81 if (refNamesAsSet.contains(refName)) {
82 refName = findAvailableRefName(refName, refNamesAsSet);
85 instanceToRef.put(serviceInstance, refName);
86 refNameToInstance.put(refName, serviceInstance);
92 private Set<String> toSet(Collection<String> values) {
93 Set<String> refNamesAsSet = Sets.newHashSet();
95 for (String refName : values) {
96 boolean resultAdd = refNamesAsSet.add(refName);
97 Preconditions.checkState(resultAdd,
98 "Error occurred building services element, reference name {} was present twice", refName);
101 return refNamesAsSet;
104 public ServiceInstance getByServiceAndRefName(String serviceName, String refName) {
105 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
106 Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
107 + serviceNameToRefNameToInstance.keySet());
109 ServiceInstance serviceInstance = refNameToInstance.get(refName);
110 Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
111 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
112 return serviceInstance;
115 // TODO hide getMappedServices, call it explicitly in toXml
117 public Map<String, Map<String, String>> getMappedServices() {
118 Map<String, Map<String, String>> retVal = Maps.newHashMap();
120 for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
122 Map<String, String> innerRetVal = Maps.transformValues(serviceNameToRefNameToInstance.get(serviceName),
123 new Function<ServiceInstance, String>() {
126 public String apply(@Nullable ServiceInstance serviceInstance) {
127 return serviceInstance.toString();
130 retVal.put(serviceName, innerRetVal);
136 // TODO hide resolveServices, call it explicitly in fromXml
138 public static Services resolveServices(Map<String, Map<String, String>> mappedServices) {
139 Services tracker = new Services();
141 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
143 String serviceName = serviceEntry.getKey();
144 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
146 Map<String, ServiceInstance> refNameToInstance = tracker.serviceNameToRefNameToInstance
148 if (refNameToInstance == null) {
149 refNameToInstance = Maps.newHashMap();
150 tracker.serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
153 String refName = refEntry.getKey();
154 Preconditions.checkState(false == refNameToInstance.containsKey(refName),
155 "Duplicate reference name to service " + refName + " under service " + serviceName);
156 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
157 refNameToInstance.put(refName, serviceInstance);
159 tracker.instanceToRef.put(serviceInstance, refEntry.getKey());
165 public static Map<String, Map<String, String>> fromXml(XmlElement xml) {
166 Map<String, Map<String, String>> retVal = Maps.newHashMap();
168 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
169 xml.checkUnrecognisedElements(services);
171 for (XmlElement service : services) {
173 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
174 String serviceName = typeElement.getTextContent();
176 Map<String, String> innerMap = Maps.newHashMap();
177 retVal.put(serviceName, innerMap);
179 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
180 service.checkUnrecognisedElements(instances, typeElement);
182 for (XmlElement instance : instances) {
183 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
184 String refName = nameElement.getTextContent();
186 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
187 String providerName = providerElement.getTextContent();
189 instance.checkUnrecognisedElements(nameElement, providerElement);
191 innerMap.put(refName, providerName);
198 private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
199 String intitialRefName = refName;
202 refName = intitialRefName + "_" + suffix++;
203 if (refNamesAsSet.contains(refName) == false)
208 public Element toXml(Map<String, Map<String, String>> mappedServices, Document document) {
209 Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
210 XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
212 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
213 Element serviceElement = document.createElement(SERVICE_KEY);
214 root.appendChild(serviceElement);
216 Element typeElement = XmlUtil.createTextElement(document, TYPE_KEY, serviceEntry.getKey());
217 serviceElement.appendChild(typeElement);
219 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
220 Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
221 serviceElement.appendChild(instanceElement);
223 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
224 instanceElement.appendChild(nameElement);
226 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
227 instanceElement.appendChild(providerElement);
234 public static final class ServiceInstance {
235 public ServiceInstance(String moduleName, String instanceName) {
236 this.moduleName = moduleName;
237 this.instanceName = instanceName;
240 public static ServiceInstance fromString(String instanceId) {
241 instanceId = instanceId.trim();
242 Matcher matcher = p.matcher(instanceId);
243 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
244 + " but was " + instanceId);
245 String factoryName = matcher.group(1);
246 String instanceName = matcher.group(2);
247 return new ServiceInstance(factoryName, instanceName);
250 private final String moduleName, instanceName;
251 private String serviceName;
253 public String getServiceName() {
257 public void setServiceName(String serviceName) {
258 this.serviceName = serviceName;
261 public String getModuleName() {
265 public String getInstanceName() {
269 private static final String blueprint = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
270 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
271 + XmlNetconfConstants.NAME_KEY + "='%s']/" + XmlNetconfConstants.INSTANCE_KEY + "["
272 + XmlNetconfConstants.NAME_KEY + "='%s']";
274 private static final String blueprintR = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
275 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
276 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
277 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
279 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
282 public String toString() {
283 return String.format(blueprint, moduleName, instanceName);
287 public int hashCode() {
288 final int prime = 31;
290 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
291 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
296 public boolean equals(Object obj) {
301 if (getClass() != obj.getClass())
303 ServiceInstance other = (ServiceInstance) obj;
304 if (instanceName == null) {
305 if (other.instanceName != null)
307 } else if (!instanceName.equals(other.instanceName))
309 if (moduleName == null) {
310 if (other.moduleName != null)
312 } else if (!moduleName.equals(other.moduleName))