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