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.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Sets;
15 import org.opendaylight.controller.netconf.util.xml.XmlElement;
16 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
21 import javax.annotation.Nullable;
22 import javax.management.ObjectName;
23 import java.util.Collection;
24 import java.util.List;
26 import java.util.Map.Entry;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 public final class Services {
33 private static final String PROVIDER_KEY = "provider";
34 private static final String NAME_KEY = "name";
35 public static final String TYPE_KEY = "type";
36 public static final String SERVICE_KEY = "service";
38 private long suffix = 1;
40 private final Map<ServiceInstance, String> instanceToRef = Maps.newHashMap();
41 private final Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>> serviceNameToRefNameToInstance = Maps
44 public String addServiceEntry(String serviceName, ObjectName on) {
46 String moduleName = on.getKeyProperty("moduleFactoryName");
47 String instanceName = on.getKeyProperty("instanceName");
49 return addServiceEntry(serviceName, moduleName, instanceName);
52 public String addServiceEntry(String serviceName, String moduleName, String instanceName) {
53 ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
54 serviceInstance.setServiceName(serviceName);
56 String refName = instanceToRef.get(serviceInstance);
58 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
59 if (refNameToInstance == null) {
60 refNameToInstance = Maps.newHashMap();
61 serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
64 if (refName != null) {
65 if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
66 refNameToInstance.put(refName, serviceInstance);
70 refName = "ref_" + instanceName;
72 final Set<String> refNamesAsSet = toSet(instanceToRef.values());
73 if (refNamesAsSet.contains(refName)) {
74 refName = findAvailableRefName(refName, refNamesAsSet);
77 instanceToRef.put(serviceInstance, refName);
78 refNameToInstance.put(refName, serviceInstance);
84 private Set<String> toSet(Collection<String> values) {
85 Set<String> refNamesAsSet = Sets.newHashSet();
87 for (String refName : values) {
88 boolean resultAdd = refNamesAsSet.add(refName);
89 Preconditions.checkState(resultAdd,
90 "Error occurred building services element, reference name {} was present twice", refName);
96 public ServiceInstance getByServiceAndRefName(String serviceName, String refName) {
97 Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
98 Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
99 + serviceNameToRefNameToInstance.keySet());
101 ServiceInstance serviceInstance = refNameToInstance.get(refName);
102 Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
103 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
104 return serviceInstance;
107 // TODO hide getMappedServices, call it explicitly in toXml
109 public Map<String, Map<String, String>> getMappedServices() {
110 Map<String, Map<String, String>> retVal = Maps.newHashMap();
112 for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
114 Map<String, String> innerRetVal = Maps.transformValues(serviceNameToRefNameToInstance.get(serviceName),
115 new Function<ServiceInstance, String>() {
118 public String apply(@Nullable ServiceInstance serviceInstance) {
119 return serviceInstance.toString();
122 retVal.put(serviceName, innerRetVal);
128 // TODO hide resolveServices, call it explicitly in fromXml
130 public static Services resolveServices(Map<String, Map<String, String>> mappedServices) {
131 Services tracker = new Services();
133 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
135 String serviceName = serviceEntry.getKey();
136 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
138 Map<String, ServiceInstance> refNameToInstance = tracker.serviceNameToRefNameToInstance
140 if (refNameToInstance == null) {
141 refNameToInstance = Maps.newHashMap();
142 tracker.serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
145 String refName = refEntry.getKey();
146 Preconditions.checkState(false == refNameToInstance.containsKey(refName),
147 "Duplicate reference name to service " + refName + " under service " + serviceName);
148 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
149 refNameToInstance.put(refName, serviceInstance);
151 tracker.instanceToRef.put(serviceInstance, refEntry.getKey());
157 public static Map<String, Map<String, String>> fromXml(XmlElement xml) {
158 Map<String, Map<String, String>> retVal = Maps.newHashMap();
160 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
161 xml.checkUnrecognisedElements(services);
163 for (XmlElement service : services) {
165 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
166 String serviceName = typeElement.getTextContent();
168 Map<String, String> innerMap = Maps.newHashMap();
169 retVal.put(serviceName, innerMap);
171 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
172 service.checkUnrecognisedElements(instances, typeElement);
174 for (XmlElement instance : instances) {
175 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
176 String refName = nameElement.getTextContent();
178 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
179 String providerName = providerElement.getTextContent();
181 instance.checkUnrecognisedElements(nameElement, providerElement);
183 innerMap.put(refName, providerName);
190 private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
191 String intitialRefName = refName;
194 refName = intitialRefName + "_" + suffix++;
195 if (refNamesAsSet.contains(refName) == false)
200 public Element toXml(Map<String, Map<String, String>> mappedServices, Document document) {
201 Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
202 XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
204 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
205 Element serviceElement = document.createElement(SERVICE_KEY);
206 root.appendChild(serviceElement);
208 Element typeElement = XmlUtil.createTextElement(document, TYPE_KEY, serviceEntry.getKey());
209 serviceElement.appendChild(typeElement);
211 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
212 Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
213 serviceElement.appendChild(instanceElement);
215 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
216 instanceElement.appendChild(nameElement);
218 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
219 instanceElement.appendChild(providerElement);
226 public static final class ServiceInstance {
227 public ServiceInstance(String moduleName, String instanceName) {
228 this.moduleName = moduleName;
229 this.instanceName = instanceName;
232 public static ServiceInstance fromString(String instanceId) {
233 instanceId = instanceId.trim();
234 Matcher matcher = p.matcher(instanceId);
235 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
236 + " but was " + instanceId);
237 String factoryName = matcher.group(1);
238 String instanceName = matcher.group(2);
239 return new ServiceInstance(factoryName, instanceName);
242 private final String moduleName, instanceName;
243 private String serviceName;
245 public String getServiceName() {
249 public void setServiceName(String serviceName) {
250 this.serviceName = serviceName;
253 public String getModuleName() {
257 public String getInstanceName() {
261 private static final String blueprint = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
262 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
263 + XmlNetconfConstants.NAME_KEY + "='%s']/" + XmlNetconfConstants.INSTANCE_KEY + "["
264 + XmlNetconfConstants.NAME_KEY + "='%s']";
266 private static final String blueprintR = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
267 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
268 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
269 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
271 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
274 public String toString() {
275 return String.format(blueprint, moduleName, instanceName);
279 public int hashCode() {
280 final int prime = 31;
282 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
283 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
288 public boolean equals(Object obj) {
293 if (getClass() != obj.getClass())
295 ServiceInstance other = (ServiceInstance) obj;
296 if (instanceName == null) {
297 if (other.instanceName != null)
299 } else if (!instanceName.equals(other.instanceName))
301 if (moduleName == null) {
302 if (other.moduleName != null)
304 } else if (!moduleName.equals(other.moduleName))