89108393f62003641add55b66f2904ecbda828b9
[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 org.onap.clamp.clds.dao.CldsDao;
27 import org.onap.clamp.clds.service.CldsService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.ws.rs.NotAuthorizedException;
32 import java.io.Serializable;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 public class CldsServiceData implements Serializable {
37     private static final Logger logger = LoggerFactory.getLogger(CldsServiceData.class);
38
39     private static final long serialVersionUID = 1L;
40
41     private String serviceInvariantUUID;
42     private String serviceUUID;
43     private Long ageOfRecord;
44     private List<CldsVfData> cldsVfs;
45
46     public String getServiceInvariantUUID() {
47         return serviceInvariantUUID;
48     }
49
50     public void setServiceInvariantUUID(String serviceInvariantUUID) {
51         this.serviceInvariantUUID = serviceInvariantUUID;
52     }
53
54     public List<CldsVfData> getCldsVfs() {
55         return cldsVfs;
56     }
57
58     public void setCldsVfs(List<CldsVfData> cldsVfs) {
59         this.cldsVfs = cldsVfs;
60     }
61
62     public String getServiceUUID() {
63         return serviceUUID;
64     }
65
66     public void setServiceUUID(String serviceUUID) {
67         this.serviceUUID = serviceUUID;
68     }
69
70     public CldsServiceData getCldsServiceCache(CldsDao cldsDao, String invariantServiceUUID) throws Exception {
71         return cldsDao.getCldsServiceCache(invariantServiceUUID);
72     }
73
74     public void setCldsServiceCache(CldsDao cldsDao, CldsDBServiceCache cldsDBServiceCache) throws Exception {
75         cldsDao.setCldsServiceCache(cldsDBServiceCache);
76     }
77
78     public Long getAgeOfRecord() {
79         return ageOfRecord;
80     }
81
82     public void setAgeOfRecord(Long ageOfRecord) {
83         this.ageOfRecord = ageOfRecord;
84     }
85
86     /**
87      * Filter out any VFs that the user is not authorized for.
88      * Use the CldsService to determine if the user is authorized for a VF.
89      *
90      * @param svc
91      */
92     public void filterVfs(CldsService svc) {
93         List<CldsVfData> filteredCldsVfs = new ArrayList<>();
94         if (cldsVfs == null) {
95             logger.debug("cldsVfs == null");
96         } else {
97             for (CldsVfData vf : cldsVfs) {
98                 // if user is authorized for the VF then add it to the filtered list
99                 try {
100                     if (svc.isAuthorizedForVf(vf.getVfInvariantResourceUUID())) {
101                         filteredCldsVfs.add(vf);
102                     }
103                 } catch (NotAuthorizedException e) {
104                     logger.debug("user not authorized for {}", vf.getVfInvariantResourceUUID());
105                     // when not NotAuthorizedException - don't add to filteredCldsVfs list
106                 }
107             }
108         }
109         // new filtered list replaces the list of VFs for the user
110         cldsVfs = filteredCldsVfs;
111     }
112 }