summaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/openecomp/mso/yangDecoder/transform/impl/YangDataTransformJava2NNServiceImpl.java
blob: ba82aa5539a3f563e8a33d2801949cf9107a8933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.openecomp.mso.yangDecoder.transform.impl;

//import org.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;

import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.restconf.impl.WriterParameters;
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.*;

import java.util.Map;

/**
 * Created by Administrator on 2017/3/17.
 */
public class YangDataTransformJava2NNServiceImpl {
    BindingNormalizedNodeSerializer mappingService;
    public YangDataTransformJava2NNServiceImpl(BindingNormalizedNodeSerializer mappingService)
    {
        this.mappingService=mappingService;
    }
    public  <T extends DataObject>  NormalizedNodeContext yangDataObjecttoNNC(InstanceIdentifier<T> identifier, String uri, T dobj)
    {
        final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
        Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
        WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
        aa.setPrettyPrint(true);
        return new NormalizedNodeContext( iiContext, temp.getValue() ,aa.build());
    }
    public  <T extends DataObject> NormalizedNode yangDataObjecttoNN( InstanceIdentifier<T> identifier,T dobj) {
        Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
        if (null == temp) {
            return null;
        } else {
            return temp.getValue();
        }
    }
    public NormalizedNodeContext yangNNtoNNC(NormalizedNode nn, String uri){
        final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
        WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
        aa.setPrettyPrint(true);
        return new NormalizedNodeContext(iiContext, nn,aa.build());
    }
    public  <T extends DataObject> T yangDataObjectfromNN(YangInstanceIdentifier identifier,NormalizedNode node) {
        Map.Entry<InstanceIdentifier<?>, DataObject> temp = mappingService.fromNormalizedNode(identifier, node);
        if (null == temp) {
            return null;
        } else {
            return (T) temp.getValue();
        }
    }
    public  <T extends DataObject> T yangDataObjectfromNNC(NormalizedNodeContext nnc)
    {
        return yangDataObjectfromNN(nnc.getInstanceIdentifierContext().getInstanceIdentifier(),nnc.getData());
    }
    public ContainerNode yangRpcDatatoNN(final DataContainer rpcdata){
        return mappingService.toNormalizedNodeRpcData(rpcdata);
    }
    public DataObject yangRpcDatafromNN(SchemaPath path, final ContainerNode data)
    {
        return mappingService.fromNormalizedNodeRpcData(path,data);
    }
    public static DataSchemaNode findDataSchemaNode(String uriPath,String inputoroutput)
    {
        final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
        DataSchemaNode dsn=JsonParserStream.getWrapSchemaNode( iicontext.getSchemaNode());
        return dsn;
    }
    public static SchemaNode findSchemaNode(final InstanceIdentifierContext<?> iicontext ,String inputoroutput)
    {
        SchemaNode schemaNode;

        final SchemaNode schemaNode0 = iicontext.getSchemaNode();
        if (schemaNode0 instanceof RpcDefinition) {
            if (inputoroutput.contains("output")) {
                schemaNode = ((RpcDefinition) schemaNode0).getOutput();
            } else {
                schemaNode = ((RpcDefinition) schemaNode0).getInput();
            }

        } else if(schemaNode0 instanceof NotificationDefinition)
        {
            schemaNode=schemaNode0;
        }
        else if (schemaNode0 instanceof DataSchemaNode) {
            schemaNode =  schemaNode0;
        } else {
            throw new IllegalStateException("Unknow SchemaNode");
        }
        return schemaNode;
    }

    public static DataSchemaNode findRpcSchemaNode(final InstanceIdentifierContext<?> iicontext,String inputoroutput)
    {
        DataSchemaNode schemaNode;
        final SchemaNode schemaNode0 = iicontext.getSchemaNode();
        boolean isInput = false;
        if (schemaNode0 instanceof RpcDefinition) {
            if (inputoroutput.contains("output")) {
                schemaNode = ((RpcDefinition) schemaNode0).getOutput();
                isInput = false;
            } else {
                schemaNode = ((RpcDefinition) schemaNode0).getInput();
                isInput = true;
            }

        } else if (schemaNode0 instanceof DataSchemaNode) {
            schemaNode = (DataSchemaNode) schemaNode0;
        } else {
            throw new IllegalStateException("Unknow SchemaNode");
        }
        return   schemaNode;
    }
    public DataObject yangRpcDatafromNN(final InstanceIdentifierContext<?> iicontext , final NormalizedNode data) {
       // final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
        ContainerNode contn= (ContainerNode)data;
        DataSchemaNode schemaNode= findRpcSchemaNode(iicontext,contn.getNodeType().getLocalName());;
    /*    final SchemaNode schemaNode0 = iicontext.getSchemaNode();
        boolean isInput = false;
        if (schemaNode0 instanceof RpcDefinition) {
            if (contn.getNodeType().getLocalName().contains("output")) {
                schemaNode = ((RpcDefinition) schemaNode0).getOutput();
                isInput = false;
            } else {
                schemaNode = ((RpcDefinition) schemaNode0).getInput();
                isInput = true;
            }

        } else if (schemaNode0 instanceof DataSchemaNode) {
            schemaNode = (DataSchemaNode) schemaNode0;
        } else {
            throw new IllegalStateException("Unknow SchemaNode");
        }*/
        SchemaPath path=schemaNode.getPath();
        return  mappingService.fromNormalizedNodeRpcData(path,contn);
    }
}