5386d96ddf67d687102b874ec471874b1369a437
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / ModifyNssi.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 Wipro Limited.
6  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights 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.policy.controlloop.actor.so;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.policy.common.endpoints.event.comm.Topic;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
33 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
36 import org.onap.policy.so.SoRequest3gpp;
37
38 public class ModifyNssi extends SoOperation {
39     public static final String NAME = "Modify NSSI";
40
41     private static final List<String> PROPERTY_NAMES = List.of(
42             OperationProperties.EVENT_PAYLOAD);
43
44     /**
45      * Constructs the object.
46      *
47      * @param params        operation parameters
48      * @param config        configuration for this operation
49      */
50     public ModifyNssi(ControlLoopOperationParams params, HttpPollingConfig config) {
51         super(params, config, PROPERTY_NAMES);
52     }
53
54     @Override
55     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
56
57         SoRequest3gpp soRequest = makeRequest();
58
59         String path = getPath();
60         String url = getClient().getBaseUrl() + path;
61
62         String strRequest = prettyPrint(soRequest);
63         logMessage(NetLoggerUtil.EventType.OUT, Topic.CommInfrastructure.REST, url, strRequest);
64
65         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
66         Map<String, Object> headers = createSimpleHeaders();
67
68         return handleResponse(outcome, url, callback -> getClient().put(callback, path, entity, headers));
69     }
70
71     protected SoRequest3gpp makeRequest() {
72
73         String payload = getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload");
74         try {
75             return getCoder().convert(payload, SoRequest3gpp.class);
76         } catch (CoderException e) {
77             throw new IllegalArgumentException("invalid payload value: " + payload, e);
78         }
79     }
80 }