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.request.information.RequestInformation.RequestAction;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import static org.junit.Assert.assertEquals;
43 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
44 import static org.onap.sdnc.northbound.util.MDSALUtil.read;
45 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
46 import static org.onap.sdnc.northbound.util.MDSALUtil.rpc;
47 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
48 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
49 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
50 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
51 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
52 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
53 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
54 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
58 * This class test the ServiceTopologyOperation mdsal RPC.
60 @RunWith(MockitoJUnitRunner.class)
61 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
64 final String SVC_OPERATION = "service-topology-operation";
68 public void setUp() throws Exception {
70 svcClient.setScvOperation(SVC_OPERATION);
75 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected
76 * {@link ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
79 public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
82 //mock svcClient to perform a successful execution with the expected parameters
83 svcClient.mockHasGraph(true);
84 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
85 svcClient.mockExecute(svcResultProp);
87 // create the ServiceTopologyOperationInput from the template
88 ServiceTopologyOperationInput serviceTopologyOperationInput = createSTOI(RequestAction.CreateServiceInstance);
90 //execute the mdsal rpc
91 ServiceTopologyOperationOutput actualServiceTopologyOperationOutput = rpc(
92 genericResourceApiProvider::serviceTopologyOperation
93 , RpcResult::getResult
94 , serviceTopologyOperationInput
98 //verify the returned ServiceTopologyOperationOutput
99 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,serviceTopologyOperationInput);
100 assertEquals(expectedServiceTopologyOperationOutput,actualServiceTopologyOperationOutput);
103 //verify the persisted Service
104 Service actualService = read(dataBroker,serviceTopologyOperationInput.getServiceInformation().getServiceInstanceId(), LogicalDatastoreType.CONFIGURATION);
105 Service expectedService = createExpectedService(
106 expectedServiceTopologyOperationOutput,
107 serviceTopologyOperationInput,
109 assertEquals(expectedService,actualService);
116 private ServiceTopologyOperationInput createSTOI(RequestAction requestAction)
120 serviceTopologyOperationInput()
121 .setSdncRequestHeader(build(sdncRequestHeader()
122 .setSvcRequestId("svc-request-id: xyz")
123 .setSvcAction(SvcAction.Assign)
125 .setRequestInformation(build(requestInformation()
126 .setRequestId("request-id: xyz")
127 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
129 .setServiceInformation(build(serviceInformationBuilder()
130 .setServiceInstanceId("service-instance-id: xyz")
136 private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,ServiceTopologyOperationInput expectedServiceTopologyOperationInput){
138 serviceTopologyOperationOutput()
139 .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
140 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
141 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
142 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
143 .setServiceResponseInformation(build(serviceResponseInformation()
144 .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
145 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
150 private Service createExpectedService(
151 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
152 ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
153 Service actualService
157 //We cannot predict the timeStamp value so just steal it from the actual
158 //we need this to prevent the equals method from returning false as a result of the timestamp
159 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null?
160 null : actualService.getServiceStatus().getResponseTimestamp();
162 SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
163 ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
164 RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
168 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
169 .setServiceData(build(serviceData()))
173 .setAction(expectedRequestInformation.getRequestAction().name())
174 .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
175 .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
176 .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
177 .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
178 .setRpcName(SVC_OPERATION)
179 .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
180 .setResponseTimestamp(responseTimeStamp)
187 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum){
188 return fromEnum == null? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());