53abc8c081ea7cb292c757292e34ff6c219312b7
[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.PeerSubscriptionService;
62
63 import org.acumos.cds.domain.MLPPeer;
64 import org.acumos.cds.domain.MLPPeerSubscription;
65
66 import org.apache.commons.io.IOUtils;
67
68 @Service
69 @ConfigurationProperties(prefix="peersLocal")
70 @Conditional(AdapterCondition.class)
71 public class PeerServiceLocalImpl
72                                                                                                 extends AbstractServiceLocalImpl
73                                                                                                 implements PeerService,
74                                                                                                                                          PeerSubscriptionService {
75
76         private List<FLPPeer>           peers;
77
78
79         @PostConstruct
80         public void initPeerService() {
81                 log.debug(EELFLoggerDelegate.debugLogger, "init local peer info service");
82                 checkResource();
83                 try {
84                         watcher.watchOn(this.resource.getURL().toURI(),
85                                                                                         (uri) -> { loadPeersSubscriptionsInfo(); });
86                 }
87                 catch (IOException | URISyntaxException iox) {
88                         log.info(EELFLoggerDelegate.errorLogger, "Peers subscriptions watcher registration failed for " + this.resource, iox);
89                 }
90
91                 loadPeersSubscriptionsInfo();
92
93                 // Done
94                 log.debug(EELFLoggerDelegate.debugLogger, "Local PeerService available");
95         }
96
97         private void loadPeersSubscriptionsInfo() {
98                 log.info(EELFLoggerDelegate.debugLogger, "Loading peers subscriptions from " + this.resource);
99                 synchronized (this) {
100             try {
101                                 ObjectReader objectReader =
102                                 new ObjectMapper().reader(FLPPeer.class);
103         MappingIterator objectIterator =
104                                     objectReader.readValues(this.resource.getURL());
105                                 this.peers = objectIterator.readAll();
106                                 log.info(EELFLoggerDelegate.debugLogger, "loaded " + this.peers.size() + " peers");
107                         }
108                         catch (Exception x) {
109         throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
110                         }
111                 }
112         }
113
114         @PreDestroy
115         public void cleanupPeerService() {
116                 log.debug(EELFLoggerDelegate.debugLogger, "Local peer info service destroyed");
117         }
118
119
120         /** */
121         public List<MLPPeer> getPeers() {
122                 synchronized (this) {
123                         return this.peers == null ? null : 
124                                                                                                                                 this.peers.stream()
125                                                                                                                                                 .map(peer -> (MLPPeer)peer)
126                                                                                                                                                 .collect(Collectors.toList());
127                 }
128         }
129         
130         /** */
131         public List<MLPPeer> getPeer(final String theSubjectName) {
132                 log.info(EELFLoggerDelegate.debugLogger, "Looking for peer " + theSubjectName);
133                 return 
134                         this.peers
135                                                 .stream()
136                                                 .filter(peer -> { 
137                 log.info(EELFLoggerDelegate.debugLogger, "Found peer " + peer.getSubjectName());
138                                                                                                                         return theSubjectName.equals(peer.getSubjectName()); })
139                                                 .collect(Collectors.toList());
140         }
141         
142         /** */
143         public MLPPeer getOnePeer(final String thePeerId) {
144                 MLPPeer apeer =
145                         this.peers
146                                                 .stream()
147                                                 .filter(peer -> thePeerId.equals(peer.getPeerId()))
148                                                 .findFirst()
149                                                 .orElse(null);
150
151                 log.debug(EELFLoggerDelegate.errorLogger, "Local peer info, one peer: " + apeer);
152
153                 return apeer;
154         }
155         
156         /** */
157         public MLPPeer savePeer(MLPPeer mlpPeer) {
158                 throw new UnsupportedOperationException();
159         }
160                 
161         /** */
162         public boolean updatePeer(MLPPeer mlpPeer) {
163                 throw new UnsupportedOperationException();
164         }
165
166         /** */
167         public boolean deletePeer(MLPPeer mlpPeer) {
168                 throw new UnsupportedOperationException();
169         }
170
171         public List<MLPPeerSubscription> getPeerSubscriptions(final String thePeerId) {
172                 FLPPeer peer =
173                         this.peers
174                                                         .stream()
175                                                         .filter(entry -> thePeerId.equals(entry.getPeerId()))
176                                                         .findFirst()
177                                                         .orElse(null);
178                 log.info(EELFLoggerDelegate.errorLogger, "Peer " + thePeerId + " subs:" + (peer == null ? "none" : peer.getSubscriptions()));
179                 return peer == Collections.EMPTY_LIST ? null : peer.getSubscriptions();
180         }
181
182         /** */
183         public MLPPeerSubscription getPeerSubscription(Long theSubId) {
184                 for (FLPPeer peer: this.peers) {
185                         for (MLPPeerSubscription peerSub: peer.getSubscriptions()) {
186                                 if (peerSub.getSubId().equals(theSubId))
187                                         return peerSub;
188                         }
189                 }       
190                 return null;
191         }
192
193         /** */
194         public MLPPeerSubscription savePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
195                 throw new UnsupportedOperationException();
196         }
197         
198         /** */
199         public boolean updatePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
200                 throw new UnsupportedOperationException();
201         }
202
203         /** */
204         public boolean deletePeerSubscription(MLPPeerSubscription mlpPeerSubscription) {
205                 throw new UnsupportedOperationException();
206         }
207
208           /** */
209   public static class FLPPeer extends MLPPeer {
210
211                 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
212     private List<MLPPeerSubscription> subscriptions;
213
214    // @JsonIgnore
215     public List<MLPPeerSubscription> getSubscriptions() {
216       return this.subscriptions;
217     }
218
219     public void setSubscriptions(List<MLPPeerSubscription> theSubscriptions) {
220       this.subscriptions = theSubscriptions;
221     }
222
223                 public String toString() {
224                         return super.toString() + ",subscriptions:" + this.subscriptions;
225                 }
226   }
227
228 }