Rework the Clamp db model
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsServiceData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.ws.rs.NotAuthorizedException;
31
32 import org.onap.clamp.clds.dao.CldsDao;
33 import org.onap.clamp.clds.service.CldsService;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 public class CldsServiceData implements Serializable {
39
40     private static final long       serialVersionUID = -9153372664377279423L;
41
42     protected static final EELFLogger       logger           = EELFManager.getInstance().getLogger(CldsServiceData.class);
43     protected static final EELFLogger auditLogger      = EELFManager.getInstance().getAuditLogger();
44
45     private String                  serviceInvariantUUID;
46     private String                  serviceUUID;
47     private Long                    ageOfRecord;
48     private List<CldsVfData>        cldsVfs;
49
50     public String getServiceInvariantUUID() {
51         return serviceInvariantUUID;
52     }
53
54     public void setServiceInvariantUUID(String serviceInvariantUUID) {
55         this.serviceInvariantUUID = serviceInvariantUUID;
56     }
57
58     public List<CldsVfData> getCldsVfs() {
59         return cldsVfs;
60     }
61
62     public void setCldsVfs(List<CldsVfData> cldsVfs) {
63         this.cldsVfs = cldsVfs;
64     }
65
66     public String getServiceUUID() {
67         return serviceUUID;
68     }
69
70     public void setServiceUUID(String serviceUUID) {
71         this.serviceUUID = serviceUUID;
72     }
73
74     public CldsServiceData getCldsServiceCache(CldsDao cldsDao, String invariantServiceUUID) throws Exception {
75         return cldsDao.getCldsServiceCache(invariantServiceUUID);
76     }
77
78     public void setCldsServiceCache(CldsDao cldsDao, CldsDBServiceCache cldsDBServiceCache) throws Exception {
79         cldsDao.setCldsServiceCache(cldsDBServiceCache);
80     }
81
82     public Long getAgeOfRecord() {
83         return ageOfRecord;
84     }
85
86     public void setAgeOfRecord(Long ageOfRecord) {
87         this.ageOfRecord = ageOfRecord;
88     }
89
90     /**
91      * Filter out any VFs that the user is not authorized for. Use the
92      * CldsService to determine if the user is authorized for a VF.
93      *
94      * @param svc
95      */
96     public void filterVfs(CldsService svc) {
97         List<CldsVfData> filteredCldsVfs = new ArrayList<>();
98         if (cldsVfs == null) {
99             logger.debug("cldsVfs == null");
100         } else {
101             for (CldsVfData vf : cldsVfs) {
102                 // if user is authorized for the VF then add it to the filtered
103                 // list
104                 try {
105                     if (svc.isAuthorizedForVf(vf.getVfInvariantResourceUUID())) {
106                         filteredCldsVfs.add(vf);
107                     }
108                 } catch (NotAuthorizedException e) {
109                     logger.debug("user not authorized for {}", vf.getVfInvariantResourceUUID());
110                     // when not NotAuthorizedException - don't add to
111                     // filteredCldsVfs list
112                 }
113             }
114         }
115         // new filtered list replaces the list of VFs for the user
116         cldsVfs = filteredCldsVfs;
117     }
118 }