4d567676bc4d8c277b9fcca8f90200d0d4156a73
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.yangDecoder.transform.impl;
22
23 //import org.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;
24
25 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
26 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.netconf.sal.restconf.impl.WriterParameters;
29 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.yangtools.yang.binding.DataContainer;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.*;
37
38 import java.util.Map;
39
40 /**
41  * Created by Administrator on 2017/3/17.
42  */
43 public class YangDataTransformJava2NNServiceImpl {
44     BindingNormalizedNodeSerializer mappingService;
45     public YangDataTransformJava2NNServiceImpl(BindingNormalizedNodeSerializer mappingService)
46     {
47         this.mappingService=mappingService;
48     }
49     public  <T extends DataObject>  NormalizedNodeContext yangDataObjecttoNNC(InstanceIdentifier<T> identifier, String uri, T dobj)
50     {
51         final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
52         Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
53         WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
54         aa.setPrettyPrint(true);
55         return new NormalizedNodeContext( iiContext, temp.getValue() ,aa.build());
56     }
57     public  <T extends DataObject> NormalizedNode yangDataObjecttoNN( InstanceIdentifier<T> identifier,T dobj) {
58         Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
59         if (null == temp) {
60             return null;
61         } else {
62             return temp.getValue();
63         }
64     }
65     public NormalizedNodeContext yangNNtoNNC(NormalizedNode nn, String uri){
66         final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
67         WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
68         aa.setPrettyPrint(true);
69         return new NormalizedNodeContext(iiContext, nn,aa.build());
70     }
71     public  <T extends DataObject> T yangDataObjectfromNN(YangInstanceIdentifier identifier,NormalizedNode node) {
72         Map.Entry<InstanceIdentifier<?>, DataObject> temp = mappingService.fromNormalizedNode(identifier, node);
73         if (null == temp) {
74             return null;
75         } else {
76             return (T) temp.getValue();
77         }
78     }
79     public  <T extends DataObject> T yangDataObjectfromNNC(NormalizedNodeContext nnc)
80     {
81         return yangDataObjectfromNN(nnc.getInstanceIdentifierContext().getInstanceIdentifier(),nnc.getData());
82     }
83     public ContainerNode yangRpcDatatoNN(final DataContainer rpcdata){
84         return mappingService.toNormalizedNodeRpcData(rpcdata);
85     }
86     public DataObject yangRpcDatafromNN(SchemaPath path, final ContainerNode data)
87     {
88         return mappingService.fromNormalizedNodeRpcData(path,data);
89     }
90     public static DataSchemaNode findDataSchemaNode(String uriPath,String inputoroutput)
91     {
92         final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
93         DataSchemaNode dsn=JsonParserStream.getWrapSchemaNode( iicontext.getSchemaNode());
94         return dsn;
95     }
96     public static SchemaNode findSchemaNode(final InstanceIdentifierContext<?> iicontext ,String inputoroutput)
97     {
98         SchemaNode schemaNode;
99
100         final SchemaNode schemaNode0 = iicontext.getSchemaNode();
101         if (schemaNode0 instanceof RpcDefinition) {
102             if (inputoroutput.contains("output")) {
103                 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
104             } else {
105                 schemaNode = ((RpcDefinition) schemaNode0).getInput();
106             }
107
108         } else if(schemaNode0 instanceof NotificationDefinition)
109         {
110             schemaNode=schemaNode0;
111         }
112         else if (schemaNode0 instanceof DataSchemaNode) {
113             schemaNode =  schemaNode0;
114         } else {
115             throw new IllegalStateException("Unknow SchemaNode");
116         }
117         return schemaNode;
118     }
119
120     public static DataSchemaNode findRpcSchemaNode(final InstanceIdentifierContext<?> iicontext,String inputoroutput)
121     {
122         DataSchemaNode schemaNode;
123         final SchemaNode schemaNode0 = iicontext.getSchemaNode();
124         boolean isInput = false;
125         if (schemaNode0 instanceof RpcDefinition) {
126             if (inputoroutput.contains("output")) {
127                 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
128                 isInput = false;
129             } else {
130                 schemaNode = ((RpcDefinition) schemaNode0).getInput();
131                 isInput = true;
132             }
133
134         } else if (schemaNode0 instanceof DataSchemaNode) {
135             schemaNode = (DataSchemaNode) schemaNode0;
136         } else {
137             throw new IllegalStateException("Unknow SchemaNode");
138         }
139         return   schemaNode;
140     }
141     public DataObject yangRpcDatafromNN(final InstanceIdentifierContext<?> iicontext , final NormalizedNode data) {
142        // final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
143         ContainerNode contn= (ContainerNode)data;
144         DataSchemaNode schemaNode= findRpcSchemaNode(iicontext,contn.getNodeType().getLocalName());;
145     /*    final SchemaNode schemaNode0 = iicontext.getSchemaNode();
146         boolean isInput = false;
147         if (schemaNode0 instanceof RpcDefinition) {
148             if (contn.getNodeType().getLocalName().contains("output")) {
149                 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
150                 isInput = false;
151             } else {
152                 schemaNode = ((RpcDefinition) schemaNode0).getInput();
153                 isInput = true;
154             }
155
156         } else if (schemaNode0 instanceof DataSchemaNode) {
157             schemaNode = (DataSchemaNode) schemaNode0;
158         } else {
159             throw new IllegalStateException("Unknow SchemaNode");
160         }*/
161         SchemaPath path=schemaNode.getPath();
162         return  mappingService.fromNormalizedNodeRpcData(path,contn);
163     }
164 }