6586c8e340bccc746d0ac5853765c03b72563250
[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 com.google.common.base.Optional;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInputBuilder;
29 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutputBuilder;
30 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.Services;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformationBuilder;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeaderBuilder;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformationBuilder;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.ServiceBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.ServiceKey;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.response.information.ServiceResponseInformationBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatusBuilder;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43
44 import java.util.concurrent.Future;
45 import java.util.function.Function;
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 ServiceTopologyOperationInputBuilder serviceTopologyOperationInput() {
76         return new ServiceTopologyOperationInputBuilder();
77     }
78
79
80     public static ServiceTopologyOperationOutputBuilder serviceTopologyOperationOutput(){
81         return new ServiceTopologyOperationOutputBuilder();
82     }
83
84
85     public static SdncRequestHeaderBuilder sdncRequestHeader() {
86         return new SdncRequestHeaderBuilder();
87     }
88
89
90     public static RequestInformationBuilder requestInformation() {
91         return new RequestInformationBuilder();
92     }
93
94     public static ServiceResponseInformationBuilder serviceResponseInformation(){
95         return  new ServiceResponseInformationBuilder();
96     }
97
98     public static ServiceInformationBuilder serviceInformationBuilder() {
99         return  new ServiceInformationBuilder();
100     }
101
102
103     public static ServiceBuilder service(){return new ServiceBuilder();}
104
105
106     public static ServiceDataBuilder serviceData(){return new ServiceDataBuilder();}
107
108
109     public static ServiceStatusBuilder serviceStatus(){return new ServiceStatusBuilder();}
110
111     public static <P> P build(Builder<P> b) {
112         return b == null? null :b.build();
113     }
114
115     public static <O> O result(Future<RpcResult<O>> future, Function<RpcResult<O>,O> function)  throws Exception {
116         return function.apply(future.get());
117     }
118
119     public static <I,O> O rpc(Function<I,Future<RpcResult<O>>> rpc,Function<RpcResult<O>,O> function,I input)  throws Exception {
120         Future<RpcResult<O>> future = rpc.apply(input);
121         return function.apply(future.get());
122     }
123
124
125
126     /** @return Service - the Service object read from the DataBroker or null if none was found */
127     public static Service read(DataBroker dataBroker,String serviceKey, LogicalDatastoreType logicalDatastoreType) throws Exception {
128         InstanceIdentifier serviceInstanceIdentifier = InstanceIdentifier.<Services>builder(Services.class)
129                 .child(Service.class, new ServiceKey(serviceKey)).build();
130         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
131         Optional<Service> data = (Optional<Service>) readTx.read(logicalDatastoreType, serviceInstanceIdentifier).get();
132         if(!data.isPresent()){
133             return null;
134         }
135
136
137         //The toString() value from a Service object returned form data.get() is different than the toString() value
138         //from a Service Object constructed from a Builder. This makes it difficult to compare deltas when doing a
139         // assertEquals.  That why we rebuild it her to solve that problem.
140         Service service = data.get();
141         return build(
142                 (new ServiceBuilder(service))
143                         .setServiceStatus(build(
144                                 service.getServiceStatus() == null ? null : new ServiceStatusBuilder(service.getServiceStatus())
145                         ))
146                         .setServiceData(build(
147                                 service.getServiceData() == null ? null : new ServiceDataBuilder(service.getServiceData())
148                         ))
149         );
150     }
151
152
153
154
155
156
157
158 }