ECS, support for notification of available information types
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / r1consumer / ConsumerCallbacks.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
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.oransc.enrichment.controllers.r1consumer;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.lang.invoke.MethodHandles;
27
28 import org.oransc.enrichment.clients.AsyncRestClient;
29 import org.oransc.enrichment.clients.AsyncRestClientFactory;
30 import org.oransc.enrichment.configuration.ApplicationConfig;
31 import org.oransc.enrichment.repository.InfoType;
32 import org.oransc.enrichment.repository.InfoTypeSubscriptions;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * Callbacks to the Consumer. Notifies consumer according to the API (which this
40  * class adapts to)
41  */
42 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
43 @Component
44 public class ConsumerCallbacks implements InfoTypeSubscriptions.Callbacks {
45
46     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47     private static Gson gson = new GsonBuilder().create();
48
49     private final AsyncRestClient restClient;
50
51     public ConsumerCallbacks(@Autowired ApplicationConfig config) {
52         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
53         this.restClient = restClientFactory.createRestClientNoHttpProxy("");
54     }
55
56     @Override
57     public void notifyTypeRegistered(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
58         ConsumerTypeRegistrationInfo info = new ConsumerTypeRegistrationInfo(type.getJobDataSchema(),
59             ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.REGISTERED, type.getId());
60         String body = gson.toJson(info);
61
62         post(subscriptionInfo.getCallbackUrl(), body);
63
64     }
65
66     @Override
67     public void notifyTypeRemoved(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
68         ConsumerTypeRegistrationInfo info = new ConsumerTypeRegistrationInfo(type.getJobDataSchema(),
69             ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.DEREGISTERED, type.getId());
70         String body = gson.toJson(info);
71         post(subscriptionInfo.getCallbackUrl(), body);
72
73     }
74
75     private void post(String url, String body) {
76         restClient.post(url, body) //
77             .subscribe(response -> logger.debug("Post OK {}", url), //
78                 throwable -> logger.warn("Post failed for consumer callback {} {}", url, body), null);
79     }
80
81 }