Merge "Implement framework to process REST notifications"
[ccsdk/sli/plugins.git] / fabric-discovery-plugin / provider / src / main / java / org / onap / ccsdk / sli / plugins / fabricdiscovery / FabricDiscoveryPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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
22 package org.onap.ccsdk.sli.plugins.fabricdiscovery;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.Map;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 public class FabricDiscoveryPlugin implements SvcLogicJavaPlugin, IFabricDiscoveryService {
38
39     private ExecutorService service;
40     private Map<String, WebSocketClient> streamMap;
41     private static final Logger LOG = LoggerFactory.getLogger(FabricDiscoveryPlugin.class);
42
43     public FabricDiscoveryPlugin() {
44         service = Executors.newFixedThreadPool(10);
45         streamMap = new ConcurrentHashMap<String, WebSocketClient> ();
46     }
47
48     @Override
49     public void processDcNotificationStream (Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
50         boolean enable = Boolean.parseBoolean(parseParam(paramMap, "enable", true, null));
51         String stream = parseParam(paramMap, "stream", true, null);
52
53         LOG.info("{} monitoring notification stream: {}", (enable) ? "START" : "STOP", stream);
54         try {
55             service.execute(new Runnable () {
56                 public void run () {
57                     try {
58                         URI uri = new URI(stream);
59                         if (enable) {
60                             if (streamMap.get(stream) != null) {
61                                 LOG.info("Notification Stream: {} is already being monitoried", stream);
62                                 return;
63                             }
64                             IClientMessageCallback messageCallback = new ClientMessageCallback();
65                             WebSocketClient wcClient = new WebSocketClient(uri, messageCallback);
66                             streamMap.put(stream, wcClient);
67                             wcClient.initialize();
68                             try {
69                                 wcClient.connect();
70                             } catch (InterruptedException e) {
71                                 LOG.info("Web Socket Client throws Exception: ", e.getMessage());
72                             }
73                         } else {
74                             WebSocketClient wc = streamMap.get(stream);
75                             if (wc != null) {
76                                 try {
77                                     wc.close("Closing");
78                                 } catch (InterruptedException e) {
79                                     LOG.info("Web Socket Client throws Exception: ", e.getMessage());
80                                 }
81                             }
82                         }
83                     } catch (URISyntaxException e) {
84                         LOG.info("Exception converting stream to URI with: ", e.getMessage());
85                     }
86                 }
87             });
88         } catch (Exception e) {
89             LOG.info("Web Socket client connection throws an exception: ", e.getMessage());
90         }
91     }
92
93     private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
94         throws SvcLogicException {
95         String s = paramMap.get(name);
96
97         if (s == null || s.trim().length() == 0) {
98             if (!required)
99                 return def;
100             throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
101         }
102
103         s = s.trim();
104         String value = "";
105         int i = 0;
106         int i1 = s.indexOf('%');
107         while (i1 >= 0) {
108             int i2 = s.indexOf('%', i1 + 1);
109             if (i2 < 0)
110                 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
111
112             String varName = s.substring(i1 + 1, i2);
113             String varValue = System.getenv(varName);
114             if (varValue == null)
115                 varValue = "";
116
117             value = (new StringBuilder()).append(value)
118                     .append(s.substring(i, i1))
119                     .append(varValue).toString();
120             i = i2 + 1;
121             i1 = s.indexOf('%', i);
122         }
123         value = (new StringBuilder()).append(value)
124                      .append(s.substring(i)).toString();
125
126         LOG.info("Parameter {}: {}", name, value);
127         return value;
128     }
129 }