fa18cb2544064001d0aee8dfed4448d7066800b5
[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
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
33 import org.onap.policy.rest.RestManager;
34 import org.onap.policy.rest.RestManager.Pair;
35 import org.onap.policy.sdnc.util.Serialization;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 // TODO this class will be deleted
41
42 public final class SdncManager implements Runnable {
43
44     private String sdncUrlBase;
45     private String username;
46     private String password;
47     private SdncRequest sdncRequest;
48     private SdncCallback callback;
49     private static final Logger logger = LoggerFactory.getLogger(SdncManager.class);
50
51     // The REST manager used for processing REST calls for this Sdnc manager
52     private RestManager restManager;
53
54     @FunctionalInterface
55     public interface SdncCallback {
56         public void onCallback(SdncResponse response);
57     }
58
59     /**
60      * Constructor.
61      *
62      * @param cb Callback method
63      * @param request request
64      */
65     public SdncManager(SdncCallback cb, SdncRequest request, String url,
66             String user, String password) {
67         if (cb == null || request == null) {
68             throw new IllegalArgumentException(
69                   "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"
70             );
71         }
72         this.callback = cb;
73         this.sdncRequest = request;
74         if (url == null) {
75             throw new IllegalArgumentException(
76                     "the \"url\" parameter on the SdncManager constructor may not be null"
77               );
78         }
79         this.sdncUrlBase = url;
80         this.username = user;
81         this.password = password;
82
83         restManager = new RestManager();
84     }
85
86     /**
87      * Set the parameters.
88      *
89      * @param baseUrl base URL
90      * @param name username
91      * @param pwd password
92      */
93     public void setSdncParams(String baseUrl, String name, String pwd) {
94         sdncUrlBase = baseUrl;
95         username = name;
96         password = pwd;
97     }
98
99     @Override
100     public void run() {
101         Map<String, String> headers = new HashMap<>();
102         Pair<Integer, String> httpDetails;
103
104         SdncResponse responseError = new SdncResponse();
105         SdncResponseOutput responseOutput = new SdncResponseOutput();
106         responseOutput.setResponseCode("404");
107         responseError.setResponseOutput(responseOutput);
108
109         headers.put("Accept", "application/json");
110         String sdncUrl = sdncUrlBase + sdncRequest.getUrl();
111
112         try {
113             String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest);
114             NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, sdncUrl, sdncRequestJson);
115             logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, sdncUrl, NetLoggerUtil.SYSTEM_LS, sdncRequestJson);
116
117             httpDetails = restManager.post(sdncUrl, username, password, headers, "application/json",
118                                            sdncRequestJson);
119         } catch (Exception e) {
120             logger.info(e.getMessage(), e);
121             this.callback.onCallback(responseError);
122             return;
123         }
124
125         if (httpDetails == null) {
126             this.callback.onCallback(responseError);
127             return;
128         }
129
130         try {
131             SdncResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, SdncResponse.class);
132             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, sdncUrl, httpDetails.second);
133             logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, NetLoggerUtil.SYSTEM_LS, httpDetails.second);
134             String body = Serialization.gsonPretty.toJson(response);
135             logger.info("Response to Sdnc Heal post:");
136             logger.info(body);
137             response.setRequestId(sdncRequest.getRequestId().toString());
138
139             if (!"200".equals(response.getResponseOutput().getResponseCode())) {
140                 logger.info(
141                     "Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second
142                 );
143             }
144
145             this.callback.onCallback(response);
146         } catch (JsonSyntaxException e) {
147             logger.info("Failed to deserialize into SdncResponse {}", e.getLocalizedMessage(), e);
148         } catch (Exception e) {
149             logger.info("Unknown error deserializing into SdncResponse {}", e.getLocalizedMessage(), e);
150         }
151     }
152
153     /**
154      * Protected setter for rest manager to allow mocked rest manager to be used for testing.
155      * @param restManager the test REST manager
156      */
157     protected void setRestManager(final RestManager restManager) {
158         this.restManager = restManager;
159     }
160 }