65b655936f7b8001b5cef409763acc3ec7554445
[policy/models.git] / models-interactions / model-impl / sdnc / src / main / java / org / onap / policy / sdnc / SdncManager.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Huawei. All rights reserved.
4  * ================================================================================
5  * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.sdnc;
24
25 import com.google.gson.JsonSyntaxException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
29 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.rest.RestManager;
32 import org.onap.policy.rest.RestManager.Pair;
33 import org.onap.policy.sdnc.util.Serialization;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 // TODO this class will be deleted
38
39 public final class SdncManager implements Runnable {
40
41     private String sdncUrlBase;
42     private String username;
43     private String password;
44     private SdncRequest sdncRequest;
45     private SdncCallback callback;
46     private static final Logger logger = LoggerFactory.getLogger(SdncManager.class);
47
48     // The REST manager used for processing REST calls for this Sdnc manager
49     private RestManager restManager;
50
51     @FunctionalInterface
52     public interface SdncCallback {
53         public void onCallback(SdncResponse response);
54     }
55
56     /**
57      * Constructor.
58      *
59      * @param cb Callback method
60      * @param request request
61      */
62     public SdncManager(SdncCallback cb, SdncRequest request, String url,
63             String user, String password) {
64         if (cb == null || request == null) {
65             throw new IllegalArgumentException(
66                   "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"
67             );
68         }
69         this.callback = cb;
70         this.sdncRequest = request;
71         if (url == null) {
72             throw new IllegalArgumentException(
73                     "the \"url\" parameter on the SdncManager constructor may not be null"
74               );
75         }
76         this.sdncUrlBase = url;
77         this.username = user;
78         this.password = password;
79
80         restManager = new RestManager();
81     }
82
83     /**
84      * Set the parameters.
85      *
86      * @param baseUrl base URL
87      * @param name username
88      * @param pwd password
89      */
90     public void setSdncParams(String baseUrl, String name, String pwd) {
91         sdncUrlBase = baseUrl;
92         username = name;
93         password = pwd;
94     }
95
96     @Override
97     public void run() {
98         Map<String, String> headers = new HashMap<>();
99         Pair<Integer, String> httpDetails;
100
101         SdncResponse responseError = new SdncResponse();
102         SdncResponseOutput responseOutput = new SdncResponseOutput();
103         responseOutput.setResponseCode("404");
104         responseError.setResponseOutput(responseOutput);
105
106         headers.put("Accept", "application/json");
107         String sdncUrl = sdncUrlBase + sdncRequest.getUrl();
108
109         try {
110             String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest);
111             NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, sdncUrl, sdncRequestJson);
112             logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, sdncUrl, NetLoggerUtil.SYSTEM_LS, sdncRequestJson);
113
114             httpDetails = restManager.post(sdncUrl, username, password, headers, "application/json",
115                                            sdncRequestJson);
116         } catch (Exception e) {
117             logger.info(e.getMessage(), e);
118             this.callback.onCallback(responseError);
119             return;
120         }
121
122         if (httpDetails == null) {
123             this.callback.onCallback(responseError);
124             return;
125         }
126
127         try {
128             SdncResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, SdncResponse.class);
129             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, sdncUrl, httpDetails.second);
130             logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, NetLoggerUtil.SYSTEM_LS, httpDetails.second);
131             String body = Serialization.gsonPretty.toJson(response);
132             logger.info("Response to Sdnc Heal post:");
133             logger.info(body);
134             response.setRequestId(sdncRequest.getRequestId().toString());
135
136             if (!"200".equals(response.getResponseOutput().getResponseCode())) {
137                 logger.info(
138                     "Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second
139                 );
140             }
141
142             this.callback.onCallback(response);
143         } catch (JsonSyntaxException e) {
144             logger.info("Failed to deserialize into SdncResponse {}", e.getLocalizedMessage(), e);
145         } catch (Exception e) {
146             logger.info("Unknown error deserializing into SdncResponse {}", e.getLocalizedMessage(), e);
147         }
148     }
149
150     /**
151      * Protected setter for rest manager to allow mocked rest manager to be used for testing.
152      * @param restManager the test REST manager
153      */
154     protected void setRestManager(final RestManager restManager) {
155         this.restManager = restManager;
156     }
157 }