2 * Copyright (c) 2014 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
8 package org.opendaylight.netconf.sal.restconf.impl;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.netconf.sal.rest.impl.StringModuleInstanceIdentifierCodec;
19 import org.opendaylight.restconf.common.util.IdentityValuesDTO;
20 import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
21 import org.opendaylight.restconf.common.util.IdentityValuesDTO.Predicate;
22 import org.opendaylight.restconf.common.util.RestUtil;
23 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
32 import org.opendaylight.yangtools.yang.data.api.codec.InstanceIdentifierCodec;
33 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
34 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public final class RestCodec {
49 private static final Logger LOG = LoggerFactory.getLogger(RestCodec.class);
54 // FIXME: IllegalArgumentCodec is not quite accurate
55 public static IllegalArgumentCodec<Object, Object> from(final TypeDefinition<?> typeDefinition,
56 final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
57 return new ObjectCodec(typeDefinition, mountPoint, controllerContext);
60 @SuppressWarnings("rawtypes")
61 public static final class ObjectCodec implements IllegalArgumentCodec<Object, Object> {
63 private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
65 public static final IllegalArgumentCodec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
67 private final ControllerContext controllerContext;
68 private final IllegalArgumentCodec instanceIdentifier;
69 private final IllegalArgumentCodec identityrefCodec;
71 private final TypeDefinition<?> type;
73 private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
74 final ControllerContext controllerContext) {
75 this.controllerContext = controllerContext;
76 this.type = RestUtil.resolveBaseTypeFrom(typeDefinition);
77 if (this.type instanceof IdentityrefTypeDefinition) {
78 this.identityrefCodec = new IdentityrefCodecImpl(mountPoint, controllerContext);
80 this.identityrefCodec = null;
82 if (this.type instanceof InstanceIdentifierTypeDefinition) {
83 this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, controllerContext);
85 this.instanceIdentifier = null;
89 @SuppressWarnings("unchecked")
91 public Object deserialize(final Object input) {
93 if (this.type instanceof IdentityrefTypeDefinition) {
94 if (input instanceof IdentityValuesDTO) {
95 return this.identityrefCodec.deserialize(input);
97 if (LOG.isDebugEnabled()) {
99 "Value is not instance of IdentityrefTypeDefinition but is {}. "
100 + "Therefore NULL is used as translation of - {}",
101 input == null ? "null" : input.getClass(), String.valueOf(input));
104 } else if (this.type instanceof InstanceIdentifierTypeDefinition) {
105 if (input instanceof IdentityValuesDTO) {
106 return this.instanceIdentifier.deserialize(input);
108 final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
109 controllerContext.getGlobalSchema());
110 return codec.deserialize((String) input);
113 final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
114 TypeDefinitionAwareCodec.from(this.type);
115 if (typeAwarecodec != null) {
116 if (input instanceof IdentityValuesDTO) {
117 return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
119 return typeAwarecodec.deserialize(String.valueOf(input));
121 LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
125 } catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
126 LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
131 @SuppressWarnings("unchecked")
133 public Object serialize(final Object input) {
135 if (this.type instanceof IdentityrefTypeDefinition) {
136 return this.identityrefCodec.serialize(input);
137 } else if (this.type instanceof LeafrefTypeDefinition) {
138 return LEAFREF_DEFAULT_CODEC.serialize(input);
139 } else if (this.type instanceof InstanceIdentifierTypeDefinition) {
140 return this.instanceIdentifier.serialize(input);
142 final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
143 TypeDefinitionAwareCodec.from(this.type);
144 if (typeAwarecodec != null) {
145 return typeAwarecodec.serialize(input);
147 LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
151 } catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
152 LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
159 public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
161 private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
163 private final DOMMountPoint mountPoint;
164 private final ControllerContext controllerContext;
166 public IdentityrefCodecImpl(final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
167 this.mountPoint = mountPoint;
168 this.controllerContext = controllerContext;
172 public IdentityValuesDTO serialize(final QName data) {
173 return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), null, null);
177 public QName deserialize(final IdentityValuesDTO data) {
178 final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
179 final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
181 if (module == null) {
182 LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace());
183 LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace));
187 return QName.create(module.getNamespace(), module.getRevision(), valueWithNamespace.getValue());
192 public static class LeafrefCodecImpl implements LeafrefCodec<String> {
195 public String serialize(final Object data) {
196 return String.valueOf(data);
200 public Object deserialize(final String data) {
206 public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec<IdentityValuesDTO> {
207 private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class);
209 private final DOMMountPoint mountPoint;
210 private final ControllerContext controllerContext;
212 public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint,
213 final ControllerContext controllerContext) {
214 this.mountPoint = mountPoint;
215 this.controllerContext = controllerContext;
219 public IdentityValuesDTO serialize(final YangInstanceIdentifier data) {
220 final IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO();
221 for (final PathArgument pathArgument : data.getPathArguments()) {
222 final IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType());
223 if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) {
224 final List<Predicate> predicates =
225 keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument).entrySet());
226 identityValue.setPredicates(predicates);
227 } else if (pathArgument instanceof NodeWithValue && identityValue != null) {
228 final List<Predicate> predicates = new ArrayList<>();
229 final String value = String.valueOf(((NodeWithValue<?>) pathArgument).getValue());
230 predicates.add(new Predicate(null, value));
231 identityValue.setPredicates(predicates);
233 identityValuesDTO.add(identityValue);
235 return identityValuesDTO;
238 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
239 justification = "Unrecognised NullableDecl")
241 public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) {
242 final List<PathArgument> result = new ArrayList<>();
243 final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
244 final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
246 if (module == null) {
247 LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.",
248 valueWithNamespace.getNamespace());
249 LOG.info("Instance-identifier will be translated as NULL for data - {}",
250 String.valueOf(valueWithNamespace.getValue()));
254 DataNodeContainer parentContainer = module;
255 final List<IdentityValue> identities = data.getValuesWithNamespaces();
256 for (int i = 0; i < identities.size(); i++) {
257 final IdentityValue identityValue = identities.get(i);
258 XMLNamespace validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint,
260 final DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace(
261 parentContainer, identityValue.getValue(), validNamespace);
263 LOG.info("'{}' node was not found in {}", identityValue, parentContainer.getChildNodes());
264 LOG.info("Instance-identifier will be translated as NULL for data - {}",
265 String.valueOf(identityValue.getValue()));
268 final QName qName = node.getQName();
269 PathArgument pathArgument = null;
270 if (identityValue.getPredicates().isEmpty()) {
271 pathArgument = new NodeIdentifier(qName);
273 if (node instanceof LeafListSchemaNode) { // predicate is value of leaf-list entry
274 final Predicate leafListPredicate = identityValue.getPredicates().get(0);
275 if (!leafListPredicate.isLeafList()) {
276 LOG.info("Predicate's data is not type of leaf-list. It should be in format \".='value'\"");
277 LOG.info("Instance-identifier will be translated as NULL for data - {}",
278 String.valueOf(identityValue.getValue()));
281 pathArgument = new NodeWithValue<>(qName, leafListPredicate.getValue());
282 } else if (node instanceof ListSchemaNode) { // predicates are keys of list
283 final DataNodeContainer listNode = (DataNodeContainer) node;
284 final Map<QName, Object> predicatesMap = new HashMap<>();
285 for (final Predicate predicate : identityValue.getPredicates()) {
286 validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint,
288 final DataSchemaNode listKey = ControllerContext
289 .findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(),
291 predicatesMap.put(listKey.getQName(), predicate.getValue());
293 pathArgument = NodeIdentifierWithPredicates.of(qName, predicatesMap);
295 LOG.info("Node {} is not List or Leaf-list.", node);
296 LOG.info("Instance-identifier will be translated as NULL for data - {}",
297 String.valueOf(identityValue.getValue()));
301 result.add(pathArgument);
302 if (i < identities.size() - 1) { // last element in instance-identifier can be other than
304 if (node instanceof DataNodeContainer) {
305 parentContainer = (DataNodeContainer) node;
307 LOG.info("Node {} isn't instance of DataNodeContainer", node);
308 LOG.info("Instance-identifier will be translated as NULL for data - {}",
309 String.valueOf(identityValue.getValue()));
315 return result.isEmpty() ? null : YangInstanceIdentifier.create(result);
318 private static List<Predicate> keyValuesToPredicateList(final Set<Entry<QName, Object>> keyValues) {
319 final List<Predicate> result = new ArrayList<>();
320 for (final Entry<QName, Object> entry : keyValues) {
321 final QName qualifiedName = entry.getKey();
322 final Object value = entry.getValue();
323 result.add(new Predicate(qNameToIdentityValue(qualifiedName), String.valueOf(value)));
328 private static IdentityValue qNameToIdentityValue(final QName qualifiedName) {
329 if (qualifiedName != null) {
330 return new IdentityValue(qualifiedName.getNamespace().toString(), qualifiedName.getLocalName());
336 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
337 justification = "https://github.com/spotbugs/spotbugs/issues/811")
338 private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint,
339 final ControllerContext controllerContext) {
340 final XMLNamespace validNamespace = resolveValidNamespace(namespace, mountPoint, controllerContext);
342 Module module = null;
343 if (mountPoint != null) {
344 module = controllerContext.findModuleByNamespace(mountPoint, validNamespace);
346 module = controllerContext.findModuleByNamespace(validNamespace);
348 if (module == null) {
349 LOG.info("Module for namespace {} was not found.", validNamespace);
355 private static XMLNamespace resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
356 final ControllerContext controllerContext) {
357 XMLNamespace validNamespace;
358 if (mountPoint != null) {
359 validNamespace = controllerContext.findNamespaceByModuleName(mountPoint, namespace);
361 validNamespace = controllerContext.findNamespaceByModuleName(namespace);
363 if (validNamespace == null) {
364 validNamespace = XMLNamespace.of(namespace);
367 return validNamespace;