2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.sdnc.northbound.util;
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;
44 import java.util.concurrent.Future;
45 import java.util.function.Function;
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.
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)
62 * .setRequestInformation(build(requestInformation()
63 * .setRequestId("request-id: xyz")
64 * .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
66 * .setServiceInformation(build(serviceInformationBuilder()
67 * .setServiceInstanceId("service-instance-id: xyz")
73 public class MDSALUtil {
75 public static ServiceTopologyOperationInputBuilder serviceTopologyOperationInput() {
76 return new ServiceTopologyOperationInputBuilder();
80 public static ServiceTopologyOperationOutputBuilder serviceTopologyOperationOutput(){
81 return new ServiceTopologyOperationOutputBuilder();
85 public static SdncRequestHeaderBuilder sdncRequestHeader() {
86 return new SdncRequestHeaderBuilder();
90 public static RequestInformationBuilder requestInformation() {
91 return new RequestInformationBuilder();
94 public static ServiceResponseInformationBuilder serviceResponseInformation(){
95 return new ServiceResponseInformationBuilder();
98 public static ServiceInformationBuilder serviceInformationBuilder() {
99 return new ServiceInformationBuilder();
103 public static ServiceBuilder service(){return new ServiceBuilder();}
106 public static ServiceDataBuilder serviceData(){return new ServiceDataBuilder();}
109 public static ServiceStatusBuilder serviceStatus(){return new ServiceStatusBuilder();}
111 public static <P> P build(Builder<P> b) {
112 return b == null? null :b.build();
115 public static <O> O result(Future<RpcResult<O>> future, Function<RpcResult<O>,O> function) throws Exception {
116 return function.apply(future.get());
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());
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()){
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();
142 (new ServiceBuilder(service))
143 .setServiceStatus(build(
144 service.getServiceStatus() == null ? null : new ServiceStatusBuilder(service.getServiceStatus())
146 .setServiceData(build(
147 service.getServiceData() == null ? null : new ServiceDataBuilder(service.getServiceData())