a973e677394dfef51d456043567e3511986b6762
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingToNormalizedNodeCodec.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import java.util.Iterator;
13 import java.util.Map.Entry;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
16 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
17 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
18 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
20 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
27 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
30
31 public class BindingToNormalizedNodeCodec implements SchemaContextListener,AutoCloseable {
32
33     private final BindingIndependentMappingService bindingToLegacy;
34     private final BindingNormalizedNodeCodecRegistry codecRegistry;
35     private DataNormalizer legacyToNormalized;
36     private final GeneratedClassLoadingStrategy classLoadingStrategy;
37
38     public BindingToNormalizedNodeCodec(final GeneratedClassLoadingStrategy classLoadingStrategy, final BindingIndependentMappingService mappingService, final BindingNormalizedNodeCodecRegistry codecRegistry) {
39         super();
40         this.bindingToLegacy = mappingService;
41         this.classLoadingStrategy = classLoadingStrategy;
42         this.codecRegistry = codecRegistry;
43
44     }
45
46     public YangInstanceIdentifier toNormalized(final InstanceIdentifier<? extends DataObject> binding) {
47         return codecRegistry.toYangInstanceIdentifier(binding);
48     }
49
50     @SuppressWarnings({ "unchecked", "rawtypes" })
51     public Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalizedNode(
52             final InstanceIdentifier<? extends DataObject> bindingPath, final DataObject bindingObject) {
53         return codecRegistry.toNormalizedNode((InstanceIdentifier) bindingPath, bindingObject);
54
55     }
56
57     public Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalizedNode(
58             final Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding) {
59         return toNormalizedNode(binding.getKey(),binding.getValue());
60     }
61
62     /**
63      *
64      * Returns a Binding-Aware instance identifier from normalized
65      * instance-identifier if it is possible to create representation.
66      *
67      * Returns Optional.absent for cases where target is mixin node except
68      * augmentation.
69      *
70      */
71     public Optional<InstanceIdentifier<? extends DataObject>> toBinding(final YangInstanceIdentifier normalized)
72                     throws DeserializationException {
73         try {
74             return Optional.<InstanceIdentifier<? extends DataObject>>fromNullable(codecRegistry.fromYangInstanceIdentifier(normalized));
75         } catch (IllegalArgumentException e) {
76             return Optional.absent();
77         }
78     }
79
80     public DataNormalizer getDataNormalizer() {
81         return legacyToNormalized;
82     }
83
84     public Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> toBinding(
85             final @Nonnull Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized)
86                     throws DeserializationException {
87         try {
88             /*
89              * This cast is required, due to generics behaviour in openjdk / oracle javac
90              *
91              * InstanceIdentifier has definition InstanceIdentifier<T extends DataObject>,
92              * this means '?' is always  <? extends DataObject>. Eclipse compiler
93              * is able to determine this relationship and treats
94              * Entry<InstanceIdentifier<?>,DataObject> and Entry<InstanceIdentifier<? extends DataObject,DataObject>
95              * as assignable. However openjdk / oracle javac treats this two types
96              * as incompatible and issues a compile error.
97              *
98              * It is safe to  loose generic information and cast it to other generic signature.
99              *
100              */
101             @SuppressWarnings({ "unchecked", "rawtypes" })
102             final Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = (Entry) codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue());
103             return Optional.fromNullable(binding);
104         } catch (IllegalArgumentException e) {
105             return Optional.absent();
106         }
107     }
108
109     @Override
110     public void onGlobalContextUpdated(final SchemaContext arg0) {
111         legacyToNormalized = new DataNormalizer(arg0);
112         codecRegistry.onBindingRuntimeContextUpdated(BindingRuntimeContext.create(classLoadingStrategy, arg0));
113     }
114
115     public <T extends DataObject> Function<Optional<NormalizedNode<?, ?>>, Optional<T>>  deserializeFunction(final InstanceIdentifier<T> path) {
116         return codecRegistry.deserializeFunction(path);
117     }
118
119     /**
120      * Returns an default object according to YANG schema for supplied path.
121      *
122      * @param path DOM Path
123      * @return Node with defaults set on.
124      */
125     public NormalizedNode<?, ?> getDefaultNodeFor(final YangInstanceIdentifier path) {
126         Iterator<PathArgument> iterator = path.getPathArguments().iterator();
127         DataNormalizationOperation<?> currentOp = legacyToNormalized.getRootOperation();
128         while (iterator.hasNext()) {
129             PathArgument currentArg = iterator.next();
130             try {
131                 currentOp = currentOp.getChild(currentArg);
132             } catch (DataNormalizationException e) {
133                 throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e);
134             }
135         }
136         return currentOp.createDefault(path.getLastPathArgument());
137     }
138
139     public BindingIndependentMappingService getLegacy() {
140         return bindingToLegacy;
141     }
142
143     public BindingNormalizedNodeCodecRegistry getCodecRegistry() {
144         return codecRegistry;
145     }
146
147     @Override
148     public void close() {
149         // NOOP Intentionally
150     }
151 }

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