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;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.onap.sdnc.northbound.util.PropBuilder;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInput;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutput;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import static org.junit.Assert.assertEquals;
42 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
43 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
44 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
45 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
46 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
47 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
48 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
49 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
50 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
51 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
52 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
56 * This class test the ServiceTopologyOperation mdsal RPC.
58 @RunWith(MockitoJUnitRunner.class)
59 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
62 final String SVC_OPERATION = "service-topology-operation";
66 public void setUp() throws Exception {
68 svcClient.setScvOperation(SVC_OPERATION);
73 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected
74 * {@link ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
77 public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
80 //mock svcClient to perform a successful execution with the expected parameters
81 svcClient.mockHasGraph(true);
82 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
83 svcClient.mockExecute(svcResultProp);
85 // create the ServiceTopologyOperationInput from the template
86 ServiceTopologyOperationInput serviceTopologyOperationInput = createSTOI();
88 //execute the mdsal exec
89 ServiceTopologyOperationOutput actualServiceTopologyOperationOutput = exec(
90 genericResourceApiProvider::serviceTopologyOperation
91 , serviceTopologyOperationInput
92 , RpcResult::getResult
96 //verify the returned ServiceTopologyOperationOutput
97 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,serviceTopologyOperationInput);
98 assertEquals(expectedServiceTopologyOperationOutput,actualServiceTopologyOperationOutput);
101 //verify the persisted Service
102 Service actualService = db.read(serviceTopologyOperationInput.getServiceInformation().getServiceInstanceId(), LogicalDatastoreType.CONFIGURATION);
103 Service expectedService = createExpectedService(
104 expectedServiceTopologyOperationOutput,
105 serviceTopologyOperationInput,
107 assertEquals(expectedService,actualService);
114 private ServiceTopologyOperationInput createSTOI()
118 serviceTopologyOperationInput()
119 .setSdncRequestHeader(build(sdncRequestHeader()
120 .setSvcRequestId("svc-request-id: xyz")
121 .setSvcAction(SvcAction.Assign)
123 .setRequestInformation(build(requestInformation()
124 .setRequestId("request-id: xyz")
125 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
127 .setServiceInformation(build(serviceInformationBuilder()
128 .setServiceInstanceId("service-instance-id: xyz")
134 private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,ServiceTopologyOperationInput expectedServiceTopologyOperationInput){
136 serviceTopologyOperationOutput()
137 .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
138 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
139 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
140 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
141 .setServiceResponseInformation(build(serviceResponseInformation()
142 .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
143 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
148 private Service createExpectedService(
149 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
150 ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
151 Service actualService
155 //We cannot predict the timeStamp value so just steal it from the actual
156 //we need this to prevent the equals method from returning false as a result of the timestamp
157 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null?
158 null : actualService.getServiceStatus().getResponseTimestamp();
160 SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
161 ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
162 RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
166 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
167 .setServiceData(build(serviceData()))
171 .setAction(expectedRequestInformation.getRequestAction().name())
172 .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
173 .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
174 .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
175 .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
176 .setRpcName(SVC_OPERATION)
177 .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
178 .setResponseTimestamp(responseTimeStamp)
185 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum){
186 return fromEnum == null? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());