31ec9cd3954107b1b19a0a32356fbf696b0f2915
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / NetworkTopologyOperationRPCTest.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;
23
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;
41
42 import java.time.Instant;
43
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;
63
64
65 /**
66  * This class test the NetworkTopologyOperation mdsal RPC.
67  */
68 @RunWith(MockitoJUnitRunner.class)
69 public class NetworkTopologyOperationRPCTest extends GenericResourceApiProviderTest {
70
71
72     final String SVC_OPERATION = "network-topology-operation";
73
74
75     @Before
76     public void setUp() throws Exception {
77         super.setUp();
78         svcClient.setScvOperation(SVC_OPERATION);
79     }
80
81
82     /**
83      * Verify  ServiceTopologyOperation RPC executes a DG then produces the expected
84      * {@link NetworkTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
85      */
86     @Test
87     public void testNetworkTopologyOperation() throws Exception {
88
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);
93
94
95         //construct the input parameter for the NetworkTopologyOperation
96         NetworkTopologyOperationInput networkTopologyOperationInput = createNTOI();
97
98
99         //pre-populate the DataBroke with the required ServiceData.
100         Service service = persistServiceInDataBroker(networkTopologyOperationInput);
101
102
103
104         //execute the mdsal exec
105         NetworkTopologyOperationOutput actualNetworkTopologyOperationOutput = exec(
106                 genericResourceApiProvider::networkTopologyOperation
107                 , networkTopologyOperationInput
108                 , RpcResult::getResult
109         );
110
111         //verify the returned NetworkTopologyOperationOutput
112         NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput
113                 = createExpectedNTOO(svcResultProp,networkTopologyOperationInput);
114         assertEquals(expectedNetworkTopologyOperationOutput,actualNetworkTopologyOperationOutput);
115
116
117         //verify the persisted Service
118         Service actualService = db.read(
119                 networkTopologyOperationInput.getServiceInformation().getServiceInstanceId(),
120                 LogicalDatastoreType.CONFIGURATION
121         );
122         Service expectedService = createExpectedService(
123                 expectedNetworkTopologyOperationOutput,
124                 networkTopologyOperationInput,
125                 service.getServiceData(),
126                 actualService);
127         assertEquals(expectedService,actualService);
128
129     }
130
131
132     private NetworkTopologyOperationInput createNTOI()
133     {
134
135         return build(
136                 networkTopologyOperationInput()
137                         .setSdncRequestHeader(build(sdncRequestHeader()
138                                 .setSvcRequestId("svc-request-id: xyz")
139                                 .setSvcAction(SvcAction.Assign)
140                         ))
141                         .setRequestInformation(build(requestInformation()
142                                 .setRequestId("request-id: xyz")
143                                 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
144                         ))
145                         .setServiceInformation(build(serviceInformationBuilder()
146                                 .setServiceInstanceId("service-instance-id: xyz")
147                         ))
148                         .setNetworkInformation(build(
149                                 networkInformation()
150                         ))
151         );
152     }
153
154     private Service persistServiceInDataBroker(
155             NetworkTopologyOperationInput networkTopologyOperationInput
156     ) throws Exception{
157         Service service = build(
158                 service()
159                         .setServiceInstanceId(
160                                 networkTopologyOperationInput.getServiceInformation().getServiceInstanceId()
161                         )
162                         .setServiceData(build(
163                                 serviceData()
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())
173                                         ))
174                         ))
175
176         );
177         db.write(true,service, LogicalDatastoreType.CONFIGURATION);
178         return service;
179     }
180
181
182
183
184
185
186     private NetworkTopologyOperationOutput createExpectedNTOO(
187             PropBuilder expectedSvcResultProp,
188             NetworkTopologyOperationInput expectedNetworkTopologyOperationInput){
189         return build(
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))
198                         ))
199                         .setNetworkResponseInformation(build(
200                                 networkResponseInformation()
201                                 .setInstanceId(expectedSvcResultProp.get(svcClient.networkId))
202                                 .setObjectPath(expectedSvcResultProp.get(svcClient.networkObjectPath))
203                         ))
204         );
205     }
206
207     private Service createExpectedService(
208             NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput,
209             NetworkTopologyOperationInput expectedNetworkTopologyOperationInput,
210             ServiceData expectedServiceData,
211             Service actualService
212     ){
213
214
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();
219
220         SdncRequestHeader expectedSdncRequestHeader = expectedNetworkTopologyOperationInput.getSdncRequestHeader();
221         ServiceInformation expectedServiceInformation = expectedNetworkTopologyOperationInput.getServiceInformation();
222         RequestInformation expectedRequestInformation = expectedNetworkTopologyOperationInput.getRequestInformation();
223
224         return build(
225                 service()
226                         .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
227                         .setServiceData(build(serviceData()))
228                         .setServiceData(expectedServiceData)
229                         .setServiceStatus(
230                                 build(
231                                         serviceStatus()
232                                 )
233                         )
234         );
235
236     }
237
238     public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum){
239         return fromEnum == null? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());
240     }
241
242
243
244
245 }