2 * Copyright (c) 2015 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.config.facade.xml.mapping.config;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Maps;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import javax.management.ObjectName;
21 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
22 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
23 import org.opendaylight.controller.config.util.xml.DocumentedException;
24 import org.opendaylight.controller.config.util.xml.XmlElement;
25 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
26 import org.opendaylight.controller.config.util.xml.XmlUtil;
27 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
31 public final class Services {
33 private static final String EMPTY_PROVIDER = "";
34 private static final String PROVIDER_KEY = "provider";
35 private static final String NAME_KEY = "name";
36 public static final String TYPE_KEY = "type";
37 public static final String SERVICE_KEY = "service";
39 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
45 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
46 return namespaceToServiceNameToRefNameToInstance;
49 private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
50 Services tracker = new Services();
52 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
53 String namespace = namespaceEntry.getKey();
55 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
57 String serviceName = serviceEntry.getKey();
58 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
60 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
61 if (namespaceToServices == null) {
62 namespaceToServices = Maps.newHashMap();
63 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
66 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
68 if (refNameToInstance == null) {
69 refNameToInstance = Maps.newHashMap();
70 namespaceToServices.put(serviceName, refNameToInstance);
73 String refName = refEntry.getKey();
74 //we want to compare reference not value of the provider
75 refNameToInstance.put(refName, refEntry.getValue() == EMPTY_PROVIDER
76 //provider name cannot be EMPTY_PROVIDER instance unless we are executing delete
77 ? ServiceInstance.EMPTY_SERVICE_INSTANCE
78 : ServiceInstance.fromString(refEntry.getValue()));
86 // TODO support edit strategies on services
88 public static Services fromXml(XmlElement xml) throws DocumentedException {
89 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
91 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
92 xml.checkUnrecognisedElements(services);
94 for (XmlElement service : services) {
96 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
97 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
99 Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
101 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
102 if(namespaceToServices == null) {
103 namespaceToServices = Maps.newHashMap();
104 retVal.put(prefixNamespace.getValue(), namespaceToServices);
107 String serviceName = ObjectNameAttributeReadingStrategy
108 .checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
110 Map<String, String> innerMap = namespaceToServices.get(serviceName);
111 if (innerMap == null) {
112 innerMap = Maps.newHashMap();
113 namespaceToServices.put(serviceName, innerMap);
116 List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
117 service.checkUnrecognisedElements(instances, typeElement);
119 for (XmlElement instance : instances) {
120 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
121 String refName = nameElement.getTextContent();
123 if (!ModifyAction.DELETE.toString().toLowerCase().equals(
124 instance.getAttribute(
125 XmlMappingConstants.OPERATION_ATTR_KEY,
126 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)))
128 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
129 String providerName = providerElement.getTextContent();
131 instance.checkUnrecognisedElements(nameElement, providerElement);
133 innerMap.put(refName, providerName);
135 //since this is a delete we dont have a provider name - we want empty service instance
136 innerMap.put(refName, EMPTY_PROVIDER);
141 return resolveServices(retVal);
144 public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
145 final Optional<String> configNs = Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
146 Element root = XmlUtil.createElement(document, XmlMappingConstants.SERVICES_KEY, configNs);
148 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
149 for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
151 for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
152 // service belongs to config.yang namespace
153 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, configNs);
154 root.appendChild(serviceElement);
156 // type belongs to config.yang namespace
157 String serviceType = serviceEntry.getKey();
158 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document, XmlMappingConstants.TYPE_KEY,
159 XmlMappingConstants.PREFIX, namespaceToRefEntry.getKey(), serviceType);
161 serviceElement.appendChild(typeElement);
163 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
164 Element instanceElement = XmlUtil.createElement(document, XmlMappingConstants.INSTANCE_KEY, Optional.<String>absent());
165 serviceElement.appendChild(instanceElement);
167 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
168 instanceElement.appendChild(nameElement);
170 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
171 instanceElement.appendChild(providerElement);
179 public static final class ServiceInstance {
180 public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
182 public ServiceInstance(String moduleName, String instanceName) {
183 this.moduleName = moduleName;
184 this.instanceName = instanceName;
187 public static ServiceInstance fromString(String instanceId) {
188 instanceId = instanceId.trim();
189 Matcher matcher = p.matcher(instanceId);
190 if(!matcher.matches()) {
191 matcher = pDeprecated.matcher(instanceId);
194 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
195 + " or " + pDeprecated.toString() + " but was " + instanceId);
197 String factoryName = matcher.group(1);
198 String instanceName = matcher.group(2);
199 return new ServiceInstance(factoryName, instanceName);
202 private final String moduleName, instanceName;
203 private String serviceName;
205 public String getServiceName() {
209 public void setServiceName(String serviceName) {
210 this.serviceName = serviceName;
213 public String getModuleName() {
217 public String getInstanceName() {
221 private static final String blueprint = "/"
222 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "["
223 + XmlMappingConstants.TYPE_KEY + "='%s']["
224 + XmlMappingConstants.NAME_KEY + "='%s']";
226 // TODO unify with xpath in RuntimeRpc
228 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
229 private static final String blueprintRDeprecated = "/" + XmlMappingConstants.CONFIG_KEY + "/"
230 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "\\["
231 + XmlMappingConstants.NAME_KEY + "='%s'\\]/" + XmlMappingConstants.INSTANCE_KEY + "\\["
232 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
234 private static final String blueprintR = "/"
235 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "\\["
236 + XmlMappingConstants.TYPE_KEY + "='%s'\\]\\["
237 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
239 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
240 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
243 public String toString() {
244 return String.format(blueprint, moduleName, instanceName);
248 public int hashCode() {
249 final int prime = 31;
251 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
252 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
257 public boolean equals(Object obj) {
264 if (getClass() != obj.getClass()){
267 ServiceInstance other = (ServiceInstance) obj;
268 if (instanceName == null) {
269 if (other.instanceName != null){
272 } else if (!instanceName.equals(other.instanceName)){
275 if (moduleName == null) {
276 if (other.moduleName != null){
279 } else if (!moduleName.equals(other.moduleName)){
285 public ObjectName getObjectName(String transactionName) {
286 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
289 public static ServiceInstance fromObjectName(ObjectName on) {
290 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));