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.NetworkTopologyOperationInput;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationOutput;
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.data.ServiceData;
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 java.time.Instant;
44 import static org.junit.Assert.assertEquals;
45 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
46 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
47 import static org.onap.sdnc.northbound.util.MDSALUtil.networkInformation;
48 import static org.onap.sdnc.northbound.util.MDSALUtil.networkResponseInformation;
49 import static org.onap.sdnc.northbound.util.MDSALUtil.networkTopologyOperationInput;
50 import static org.onap.sdnc.northbound.util.MDSALUtil.networkTopologyOperationOutput;
51 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
52 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
53 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
54 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
55 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
56 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceLevelOperStatus;
57 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
58 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
59 import static org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastAction;
60 import static org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastOrderStatus;
61 import static org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastRpcAction;
62 import static org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.OrderStatus;
66 * This class test the NetworkTopologyOperation mdsal RPC.
68 @RunWith(MockitoJUnitRunner.class)
69 public class NetworkTopologyOperationRPCTest extends GenericResourceApiProviderTest {
72 final String SVC_OPERATION = "network-topology-operation";
76 public void setUp() throws Exception {
78 svcClient.setScvOperation(SVC_OPERATION);
83 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected
84 * {@link NetworkTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
87 public void testNetworkTopologyOperation() throws Exception {
89 //mock svcClient to perform a successful execution with the expected parameters
90 svcClient.mockHasGraph(true);
91 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
92 svcClient.mockExecute(svcResultProp);
95 //construct the input parameter for the NetworkTopologyOperation
96 NetworkTopologyOperationInput networkTopologyOperationInput = createNTOI();
99 //pre-populate the DataBroke with the required ServiceData.
100 Service service = persistServiceInDataBroker(networkTopologyOperationInput);
104 //execute the mdsal exec
105 NetworkTopologyOperationOutput actualNetworkTopologyOperationOutput = exec(
106 genericResourceApiProvider::networkTopologyOperation
107 , networkTopologyOperationInput
108 , RpcResult::getResult
111 //verify the returned NetworkTopologyOperationOutput
112 NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput
113 = createExpectedNTOO(svcResultProp,networkTopologyOperationInput);
114 assertEquals(expectedNetworkTopologyOperationOutput,actualNetworkTopologyOperationOutput);
117 //verify the persisted Service
118 Service actualService = db.read(
119 networkTopologyOperationInput.getServiceInformation().getServiceInstanceId(),
120 LogicalDatastoreType.CONFIGURATION
122 Service expectedService = createExpectedService(
123 expectedNetworkTopologyOperationOutput,
124 networkTopologyOperationInput,
125 service.getServiceData(),
127 assertEquals(expectedService,actualService);
132 private NetworkTopologyOperationInput createNTOI()
136 networkTopologyOperationInput()
137 .setSdncRequestHeader(build(sdncRequestHeader()
138 .setSvcRequestId("svc-request-id: xyz")
139 .setSvcAction(SvcAction.Assign)
141 .setRequestInformation(build(requestInformation()
142 .setRequestId("request-id: xyz")
143 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
145 .setServiceInformation(build(serviceInformationBuilder()
146 .setServiceInstanceId("service-instance-id: xyz")
148 .setNetworkInformation(build(
154 private Service persistServiceInDataBroker(
155 NetworkTopologyOperationInput networkTopologyOperationInput
157 Service service = build(
159 .setServiceInstanceId(
160 networkTopologyOperationInput.getServiceInformation().getServiceInstanceId()
162 .setServiceData(build(
164 .setServiceLevelOperStatus(build(
165 serviceLevelOperStatus()
166 .setOrderStatus(OrderStatus.Created)
167 .setModifyTimestamp(Instant.now().toString())
168 .setLastSvcRequestId("svc-request-id: abc")
169 .setLastRpcAction(LastRpcAction.Activate)
170 .setLastOrderStatus(LastOrderStatus.PendingAssignment)
171 .setLastAction(LastAction.ActivateNetworkInstance)
172 .setCreateTimestamp(Instant.now().toString())
177 db.write(true,service, LogicalDatastoreType.CONFIGURATION);
186 private NetworkTopologyOperationOutput createExpectedNTOO(
187 PropBuilder expectedSvcResultProp,
188 NetworkTopologyOperationInput expectedNetworkTopologyOperationInput){
190 networkTopologyOperationOutput()
191 .setSvcRequestId(expectedNetworkTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
192 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
193 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
194 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
195 .setServiceResponseInformation(build(serviceResponseInformation()
196 .setInstanceId(expectedNetworkTopologyOperationInput.getServiceInformation().getServiceInstanceId())
197 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
199 .setNetworkResponseInformation(build(
200 networkResponseInformation()
201 .setInstanceId(expectedSvcResultProp.get(svcClient.networkId))
202 .setObjectPath(expectedSvcResultProp.get(svcClient.networkObjectPath))
207 private Service createExpectedService(
208 NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput,
209 NetworkTopologyOperationInput expectedNetworkTopologyOperationInput,
210 ServiceData expectedServiceData,
211 Service actualService
215 //We cannot predict the timeStamp value so just steal it from the actual
216 //we need this to prevent the equals method from returning false as a result of the timestamp
217 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null?
218 null : actualService.getServiceStatus().getResponseTimestamp();
220 SdncRequestHeader expectedSdncRequestHeader = expectedNetworkTopologyOperationInput.getSdncRequestHeader();
221 ServiceInformation expectedServiceInformation = expectedNetworkTopologyOperationInput.getServiceInformation();
222 RequestInformation expectedRequestInformation = expectedNetworkTopologyOperationInput.getRequestInformation();
226 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
227 .setServiceData(build(serviceData()))
228 .setServiceData(expectedServiceData)
238 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum){
239 return fromEnum == null? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());