Simplify code with new Map features
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / config / Services.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.facade.xml.mapping.config;
10
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
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;
30
31 public final class Services {
32
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";
38
39     private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>>
40             namespaceToServiceNameToRefNameToInstance = new HashMap<>();
41
42     /**
43      *
44      */
45     public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
46         return namespaceToServiceNameToRefNameToInstance;
47     }
48
49     private static Services resolveServices(final Map<String, Map<String, Map<String, String>>> mappedServices) {
50         Services tracker = new Services();
51
52         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
53             String namespace = namespaceEntry.getKey();
54
55             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
56
57                 String serviceName = serviceEntry.getKey();
58                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
59
60                     Map<String, Map<String, ServiceInstance>> namespaceToServices =
61                             tracker.namespaceToServiceNameToRefNameToInstance.computeIfAbsent(namespace,
62                                     k -> new HashMap<>());
63
64                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
65                             .computeIfAbsent(serviceName, k -> new HashMap<>());
66
67                     String refName = refEntry.getKey();
68                     //we want to compare reference not value of the provider
69                     refNameToInstance.put(refName, refEntry.getValue() == EMPTY_PROVIDER
70                             //provider name cannot be EMPTY_PROVIDER instance unless we are executing delete
71                             ? ServiceInstance.EMPTY_SERVICE_INSTANCE
72                             : ServiceInstance.fromString(refEntry.getValue()));
73
74                 }
75             }
76         }
77         return tracker;
78     }
79
80     // TODO support edit strategies on services
81
82     public static Services fromXml(final XmlElement xml) throws DocumentedException {
83         Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
84
85         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
86         xml.checkUnrecognisedElements(services);
87
88         for (XmlElement service : services) {
89
90             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
91             Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
92
93             Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
94
95             Map<String, Map<String, String>> namespaceToServices =
96                     retVal.computeIfAbsent(prefixNamespace.getValue(), k -> new HashMap<>());
97
98             String serviceName =  ObjectNameAttributeReadingStrategy
99                 .checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
100
101             Map<String, String> innerMap = namespaceToServices.computeIfAbsent(serviceName, k -> new HashMap<>());
102
103             List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
104             service.checkUnrecognisedElements(instances, typeElement);
105
106             for (XmlElement instance : instances) {
107                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
108                 String refName = nameElement.getTextContent();
109
110                 if (!ModifyAction.DELETE.toString().toLowerCase().equals(
111                         instance.getAttribute(
112                                 XmlMappingConstants.OPERATION_ATTR_KEY,
113                                 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)))
114                 {
115                     XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
116                     String providerName = providerElement.getTextContent();
117
118                     instance.checkUnrecognisedElements(nameElement, providerElement);
119
120                     innerMap.put(refName, providerName);
121                 } else {
122                     //since this is a delete we dont have a provider name - we want empty service instance
123                     innerMap.put(refName, EMPTY_PROVIDER);
124                 }
125             }
126         }
127
128         return resolveServices(retVal);
129     }
130
131     public static Element toXml(final ServiceRegistryWrapper serviceRegistryWrapper, final Document document) {
132         final Optional<String> configNs = Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
133         Element root = XmlUtil.createElement(document, XmlMappingConstants.SERVICES_KEY, configNs);
134
135         Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
136         for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
137
138             for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
139                 // service belongs to config.yang namespace
140                 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, configNs);
141                 root.appendChild(serviceElement);
142
143                 // type belongs to config.yang namespace
144                 String serviceType = serviceEntry.getKey();
145                 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(
146                         document, XmlMappingConstants.TYPE_KEY, XmlMappingConstants.PREFIX,
147                         namespaceToRefEntry.getKey(), serviceType, configNs);
148
149                 serviceElement.appendChild(typeElement);
150
151                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
152                     Element instanceElement = XmlUtil.createElement(
153                             document, XmlMappingConstants.INSTANCE_KEY, configNs);
154                     serviceElement.appendChild(instanceElement);
155
156                     Element nameElement = XmlUtil.createTextElement(
157                             document, NAME_KEY, instanceEntry.getKey(), configNs);
158                     instanceElement.appendChild(nameElement);
159
160                     Element providerElement = XmlUtil.createTextElement(
161                             document, PROVIDER_KEY, instanceEntry.getValue(), configNs);
162                     instanceElement.appendChild(providerElement);
163                 }
164             }
165
166         }
167         return root;
168     }
169
170     public static final class ServiceInstance {
171         public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
172
173         public ServiceInstance(final String moduleName, final String instanceName) {
174             this.moduleName = moduleName;
175             this.instanceName = instanceName;
176         }
177
178         public static ServiceInstance fromString(String instanceId) {
179             instanceId = instanceId.trim();
180             Matcher matcher = p.matcher(instanceId);
181             if(!matcher.matches()) {
182                 matcher = pDeprecated.matcher(instanceId);
183             }
184
185             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
186                     + " or " + pDeprecated.toString() + " but was " + instanceId);
187
188             String factoryName = matcher.group(1);
189             String instanceName = matcher.group(2);
190             return new ServiceInstance(factoryName, instanceName);
191         }
192
193         private final String moduleName, instanceName;
194         private String serviceName;
195
196         public String getServiceName() {
197             return serviceName;
198         }
199
200         public void setServiceName(final String serviceName) {
201             this.serviceName = serviceName;
202         }
203
204         public String getModuleName() {
205             return moduleName;
206         }
207
208         public String getInstanceName() {
209             return instanceName;
210         }
211
212         private static final String blueprint = "/"
213                 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "["
214                 + XmlMappingConstants.TYPE_KEY + "='%s']["
215                 + XmlMappingConstants.NAME_KEY + "='%s']";
216
217         // TODO unify with xpath in RuntimeRpc
218
219         // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
220         private static final String blueprintRDeprecated = "/" + XmlMappingConstants.CONFIG_KEY + "/"
221                 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "\\["
222                 + XmlMappingConstants.NAME_KEY + "='%s'\\]/" + XmlMappingConstants.INSTANCE_KEY + "\\["
223                 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
224
225         private static final String blueprintR = "/"
226                 + XmlMappingConstants.MODULES_KEY + "/" + XmlMappingConstants.MODULE_KEY + "\\["
227                 + XmlMappingConstants.TYPE_KEY + "='%s'\\]\\["
228                 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
229
230         private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
231         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
232
233         @Override
234         public String toString() {
235             return String.format(blueprint, moduleName, instanceName);
236         }
237
238         @Override
239         public int hashCode() {
240             final int prime = 31;
241             int result = 1;
242             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
243             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
244             return result;
245         }
246
247         @Override
248         public boolean equals(final Object obj) {
249             if (this == obj){
250                 return true;
251             }
252             if (obj == null){
253                 return false;
254             }
255             if (getClass() != obj.getClass()){
256                 return false;
257             }
258             ServiceInstance other = (ServiceInstance) obj;
259             if (instanceName == null) {
260                 if (other.instanceName != null){
261                     return false;
262                 }
263             } else if (!instanceName.equals(other.instanceName)){
264                 return false;
265             }
266             if (moduleName == null) {
267                 if (other.moduleName != null){
268                     return false;
269                 }
270             } else if (!moduleName.equals(other.moduleName)){
271                 return false;
272             }
273             return true;
274         }
275
276         public ObjectName getObjectName(final String transactionName) {
277             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
278         }
279
280         public static ServiceInstance fromObjectName(final ObjectName on) {
281             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
282         }
283     }
284
285 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.