Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / mso-ve-vnfm-adapter / src / main / java / org / onap / so / adapters / vevnfm / service / SubscribeSender.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 Samsung. 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.so.adapters.vevnfm.service;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import org.onap.aai.domain.yang.EsrSystemInfo;
25 import org.onap.so.adapters.vevnfm.configuration.ConfigProperties;
26 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
27 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.LccnSubscriptionRequest;
28 import org.onap.so.rest.service.HttpRestServiceProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Service;
35 import lombok.ToString;
36
37 @Service
38 public class SubscribeSender {
39
40     public static final String SLASH = "/";
41
42     private static final Logger logger = LoggerFactory.getLogger(SubscribeSender.class);
43
44     private final String vnfmSubscription;
45     private final HttpRestServiceProvider restProvider;
46
47     @Autowired
48     public SubscribeSender(final ConfigProperties configProperties, final HttpRestServiceProvider restProvider) {
49         this.vnfmSubscription = configProperties.getVnfmSubscription();
50         this.restProvider = restProvider;
51     }
52
53     public String send(final EsrSystemInfo info, final LccnSubscriptionRequest request) throws VeVnfmException {
54         final ResponseEntity<SubscribeToManoResponse> response =
55                 restProvider.postHttpRequest(request, getUrl(info), SubscribeToManoResponse.class);
56
57         final HttpStatus statusCode = response.getStatusCode();
58         final SubscribeToManoResponse body = response.getBody();
59
60         logger.info("The VNFM replied with the code {} and the body {}", statusCode, body);
61
62         if (HttpStatus.CREATED != statusCode) {
63             throw new VeVnfmException("The status code was different than " + HttpStatus.CREATED);
64         }
65
66         return body.id;
67     }
68
69     public boolean check(final EsrSystemInfo info, final String id) {
70         final ResponseEntity<SubscribeToManoResponse> response =
71                 restProvider.getHttpResponse(getUrl(info) + SLASH + id, SubscribeToManoResponse.class);
72         return response.getBody() != null && response.getBody().id.equals(id);
73     }
74
75     private String getUrl(final EsrSystemInfo info) {
76         return info.getServiceUrl() + vnfmSubscription;
77     }
78
79     @ToString
80     static class SubscribeToManoResponse {
81         @JsonProperty("id")
82         String id;
83         @JsonProperty("callbackUri")
84         String callbackUri;
85     }
86 }