9d42c49d95c848ffdeeb957156399360dda3acb5
[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 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.Map;
24 import java.util.concurrent.CompletableFuture;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
29 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
30 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
32 import org.onap.policy.sdnc.SdncRequest;
33 import org.onap.policy.sdnc.SdncResponse;
34
35 /**
36  * Superclass for SDNC Operators.
37  */
38 public abstract class SdncOperation extends HttpOperation<SdncResponse> {
39
40     /**
41      * Constructs the object.
42      *
43      * @param params operation parameters
44      * @param operator operator that created this operation
45      */
46     public SdncOperation(ControlLoopOperationParams params, HttpOperator operator) {
47         super(params, operator, SdncResponse.class);
48     }
49
50     @Override
51     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
52
53         SdncRequest request = makeRequest(attempt);
54
55         Entity<SdncRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
56
57         Map<String, Object> headers = makeHeaders();
58
59         headers.put("Accept", MediaType.APPLICATION_JSON);
60         String url = makeUrl();
61
62         logRestRequest(url, request);
63
64         // @formatter:off
65         return handleResponse(outcome, url,
66             callback -> operator.getClient().post(callback, makePath(), entity, headers));
67         // @formatter:on
68     }
69
70     /**
71      * Makes the request.
72      *
73      * @param attempt current attempt, starting with "1"
74      * @return a new request to be posted
75      */
76     protected abstract SdncRequest makeRequest(int attempt);
77
78     /**
79      * Checks that the response has an "output" and that the output indicates success.
80      */
81     @Override
82     protected boolean isSuccess(Response rawResponse, SdncResponse response) {
83         return response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode());
84     }
85 }