2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.onap.ccsdk.sli.plugins.fabricdiscovery;
 
  25 import java.net.URISyntaxException;
 
  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;
 
  37 public class FabricDiscoveryPlugin implements SvcLogicJavaPlugin, IFabricDiscoveryService {
 
  39     private ExecutorService service;
 
  40     private Map<String, WebSocketClient> streamMap;
 
  41     private static final Logger LOG = LoggerFactory.getLogger(FabricDiscoveryPlugin.class);
 
  43     public FabricDiscoveryPlugin() {
 
  44         service = Executors.newFixedThreadPool(10);
 
  45         streamMap = new ConcurrentHashMap<String, WebSocketClient> ();
 
  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);
 
  53         LOG.info("{} monitoring notification stream: {}", (enable) ? "START" : "STOP", stream);
 
  55             service.execute(new Runnable () {
 
  58                         URI uri = new URI(stream);
 
  60                             if (streamMap.get(stream) != null) {
 
  61                                 LOG.info("Notification Stream: {} is already being monitoried", stream);
 
  64                             IClientMessageCallback messageCallback = new ClientMessageCallback();
 
  65                             WebSocketClient wcClient = new WebSocketClient(uri, messageCallback);
 
  66                             streamMap.put(stream, wcClient);
 
  67                             wcClient.initialize();
 
  70                             } catch (InterruptedException e) {
 
  71                                 LOG.info("Web Socket Client throws Exception: ", e.getMessage());
 
  74                             WebSocketClient wc = streamMap.get(stream);
 
  78                                 } catch (InterruptedException e) {
 
  79                                     LOG.info("Web Socket Client throws Exception: ", e.getMessage());
 
  83                     } catch (URISyntaxException e) {
 
  84                         LOG.info("Exception converting stream to URI with: ", e.getMessage());
 
  88         } catch (Exception e) {
 
  89             LOG.info("Web Socket client connection throws an exception: ", e.getMessage());
 
  93     private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
 
  94         throws SvcLogicException {
 
  95         String s = paramMap.get(name);
 
  97         if (s == null || s.trim().length() == 0) {
 
 100             throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
 
 106         int i1 = s.indexOf('%');
 
 108             int i2 = s.indexOf('%', i1 + 1);
 
 110                 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
 
 112             String varName = s.substring(i1 + 1, i2);
 
 113             String varValue = System.getenv(varName);
 
 114             if (varValue == null)
 
 117             value = (new StringBuilder()).append(value)
 
 118                     .append(s.substring(i, i1))
 
 119                     .append(varValue).toString();
 
 121             i1 = s.indexOf('%', i);
 
 123         value = (new StringBuilder()).append(value)
 
 124                      .append(s.substring(i)).toString();
 
 126         LOG.info("Parameter {}: {}", name, value);