f3d6f1daf5bc3ac95f8b89ad5cbfea7b8c5cfe35
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / main / java / org / onap / policy / controlloop / actor / sdnc / SdncOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.sdnc;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
32 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
35 import org.onap.policy.sdnc.SdncRequest;
36 import org.onap.policy.sdnc.SdncResponse;
37
38 /**
39  * Superclass for SDNC Operators.
40  */
41 public abstract class SdncOperation extends HttpOperation<SdncResponse> {
42
43     /**
44      * Constructs the object.
45      *
46      * @param params operation parameters
47      * @param config configuration for this operation
48      * @param propertyNames names of properties required by this operation
49      */
50     protected SdncOperation(ControlLoopOperationParams params, HttpConfig config, List<String> propertyNames) {
51         super(params, config, SdncResponse.class, propertyNames);
52     }
53
54     @Override
55     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
56
57         SdncRequest request = makeRequest(attempt);
58
59         Map<String, Object> headers = makeHeaders();
60
61         headers.put("Accept", MediaType.APPLICATION_JSON);
62         String path = getPath();
63         String url = getClient().getBaseUrl() + path;
64
65         String strRequest = prettyPrint(request);
66         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
67
68         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
69
70         // @formatter:off
71         return handleResponse(outcome, url,
72             callback -> getClient().post(callback, path, entity, headers));
73         // @formatter:on
74     }
75
76     /**
77      * Makes the request.
78      *
79      * @param attempt current attempt, starting with "1"
80      * @return a new request to be posted
81      */
82     protected abstract SdncRequest makeRequest(int attempt);
83
84     /**
85      * Checks that the response has an "output" and that the output indicates success.
86      */
87     @Override
88     protected boolean isSuccess(Response rawResponse, SdncResponse response) {
89         return response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode());
90     }
91 }