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