11610659788beabbebd943137a84a133bd2272d8
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / 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.northbound.util;
23
24 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationInputBuilder;
25 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationOutputBuilder;
26 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInputBuilder;
27 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutputBuilder;
28 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.network.information.NetworkInformationBuilder;
29 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.network.response.information.NetworkResponseInformationBuilder;
30 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformationBuilder;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeaderBuilder;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformationBuilder;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.level.oper.status.ServiceLevelOperStatusBuilder;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.ServiceBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.response.information.ServiceResponseInformationBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatusBuilder;
38 import org.opendaylight.yangtools.concepts.Builder;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40
41 import java.util.concurrent.Future;
42 import java.util.function.Consumer;
43 import java.util.function.Function;
44
45
46 /**
47  * This uill class provides utility to build yang objects using a recursive syntax that resembles the tree structure
48  * when defining the same yang object in json format.
49  *
50  * For Example
51  * <pre>
52  * {@code
53  * import static org.onap.sdnc.northbound.util.MDSALUtil.*;
54  * ServiceTopologyOperationInput input = build(
55  *         serviceTopologyOperationInput()
56  *                 .setSdncRequestHeader(build(sdncRequestHeader()
57  *                         .setSvcRequestId("svc-request-id: xyz")
58  *                         .setSvcAction(SvcAction.Assign)
59  *                 ))
60  *                 .setRequestInformation(build(requestInformation()
61  *                         .setRequestId("request-id: xyz")
62  *                        .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
63  *                 ))
64  *                .setServiceInformation(build(serviceInformationBuilder()
65  *                         .setServiceInstanceId("service-instance-id: xyz")
66  *                ))
67  * );
68  * }
69  * </pre>
70  */
71 public class MDSALUtil {
72
73     public static ServiceTopologyOperationInputBuilder serviceTopologyOperationInput() {
74         return new ServiceTopologyOperationInputBuilder();
75     }
76
77
78     public static ServiceTopologyOperationOutputBuilder serviceTopologyOperationOutput(){
79         return new ServiceTopologyOperationOutputBuilder();
80     }
81
82
83     public static SdncRequestHeaderBuilder sdncRequestHeader() {
84         return new SdncRequestHeaderBuilder();
85     }
86
87
88     public static RequestInformationBuilder requestInformation() {
89         return new RequestInformationBuilder();
90     }
91
92     public static ServiceResponseInformationBuilder serviceResponseInformation(){
93         return  new ServiceResponseInformationBuilder();
94     }
95
96     public static ServiceInformationBuilder serviceInformationBuilder() {
97         return  new ServiceInformationBuilder();
98     }
99
100
101     public static ServiceBuilder service(){return new ServiceBuilder();}
102
103
104     public static ServiceDataBuilder serviceData(){return new ServiceDataBuilder();}
105
106
107     public static ServiceStatusBuilder serviceStatus(){return new ServiceStatusBuilder();}
108
109     public static NetworkInformationBuilder networkInformation(){return new NetworkInformationBuilder();}
110
111     public static NetworkTopologyOperationInputBuilder networkTopologyOperationInput() {return new NetworkTopologyOperationInputBuilder();}
112
113     public static NetworkTopologyOperationOutputBuilder networkTopologyOperationOutput() {return new NetworkTopologyOperationOutputBuilder();}
114
115     public static NetworkResponseInformationBuilder networkResponseInformation(){return new NetworkResponseInformationBuilder();}
116
117     public static ServiceLevelOperStatusBuilder serviceLevelOperStatus() {return new ServiceLevelOperStatusBuilder();}
118
119     public static <P> P build(Builder<P> b) {
120         return b == null? null :b.build();
121     }
122
123     public static <P,B extends Builder<P>> P build(Function<P,B> builderConstructor,P sourceDataObject){
124         if(sourceDataObject == null){
125             return null;
126         }
127         B bp = builderConstructor.apply(sourceDataObject);
128         return bp.build();
129     }
130
131     public static <P,B extends Builder<P>> P build(Function<P,B> builderConstructor,P sourceDataObject,Consumer<B> builder){
132         if(sourceDataObject == null){
133             return null;
134         }
135         B bp = builderConstructor.apply(sourceDataObject);
136         builder.accept(bp);
137         return bp.build();
138     }
139
140     public static <I,O> O exec(Function<I,Future<RpcResult<O>>> rpc,I rpcParameter,Function<RpcResult<O>,O> rpcResult)  throws Exception {
141         Future<RpcResult<O>> future = rpc.apply(rpcParameter);
142         return rpcResult.apply(future.get());
143     }
144
145 }