Fix checkstyle issues to enforce it
[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, 2017 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 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
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;
29
30 public final class Services {
31
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";
37
38     private final Map<String
39         /* Namespace */,
40         Map<String/* ServiceName */,
41         Map<String/* refName */,
42         ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = new HashMap<>();
43
44     public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
45         return namespaceToServiceNameToRefNameToInstance;
46     }
47
48     private static Services resolveServices(final Map<String, Map<String, Map<String, String>>> mappedServices) {
49         Services tracker = new Services();
50
51         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
52             String namespace = namespaceEntry.getKey();
53
54             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
55
56                 String serviceName = serviceEntry.getKey();
57                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
58
59                     Map<String, Map<String, ServiceInstance>> namespaceToServices =
60                             tracker.namespaceToServiceNameToRefNameToInstance
61                             .computeIfAbsent(namespace, k -> new HashMap<>());
62
63                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices.computeIfAbsent(serviceName,
64                         k -> new HashMap<>());
65
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
70                             // 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(""),
94                     "Type attribute was not prefixed");
95
96             Map<String, Map<String, String>> namespaceToServices = retVal.computeIfAbsent(prefixNamespace.getValue(),
97                 k -> new HashMap<>());
98
99             String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement,
100                     prefixNamespace);
101
102             Map<String, String> innerMap = namespaceToServices.computeIfAbsent(serviceName, k -> new HashMap<>());
103
104             List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
105             service.checkUnrecognisedElements(instances, typeElement);
106
107             for (XmlElement instance : instances) {
108                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
109                 String refName = nameElement.getTextContent();
110
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();
116
117                     instance.checkUnrecognisedElements(nameElement, providerElement);
118
119                     innerMap.put(refName, providerName);
120                 } else {
121                     // since this is a delete we dont have a provider name - we want empty service
122                     // 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
133                 .of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
134         Element root = XmlUtil.createElement(document, XmlMappingConstants.SERVICES_KEY, configNs);
135
136         Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
137         for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
138
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);
143
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);
149
150                 serviceElement.appendChild(typeElement);
151
152                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
153                     Element instanceElement = XmlUtil.createElement(document, XmlMappingConstants.INSTANCE_KEY,
154                             configNs);
155                     serviceElement.appendChild(instanceElement);
156
157                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(),
158                             configNs);
159                     instanceElement.appendChild(nameElement);
160
161                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY,
162                             instanceEntry.getValue(), configNs);
163                     instanceElement.appendChild(providerElement);
164                 }
165             }
166
167         }
168         return root;
169     }
170
171     public static final class ServiceInstance {
172         public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
173
174         public ServiceInstance(final String moduleName, final String instanceName) {
175             this.moduleName = moduleName;
176             this.instanceName = instanceName;
177         }
178
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);
184             }
185
186             Preconditions.checkArgument(matcher.matches(),
187                     "Unexpected format for provider, expected " + PATTERN.toString()
188                     + " or " + PATTERN_DEPRECATED.toString() + " but was " + instanceId);
189
190             String factoryName = matcher.group(1);
191             String instanceName = matcher.group(2);
192             return new ServiceInstance(factoryName, instanceName);
193         }
194
195         private final String moduleName;
196         private final String instanceName;
197
198         private String serviceName;
199
200         public String getServiceName() {
201             return serviceName;
202         }
203
204         public void setServiceName(final String serviceName) {
205             this.serviceName = serviceName;
206         }
207
208         public String getModuleName() {
209             return moduleName;
210         }
211
212         public String getInstanceName() {
213             return instanceName;
214         }
215
216         private static final String BLUEPRINT = "/" + XmlMappingConstants.MODULES_KEY + "/"
217                 + XmlMappingConstants.MODULE_KEY + "[" + XmlMappingConstants.TYPE_KEY + "='%s']["
218                 + XmlMappingConstants.NAME_KEY + "='%s']";
219
220         // TODO unify with xpath in RuntimeRpc
221
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'\\]";
228
229         private static final String BLUEPRINTR = "/" + XmlMappingConstants.MODULES_KEY + "/"
230                 + XmlMappingConstants.MODULE_KEY + "\\[" + XmlMappingConstants.TYPE_KEY + "='%s'\\]\\["
231                 + XmlMappingConstants.NAME_KEY + "='%s'\\]";
232
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, "(.+)", "(.+)"));
236
237         @Override
238         public String toString() {
239             return String.format(BLUEPRINT, moduleName, instanceName);
240         }
241
242         @Override
243         public int hashCode() {
244             final int prime = 31;
245             int result = 1;
246             result = prime * result + (instanceName == null ? 0 : instanceName.hashCode());
247             result = prime * result + (moduleName == null ? 0 : moduleName.hashCode());
248             return result;
249         }
250
251         @Override
252         public boolean equals(final Object obj) {
253             if (this == obj) {
254                 return true;
255             }
256             if (obj == null) {
257                 return false;
258             }
259             if (getClass() != obj.getClass()) {
260                 return false;
261             }
262             ServiceInstance other = (ServiceInstance) obj;
263             if (instanceName == null) {
264                 if (other.instanceName != null) {
265                     return false;
266                 }
267             } else if (!instanceName.equals(other.instanceName)) {
268                 return false;
269             }
270             if (moduleName == null) {
271                 if (other.moduleName != null) {
272                     return false;
273                 }
274             } else if (!moduleName.equals(other.moduleName)) {
275                 return false;
276             }
277             return true;
278         }
279
280         public ObjectName getObjectName(final String transactionName) {
281             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
282         }
283
284         public static ServiceInstance fromObjectName(final ObjectName on) {
285             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
286         }
287     }
288 }