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 java.util.List;
13 import java.util.Map.Entry;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import javax.management.ObjectName;
19 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Maps;
33 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 final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
48 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
49 return namespaceToServiceNameToRefNameToInstance;
52 private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
53 Services tracker = new Services();
55 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
56 String namespace = namespaceEntry.getKey();
58 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
60 String serviceName = serviceEntry.getKey();
61 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
63 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
64 if (namespaceToServices == null) {
65 namespaceToServices = Maps.newHashMap();
66 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
69 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
71 if (refNameToInstance == null) {
72 refNameToInstance = Maps.newHashMap();
73 namespaceToServices.put(serviceName, refNameToInstance);
76 String refName = refEntry.getKey();
78 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
79 refNameToInstance.put(refName, serviceInstance);
87 // TODO support edit strategies on services
89 public static Services fromXml(XmlElement xml) {
90 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
92 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
93 xml.checkUnrecognisedElements(services);
95 for (XmlElement service : services) {
97 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
98 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
100 Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
102 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
103 if(namespaceToServices == null) {
104 namespaceToServices = Maps.newHashMap();
105 retVal.put(prefixNamespace.getValue(), namespaceToServices);
108 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
110 Map<String, String> innerMap = Maps.newHashMap();
111 namespaceToServices.put(serviceName, innerMap);
113 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
114 service.checkUnrecognisedElements(instances, typeElement);
116 for (XmlElement instance : instances) {
117 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
118 String refName = nameElement.getTextContent();
120 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
121 String providerName = providerElement.getTextContent();
123 instance.checkUnrecognisedElements(nameElement, providerElement);
125 innerMap.put(refName, providerName);
129 return resolveServices(retVal);
132 public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
133 Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
135 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
136 for (String namespace : mappedServices.keySet()) {
138 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
139 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
140 root.appendChild(serviceElement);
142 Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlUtil.createPrefixedValue(XmlNetconfConstants.PREFIX, TYPE_KEY), XmlNetconfConstants.PREFIX,
143 serviceEntry.getKey(), Optional.of(namespace));
144 serviceElement.appendChild(typeElement);
146 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
147 Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
148 serviceElement.appendChild(instanceElement);
150 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
151 instanceElement.appendChild(nameElement);
153 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
154 instanceElement.appendChild(providerElement);
162 public static final class ServiceInstance {
163 public ServiceInstance(String moduleName, String instanceName) {
164 this.moduleName = moduleName;
165 this.instanceName = instanceName;
168 public static ServiceInstance fromString(String instanceId) {
169 instanceId = instanceId.trim();
170 Matcher matcher = p.matcher(instanceId);
171 if(matcher.matches() == false) {
172 matcher = pDeprecated.matcher(instanceId);
175 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
176 + " or " + pDeprecated.toString() + " but was " + instanceId);
178 String factoryName = matcher.group(1);
179 String instanceName = matcher.group(2);
180 return new ServiceInstance(factoryName, instanceName);
183 private final String moduleName, instanceName;
184 private String serviceName;
186 public String getServiceName() {
190 public void setServiceName(String serviceName) {
191 this.serviceName = serviceName;
194 public String getModuleName() {
198 public String getInstanceName() {
202 private static final String blueprint = "/"
203 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
204 + XmlNetconfConstants.TYPE_KEY + "='%s']["
205 + XmlNetconfConstants.NAME_KEY + "='%s']";
207 // TODO unify with xpath in RuntimeRpc
209 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
210 private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
211 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
212 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
213 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
215 private static final String blueprintR = "/"
216 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
217 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
218 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
220 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
221 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
224 public String toString() {
225 return String.format(blueprint, moduleName, instanceName);
229 public int hashCode() {
230 final int prime = 31;
232 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
233 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
238 public boolean equals(Object obj) {
243 if (getClass() != obj.getClass())
245 ServiceInstance other = (ServiceInstance) obj;
246 if (instanceName == null) {
247 if (other.instanceName != null)
249 } else if (!instanceName.equals(other.instanceName))
251 if (moduleName == null) {
252 if (other.moduleName != null)
254 } else if (!moduleName.equals(other.moduleName))
259 public ObjectName getObjectName(String transactionName) {
260 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
263 public static ServiceInstance fromObjectName(ObjectName on) {
264 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));