2 * Copyright (c) 2015, 2017 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;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
21 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
22 import org.opendaylight.controller.config.util.xml.DocumentedException;
23 import org.opendaylight.controller.config.util.xml.XmlElement;
24 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
25 import org.opendaylight.controller.config.util.xml.XmlUtil;
26 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public final class Services {
32 private static final String EMPTY_PROVIDER = "";
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 final Map<String
40 Map<String/* ServiceName */,
41 Map<String/* refName */,
42 ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = new HashMap<>();
44 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
45 return namespaceToServiceNameToRefNameToInstance;
48 private static Services resolveServices(final Map<String, Map<String, Map<String, String>>> mappedServices) {
49 Services tracker = new Services();
51 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
52 String namespace = namespaceEntry.getKey();
54 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
56 String serviceName = serviceEntry.getKey();
57 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
59 Map<String, Map<String, ServiceInstance>> namespaceToServices =
60 tracker.namespaceToServiceNameToRefNameToInstance
61 .computeIfAbsent(namespace, k -> new HashMap<>());
63 Map<String, ServiceInstance> refNameToInstance = namespaceToServices.computeIfAbsent(serviceName,
64 k -> new HashMap<>());
66 String refName = refEntry.getKey();
67 // we want to compare reference not value of the provider
68 refNameToInstance.put(refName, refEntry.getValue() == EMPTY_PROVIDER
69 // provider name cannot be EMPTY_PROVIDER instance unless we are executing
71 ? ServiceInstance.EMPTY_SERVICE_INSTANCE
72 : ServiceInstance.fromString(refEntry.getValue()));
80 // TODO support edit strategies on services
82 public static Services fromXml(final XmlElement xml) throws DocumentedException {
83 Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
85 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
86 xml.checkUnrecognisedElements(services);
88 for (XmlElement service : services) {
90 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
91 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
93 Preconditions.checkState(prefixNamespace.getKey() != null && !prefixNamespace.getKey().equals(""),
94 "Type attribute was not prefixed");
96 Map<String, Map<String, String>> namespaceToServices = retVal.computeIfAbsent(prefixNamespace.getValue(),
97 k -> new HashMap<>());
99 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement,
102 Map<String, String> innerMap = namespaceToServices.computeIfAbsent(serviceName, k -> new HashMap<>());
104 List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
105 service.checkUnrecognisedElements(instances, typeElement);
107 for (XmlElement instance : instances) {
108 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
109 String refName = nameElement.getTextContent();
111 if (!ModifyAction.DELETE.toString().toLowerCase()
112 .equals(instance.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
113 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0))) {
114 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
115 String providerName = providerElement.getTextContent();
117 instance.checkUnrecognisedElements(nameElement, providerElement);
119 innerMap.put(refName, providerName);
121 // since this is a delete we dont have a provider name - we want empty service
123 innerMap.put(refName, EMPTY_PROVIDER);
128 return resolveServices(retVal);
131 public static Element toXml(final ServiceRegistryWrapper serviceRegistryWrapper, final Document document) {
132 final Optional<String> configNs = Optional
133 .of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
134 Element root = XmlUtil.createElement(document, XmlMappingConstants.SERVICES_KEY, configNs);
136 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
137 for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
139 for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
140 // service belongs to config.yang namespace
141 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, configNs);
142 root.appendChild(serviceElement);
144 // type belongs to config.yang namespace
145 String serviceType = serviceEntry.getKey();
146 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document,
147 XmlMappingConstants.TYPE_KEY, XmlMappingConstants.PREFIX, namespaceToRefEntry.getKey(),
148 serviceType, configNs);
150 serviceElement.appendChild(typeElement);
152 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
153 Element instanceElement = XmlUtil.createElement(document, XmlMappingConstants.INSTANCE_KEY,
155 serviceElement.appendChild(instanceElement);
157 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(),
159 instanceElement.appendChild(nameElement);
161 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY,
162 instanceEntry.getValue(), configNs);
163 instanceElement.appendChild(providerElement);
171 public static final class ServiceInstance {
172 public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
174 public ServiceInstance(final String moduleName, final String instanceName) {
175 this.moduleName = moduleName;
176 this.instanceName = instanceName;
179 public static ServiceInstance fromString(String instanceId) {
180 instanceId = instanceId.trim();
181 Matcher matcher = PATTERN.matcher(instanceId);
182 if (!matcher.matches()) {
183 matcher = PATTERN_DEPRECATED.matcher(instanceId);
186 Preconditions.checkArgument(matcher.matches(),
187 "Unexpected format for provider, expected " + PATTERN.toString()
188 + " or " + PATTERN_DEPRECATED.toString() + " but was " + instanceId);
190 String factoryName = matcher.group(1);
191 String instanceName = matcher.group(2);
192 return new ServiceInstance(factoryName, instanceName);
195 private final String moduleName;
196 private final String instanceName;
198 private String serviceName;
200 public String getServiceName() {
204 public void setServiceName(final String serviceName) {
205 this.serviceName = serviceName;
208 public String getModuleName() {
212 public String getInstanceName() {
216 private static final String BLUEPRINT = "/" + XmlMappingConstants.MODULES_KEY + "/"
217 + XmlMappingConstants.MODULE_KEY + "[" + XmlMappingConstants.TYPE_KEY + "='%s']["
218 + XmlMappingConstants.NAME_KEY + "='%s']";
220 // TODO unify with xpath in RuntimeRpc
222 // Previous version of xpath, needs to be supported for backwards compatibility
223 // (persisted configs by config-persister)
224 private static final String BLUEPRINTR_DEPRECATED = "/" + XmlMappingConstants.CONFIG_KEY + "/"
225 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "\\["
226 + XmlMappingConstants.NAME_KEY + "='%s'\\]/" + XmlMappingConstants.INSTANCE_KEY + "\\["
227 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
229 private static final String BLUEPRINTR = "/" + XmlMappingConstants.MODULES_KEY + "/"
230 + XmlMappingConstants.MODULE_KEY + "\\[" + XmlMappingConstants.TYPE_KEY + "='%s'\\]\\["
231 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
233 private static final Pattern PATTERN_DEPRECATED =
234 Pattern.compile(String.format(BLUEPRINTR_DEPRECATED, "(.+)", "(.+)"));
235 private static final Pattern PATTERN = Pattern.compile(String.format(BLUEPRINTR, "(.+)", "(.+)"));
238 public String toString() {
239 return String.format(BLUEPRINT, moduleName, instanceName);
243 public int hashCode() {
244 final int prime = 31;
246 result = prime * result + (instanceName == null ? 0 : instanceName.hashCode());
247 result = prime * result + (moduleName == null ? 0 : moduleName.hashCode());
252 public boolean equals(final Object obj) {
259 if (getClass() != obj.getClass()) {
262 ServiceInstance other = (ServiceInstance) obj;
263 if (instanceName == null) {
264 if (other.instanceName != null) {
267 } else if (!instanceName.equals(other.instanceName)) {
270 if (moduleName == null) {
271 if (other.moduleName != null) {
274 } else if (!moduleName.equals(other.moduleName)) {
280 public ObjectName getObjectName(final String transactionName) {
281 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
284 public static ServiceInstance fromObjectName(final ObjectName on) {
285 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));