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

©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.