Release version 2.1.1 and roll versions
[sdnc/northbound.git] / vnfapi / provider / src / test / java / org / onap / sdnc / vnfapi / util / MDSALUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdnc.vnfapi.util;
23
24 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfTopologyOperationInputBuilder;
25 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfTopologyOperationOutputBuilder;
26 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.sdnc.request.header.SdncRequestHeaderBuilder;
27 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.service.data.ServiceDataBuilder;
28 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.service.information.ServiceInformationBuilder;
29 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.information.VnfInformationBuilder;
30 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.model.infrastructure.VnfListBuilder;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.request.information.VnfRequestInformationBuilder;
32 import org.opendaylight.yangtools.concepts.Builder;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34
35 import java.util.concurrent.Future;
36 import java.util.function.Consumer;
37 import java.util.function.Function;
38
39
40 /**
41  * This uill class provides utility to build yang objects using a recursive syntax that resembles the tree structure
42  * when defining the same yang object in json format.
43  *
44  * For Example
45  * <pre>
46  * {@code
47  * import static org.onap.sdnc.northbound.util.MDSALUtil.*;
48  * VnfTopologyOperationInput input = build(vnfTopologyOperationInput()
49  *                .setServiceInformation(
50  *                        build(serviceInformation()
51  *                                .setServiceId("serviceId: xyz")
52  *                                .setServiceInstanceId("serviceInstanceId: xyz")
53  *                                .setServiceType("serviceType: xyz")
54  *                                .setSubscriberName("subscriberName: xyz")
55  *                        )
56  *                )
57  *                .setVnfRequestInformation(
58  *                        build(vnfRequestInformation()
59  *                                .setVnfId("vnfId: xyz")
60  *                                .setVnfName("vnfName: xyz")//defect if missing
61  *                                .setVnfType("vnfType: xyz")//defect if missing
62  *                        )
63  *                )
64  *                .setSdncRequestHeader(
65  *                        build(sdncRequestHeader()
66  *                          .setSvcAction(SvcAction.Delete)
67  *                        )
68  *                )
69  *        );
70  * );
71  * }
72  * </pre>
73  */
74 public class MDSALUtil {
75
76     public static VnfTopologyOperationInputBuilder vnfTopologyOperationInput(){return new VnfTopologyOperationInputBuilder();}
77     public static VnfTopologyOperationOutputBuilder vnfTopologyOperationOutput(){return new VnfTopologyOperationOutputBuilder();}
78
79     public static ServiceInformationBuilder serviceInformation(){return new ServiceInformationBuilder();}
80     public static VnfRequestInformationBuilder vnfRequestInformation(){return new VnfRequestInformationBuilder();}
81     public static VnfListBuilder vnfList(){return new VnfListBuilder();}
82     public static ServiceDataBuilder serviceData() { return new ServiceDataBuilder();}
83     public static SdncRequestHeaderBuilder sdncRequestHeader(){return new SdncRequestHeaderBuilder();}
84     public static VnfInformationBuilder vnfInformation(){return new VnfInformationBuilder();}
85
86
87     public static <P> P build(Builder<P> b) {
88         return b == null? null :b.build();
89     }
90
91     public static <P,B extends Builder<P>> P build(Function<P,B> builderConstructor,P sourceDataObject){
92         if(sourceDataObject == null){
93             return null;
94         }
95         B bp = builderConstructor.apply(sourceDataObject);
96         return bp.build();
97     }
98
99     public static <P,B extends Builder<P>> P build(Function<P,B> builderConstructor,P sourceDataObject,Consumer<B> builder){
100         if(sourceDataObject == null){
101             return null;
102         }
103         B bp = builderConstructor.apply(sourceDataObject);
104         builder.accept(bp);
105         return bp.build();
106     }
107
108     public static <I,O> O exec(Function<I,Future<RpcResult<O>>> rpc,I rpcParameter,Function<RpcResult<O>,O> rpcResult)  throws Exception {
109         Future<RpcResult<O>> future = rpc.apply(rpcParameter);
110         return rpcResult.apply(future.get());
111     }
112
113 }