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