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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
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.w3c.dom.Document;
21 import org.w3c.dom.Element;
23 import javax.management.ObjectName;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 public final class Services {
32 private static final String PROVIDER_KEY = "provider";
33 private static final String NAME_KEY = "name";
34 public static final String TYPE_KEY = "type";
35 public static final String SERVICE_KEY = "service";
37 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
43 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
44 return namespaceToServiceNameToRefNameToInstance;
47 private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
48 Services tracker = new Services();
50 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
51 String namespace = namespaceEntry.getKey();
53 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
55 String serviceName = serviceEntry.getKey();
56 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
58 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
59 if (namespaceToServices == null) {
60 namespaceToServices = Maps.newHashMap();
61 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
64 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
66 if (refNameToInstance == null) {
67 refNameToInstance = Maps.newHashMap();
68 namespaceToServices.put(serviceName, refNameToInstance);
71 String refName = refEntry.getKey();
73 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
74 refNameToInstance.put(refName, serviceInstance);
82 // TODO support edit strategies on services
84 public static Services fromXml(XmlElement xml) throws NetconfDocumentedException {
85 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
87 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
88 xml.checkUnrecognisedElements(services);
90 for (XmlElement service : services) {
92 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
93 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
95 Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
97 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
98 if(namespaceToServices == null) {
99 namespaceToServices = Maps.newHashMap();
100 retVal.put(prefixNamespace.getValue(), namespaceToServices);
103 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
105 Map<String, String> innerMap = Maps.newHashMap();
106 namespaceToServices.put(serviceName, innerMap);
108 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
109 service.checkUnrecognisedElements(instances, typeElement);
111 for (XmlElement instance : instances) {
112 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
113 String refName = nameElement.getTextContent();
115 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
116 String providerName = providerElement.getTextContent();
118 instance.checkUnrecognisedElements(nameElement, providerElement);
120 innerMap.put(refName, providerName);
124 return resolveServices(retVal);
127 public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
128 Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
130 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
131 for (String namespace : mappedServices.keySet()) {
133 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
134 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
135 root.appendChild(serviceElement);
137 Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlUtil.createPrefixedValue(XmlNetconfConstants.PREFIX, TYPE_KEY), XmlNetconfConstants.PREFIX,
138 serviceEntry.getKey(), Optional.of(namespace));
139 serviceElement.appendChild(typeElement);
141 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
142 Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
143 serviceElement.appendChild(instanceElement);
145 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
146 instanceElement.appendChild(nameElement);
148 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
149 instanceElement.appendChild(providerElement);
157 public static final class ServiceInstance {
158 public ServiceInstance(String moduleName, String instanceName) {
159 this.moduleName = moduleName;
160 this.instanceName = instanceName;
163 public static ServiceInstance fromString(String instanceId) {
164 instanceId = instanceId.trim();
165 Matcher matcher = p.matcher(instanceId);
166 if(!matcher.matches()) {
167 matcher = pDeprecated.matcher(instanceId);
170 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
171 + " or " + pDeprecated.toString() + " but was " + instanceId);
173 String factoryName = matcher.group(1);
174 String instanceName = matcher.group(2);
175 return new ServiceInstance(factoryName, instanceName);
178 private final String moduleName, instanceName;
179 private String serviceName;
181 public String getServiceName() {
185 public void setServiceName(String serviceName) {
186 this.serviceName = serviceName;
189 public String getModuleName() {
193 public String getInstanceName() {
197 private static final String blueprint = "/"
198 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
199 + XmlNetconfConstants.TYPE_KEY + "='%s']["
200 + XmlNetconfConstants.NAME_KEY + "='%s']";
202 // TODO unify with xpath in RuntimeRpc
204 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
205 private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
206 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
207 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
208 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
210 private static final String blueprintR = "/"
211 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
212 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
213 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
215 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
216 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
219 public String toString() {
220 return String.format(blueprint, moduleName, instanceName);
224 public int hashCode() {
225 final int prime = 31;
227 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
228 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
233 public boolean equals(Object obj) {
240 if (getClass() != obj.getClass()){
243 ServiceInstance other = (ServiceInstance) obj;
244 if (instanceName == null) {
245 if (other.instanceName != null){
248 } else if (!instanceName.equals(other.instanceName)){
251 if (moduleName == null) {
252 if (other.moduleName != null){
255 } else if (!moduleName.equals(other.moduleName)){
261 public ObjectName getObjectName(String transactionName) {
262 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
265 public static ServiceInstance fromObjectName(ObjectName on) {
266 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));