Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / LazySerializedDOMRpcResult.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 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import java.util.Collection;
11 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
12 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
13 import org.opendaylight.yangtools.yang.binding.DataContainer;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 abstract class LazySerializedDOMRpcResult implements DOMRpcResult {
20
21     private final RpcResult<?> bindingResult;
22
23     LazySerializedDOMRpcResult(final RpcResult<?> bindingResult) {
24         this.bindingResult = bindingResult;
25     }
26
27     static final LazySerializedDOMRpcResult create(final RpcResult<?> bindingResult, final BindingNormalizedNodeCodecRegistry codec) {
28         final Object resultData = bindingResult.getResult();
29
30         if(resultData instanceof DataObject) {
31             return new DataResult(bindingResult,codec);
32
33
34         }
35         return new EmptyDataResult(bindingResult);
36     }
37
38     RpcResult<?> bidningRpcResult() {
39         return bindingResult;
40     }
41
42     @Override
43     public Collection<RpcError> getErrors() {
44         return bindingResult.getErrors();
45     }
46
47
48     private static final class DataResult extends LazySerializedDOMRpcResult {
49
50         private final BindingNormalizedNodeCodecRegistry codec;
51         private NormalizedNode<?, ?> domData;
52
53         public DataResult(final RpcResult<?> bindingResult, final BindingNormalizedNodeCodecRegistry codec) {
54             super(bindingResult);
55             this.codec = codec;
56         }
57
58         @Override
59         public NormalizedNode<?, ?> getResult() {
60             if(domData == null) {
61                 domData = codec.toNormalizedNodeRpcData((DataContainer) bidningRpcResult().getResult());
62             }
63             return domData;
64         }
65     }
66
67
68     private static final class EmptyDataResult extends LazySerializedDOMRpcResult {
69
70         public EmptyDataResult(final RpcResult<?> bindingResult) {
71             super(bindingResult);
72         }
73
74         @Override
75         public NormalizedNode<?, ?> getResult() {
76             // FIXME: Should we return something else?
77             return null;
78         }
79
80     }
81
82
83
84 }

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