multiple identical hub
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / SubscriptionService.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  */
13 package org.onap.nbi.apis.hub.service;
14
15 import com.google.common.collect.Lists;
16 import java.text.MessageFormat;
17 import java.util.List;
18 import org.onap.nbi.apis.hub.model.Subscriber;
19 import org.onap.nbi.apis.hub.model.Subscription;
20 import org.onap.nbi.apis.hub.repository.SubscriberRepository;
21 import org.onap.nbi.exceptions.ValidationException;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.data.domain.Example;
24 import org.springframework.stereotype.Service;
25 import org.springframework.validation.ObjectError;
26
27 @Service
28 public class SubscriptionService {
29
30     @Autowired
31     SubscriberRepository subscriberRepository;
32
33     public Subscriber findSubscriptionById(String subscriptionId) {
34         return subscriberRepository.findOne(subscriptionId);
35     }
36
37     public Subscriber createSubscription(Subscription subscription) {
38         subscription.setId(null);
39         Subscriber subscriber = Subscriber.createFromSubscription(subscription);
40         if (isSubscriberAlreadyExisting(subscriber)) {
41             String message = MessageFormat
42                 .format("subscription with callback {0} and query {1} already exists", subscription.getCallback(),
43                     subscription.getQuery());
44             ObjectError error = new ObjectError("subscription", message);
45             List<ObjectError> errors = Lists.newArrayList(error);
46             throw new ValidationException(errors);
47         } else {
48             return subscriberRepository.save(subscriber);
49         }
50     }
51
52     private boolean isSubscriberAlreadyExisting(Subscriber subscriber) {
53         Example<Subscriber> subscriberExample = Example.of(subscriber);
54         Subscriber subscriberAlreadyExisting = subscriberRepository.findOne(subscriberExample);
55         return subscriberAlreadyExisting != null;
56     }
57
58     public void deleteSubscription(String subscriptionId) {
59         subscriberRepository.delete(subscriptionId);
60     }
61
62     public void deleteAll() {
63         subscriberRepository.deleteAll();
64     }
65
66     public long countSubscription() {
67         return subscriberRepository.count();
68     }
69
70 }