Refine access control, peer access
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / PeerServiceLocalImpl.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * 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  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20
21 package org.acumos.federation.gateway.service.impl;
22
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.BufferedReader;
26 import java.io.IOException;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30
31 import java.util.Set;
32 import java.util.Map;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.LinkedList;
36 import java.util.Date;
37 import java.util.Collections;
38 import java.util.stream.Collectors;
39
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42
43 import com.fasterxml.jackson.annotation.JsonIgnore;
44 import com.fasterxml.jackson.annotation.JsonProperty;
45 import com.fasterxml.jackson.databind.MappingIterator;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.ObjectReader;
48
49 import org.springframework.core.env.Environment;
50 import org.springframework.stereotype.Service;
51 import org.springframework.beans.factory.BeanInitializationException;
52 import org.springframework.boot.context.properties.ConfigurationProperties;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.context.annotation.Conditional;
55 import org.springframework.beans.factory.annotation.Autowired;
56
57 import org.acumos.federation.gateway.util.LocalWatchService;
58 import org.acumos.federation.gateway.common.AdapterCondition;
59 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
60 import org.acumos.federation.gateway.service.PeerService;
61 import org.acumos.federation.gateway.service.ServiceContext;
62 import org.acumos.federation.gateway.service.PeerSubscriptionService;
63
64 import org.acumos.cds.domain.MLPPeer;
65 import org.acumos.cds.domain.MLPPeerSubscription;
66
67 import org.apache.commons.io.IOUtils;
68
69 @Service
70 @ConfigurationProperties(prefix="peersLocal")
71 @Conditional(AdapterCondition.class)
72 public class PeerServiceLocalImpl
73                                                                                                 extends AbstractServiceLocalImpl
74                                                                                                 implements PeerService,
75                                                                                                                                          PeerSubscriptionService {
76
77         private List<FLPPeer>           peers;
78
79
80         @PostConstruct
81         public void initPeerService() {
82                 log.debug(EELFLoggerDelegate.debugLogger, "init local peer info service");
83                 checkResource();
84                 try {
85                         watcher.watchOn(this.resource.getURL().toURI(),
86                                                                                         (uri) -> { loadPeersSubscriptionsInfo(); });
87                 }
88                 catch (IOException | URISyntaxException iox) {
89                         log.info(EELFLoggerDelegate.errorLogger, "Peers subscriptions watcher registration failed for " + this.resource, iox);
90                 }
91
92                 loadPeersSubscriptionsInfo();
93
94                 // Done
95                 log.debug(EELFLoggerDelegate.debugLogger, "Local PeerService available");
96         }
97
98         private void loadPeersSubscriptionsInfo() {
99                 log.info(EELFLoggerDelegate.debugLogger, "Loading peers subscriptions from " + this.resource);
100                 synchronized (this) {
101             try {
102                                 ObjectReader objectReader =
103                                 new ObjectMapper().reader(FLPPeer.class);
104         MappingIterator objectIterator =
105                                     objectReader.readValues(this.resource.getURL());
106                                 this.peers = objectIterator.readAll();
107                                 log.info(EELFLoggerDelegate.debugLogger, "loaded " + this.peers.size() + " peers");
108                         }
109                         catch (Exception x) {
110         throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
111                         }
112                 }
113         }
114
115         @PreDestroy
116         public void cleanupPeerService() {
117                 log.debug(EELFLoggerDelegate.debugLogger, "Local peer info service destroyed");
118         }
119
120
121         /** */
122         public List<MLPPeer> getPeers() {
123                 synchronized (this) {
124                         return this.peers == null ? null : 
125                                                                                                                                 this.peers.stream()
126                                                                                                                                                 .map(peer -> (MLPPeer)peer)
127                                                                                                                                                 .collect(Collectors.toList());
128                 }
129         }
130
131         /** */
132         public List<MLPPeer> getPeers(ServiceContext theContext) {
133                 return getPeers();      
134         }
135
136         /** */
137         public List<MLPPeer> getPeer(final String theSubjectName) {
138                 log.info(EELFLoggerDelegate.debugLogger, "Looking for peer " + theSubjectName);
139                 return 
140                         this.peers
141                                                 .stream()
142                                                 .filter(peer -> { 
143                 log.info(EELFLoggerDelegate.debugLogger, "Found peer " + peer.getSubjectName());
144                                                                                                                         return theSubjectName.equals(peer.getSubjectName()); })
145                                                 .collect(Collectors.toList());
146         }
147         
148         /** */
149         public MLPPeer getOnePeer(final String thePeerId) {
150                 MLPPeer apeer =
151                         this.peers
152                                                 .stream()
153                                                 .filter(peer -> thePeerId.equals(peer.getPeerId()))
154                                                 .findFirst()
155                                                 .orElse(null);
156
157                 log.debug(EELFLoggerDelegate.errorLogger, "Local peer info, one peer: " + apeer);
158
159                 return apeer;
160         }
161         
162         /** */
163         public MLPPeer savePeer(MLPPeer mlpPeer) {
164                 throw new UnsupportedOperationException();
165         }
166                 
167         /** */
168         public boolean updatePeer(MLPPeer mlpPeer) {
169                 throw new UnsupportedOperationException();
170         }
171
172         /** */
173         public boolean deletePeer(MLPPeer mlpPeer) {
174                 throw new UnsupportedOperationException();
175         }
176
177         public List<MLPPeerSubscription> getPeerSubscriptions(final String thePeerId) {
178                 FLPPeer peer =
179                         this.peers
180                                                         .stream()
181                                                         .filter(entry -> thePeerId.equals(entry.getPeerId()))
182                                                         .findFirst()
183                                                         .orElse(null);
184                 log.info(EELFLoggerDelegate.errorLogger, "Peer " + thePeerId + " subs:" + (peer == null ? "none" : peer.getSubscriptions()));
185                 return peer == Collections.EMPTY_LIST ? null : peer.getSubscriptions();
186         }
187
188         /** */
189         public MLPPeerSubscription getPeerSubscription(Long theSubId) {
190                 for (FLPPeer peer: this.peers) {
191                         for (MLPPeerSubscription peerSub: peer.getSubscriptions()) {
192                                 if (peerSub.getSubId().equals(theSubId))
193                                         return peerSub;
194                         }
195                 }       
196                 return null;
197         }
198
199         /** */
200         public MLPPeerSubscription savePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
201                 throw new UnsupportedOperationException();
202         }
203         
204         /** */
205         public boolean updatePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
206                 throw new UnsupportedOperationException();
207         }
208
209         /** */
210         public boolean deletePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
211                 throw new UnsupportedOperationException();
212         }
213
214           /** */
215   public static class FLPPeer extends MLPPeer {
216
217                 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
218     private List<MLPPeerSubscription> subscriptions;
219
220    // @JsonIgnore
221     public List<MLPPeerSubscription> getSubscriptions() {
222       return this.subscriptions;
223     }
224
225     public void setSubscriptions(List<MLPPeerSubscription> theSubscriptions) {
226       this.subscriptions = theSubscriptions;
227     }
228
229                 public String toString() {
230                         return super.toString() + ",subscriptions:" + this.subscriptions;
231                 }
232   }
233
234 }