88df42bb0ec63a0cd55ac8d2d46b10f37b3caa4d
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
6  * Modifications Copyright (C) 2021 Samsung Technologies Co.
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 package org.onap.so.adapters.cnf.rest;
22
23
24 import com.google.gson.Gson;
25 import org.onap.so.adapters.cnf.model.aai.AaiRequest;
26 import org.onap.so.adapters.cnf.model.synchronization.NotificationRequest;
27 import org.onap.so.adapters.cnf.service.aai.AaiService;
28 import org.onap.so.adapters.cnf.service.synchrornization.SynchronizationService;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.PostMapping;
35 import org.springframework.web.bind.annotation.RequestBody;
36 import org.springframework.web.bind.annotation.RestController;
37
38 import java.util.Map;
39
40 @RestController
41 public class SubscriptionNotifyController {
42
43     private static final Logger logger = LoggerFactory.getLogger(SubscriptionNotifyController.class);
44     private final static Gson gson = new Gson();
45
46     private final AaiService aaiService;
47     private final SynchronizationService synchronizationService;
48
49     public SubscriptionNotifyController(AaiService aaiService, SynchronizationService synchronizationService) {
50         this.aaiService = aaiService;
51         this.synchronizationService = synchronizationService;
52     }
53
54     @PostMapping(value = "/api/cnf-adapter/v1/instance/{instanceId}/status/notify")
55     public ResponseEntity subscriptionNotifyEndpoint(@PathVariable String instanceId,
56                                                      @RequestBody NotificationRequest body) throws BadResponseException {
57         String subscriptionName = synchronizationService.getSubscriptionName(instanceId);
58         boolean isSubscriptionActive = synchronizationService.isSubscriptionActive(subscriptionName);
59         if (isSubscriptionActive) {
60             logger.info("AAI update- START");
61             aaiService.aaiUpdate(body.getMetadata());
62             return ResponseEntity
63                     .accepted()
64                     .build();
65         } else {
66             return ResponseEntity
67                     .badRequest()
68                     .body(String.format("Cannot handle notification. Subscription %s not exists", subscriptionName));
69         }
70     }
71
72     private AaiRequest convertMetadataToAaiRequest(Map<String, Object> metadata) {
73         String json = gson.toJsonTree(metadata)
74                 .getAsJsonObject()
75                 .get("metadata")
76                 .toString();
77
78         return gson.fromJson(json, AaiRequest.class);
79     }
80
81 }