2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portal.service.sharedContext;
40 import java.text.SimpleDateFormat;
41 import java.time.Instant;
42 import java.time.LocalDateTime;
43 import java.time.ZoneId;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
47 import java.util.Optional;
48 import org.onap.portal.domain.db.fn.FnSharedContext;
49 import org.onap.portal.logging.aop.EPMetricsLog;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.stereotype.Service;
54 import org.springframework.transaction.annotation.Transactional;
59 @EnableAspectJAutoProxy
61 public class FnSharedContextService {
63 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnSharedContextService.class);
65 private final FnSharedContextDao fnSharedContextDao;
68 public FnSharedContextService(final FnSharedContextDao fnSharedContextDao) {
69 this.fnSharedContextDao = fnSharedContextDao;
72 public List<FnSharedContext> getSharedContexts(String contextId) {
73 return fnSharedContextDao.getByContextId(contextId).orElse(new ArrayList<>());
76 public FnSharedContext getFnSharedContext(String contextId, String ckey) {
77 FnSharedContext context = null;
78 Optional<List<FnSharedContext>> contexts = fnSharedContextDao.getByContextIdAndCkey(contextId, ckey);
79 if (contexts.isPresent() && contexts.get().size() == 1) {
80 context = contexts.get().get(0);
85 public FnSharedContext addFnSharedContext(String contextId, String key, String value) {
86 return this.save(new FnSharedContext(contextId, key, value));
90 public FnSharedContext save(FnSharedContext context) {
91 return this.fnSharedContextDao.saveAndFlush(context);
94 public List<FnSharedContext> saveAll(List<FnSharedContext> fnSharedContexts){
95 return fnSharedContextDao.saveAll(fnSharedContexts);
98 public void delete(FnSharedContext context) {
99 this.fnSharedContextDao.delete(context);
102 public int deleteSharedContexts(String contextId) {
103 // Uses an inefficient method to avoid a where clause
104 // that could be used to mount a SQL injection attack.
105 List<FnSharedContext> contexts = getSharedContexts(contextId);
106 if (contexts == null)
109 logger.debug(EELFLoggerDelegate.debugLogger, "deleteFnSharedContexts: count is " + contexts.size());
110 for (FnSharedContext sc : contexts)
113 return contexts.size();
116 public void expireFnSharedContexts(int ageInSeconds) {
117 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds * 1000);
118 logger.debug(EELFLoggerDelegate.debugLogger,
119 "expireFnSharedContexts: expire time is " + expiredDateTime.toString());
120 this.fnSharedContextDao.deleteByCreated(convertToLocalDateTimeViaInstant(expiredDateTime));
123 private LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
124 return dateToConvert.toInstant()
125 .atZone(ZoneId.systemDefault())