package org.yamcs.tctm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.yamcs.ConfigurationException;
import org.yamcs.YConfiguration;
import org.yamcs.api.YamcsApiException;
import org.yamcs.yarch.streamsql.ParseException;
import org.yamcs.yarch.streamsql.StreamSqlException;

import com.google.common.util.concurrent.AbstractService;
import com.google.common.util.concurrent.Service;
import com.google.common.util.concurrent.ServiceManager;

/**
 * Service that initialises all the data links
 * 
 * @author nm
 *
 */
public class DataLinkInitialiser extends AbstractService {
    TmDataLinkInitialiser tmDataLinkInitialiser;
    TcUplinkerAdapter tcDataLinkInitialiser;
    PpProviderAdapter ppDataLinkInitialiser;
    ServiceManager  serviceManager;
    
    public DataLinkInitialiser(String yamcsInstance) throws ConfigurationException, StreamSqlException, ParseException, YamcsApiException, IOException {
        YConfiguration c = YConfiguration.getConfiguration("yamcs."+yamcsInstance);
        List<Service> services = new ArrayList<Service>();
        if(c.containsKey(TmDataLinkInitialiser.KEY_tmDataLinks)) {
            tmDataLinkInitialiser = new TmDataLinkInitialiser(yamcsInstance);
            services.add(tmDataLinkInitialiser);
        }
        if(c.containsKey(TcUplinkerAdapter.KEY_tcDataLinks)) {
            tcDataLinkInitialiser = new TcUplinkerAdapter(yamcsInstance);
            services.add(tcDataLinkInitialiser);
        }
        if(c.containsKey(PpProviderAdapter.KEY_ppDataLinks)) {
            ppDataLinkInitialiser = new PpProviderAdapter(yamcsInstance);
            services.add(ppDataLinkInitialiser);
        }
        if(services.isEmpty()) {
            throw new ConfigurationException("None of the "+TmDataLinkInitialiser.KEY_tmDataLinks+", "+TcUplinkerAdapter.KEY_tcDataLinks+" or "+PpProviderAdapter.KEY_ppDataLinks+" configured");
        }
        serviceManager = new ServiceManager(services);
    }

    @Override
    protected void doStart() {
        serviceManager.startAsync();
        serviceManager.awaitHealthy();
        notifyStarted();
    }

    @Override
    protected void doStop() {
        serviceManager.stopAsync();
        serviceManager.awaitStopped();
        notifyStopped();
    }
}

@@ -534,17 +534,16 @@
 links%0A *
- 
 %0A * @aut
@@ -790,20 +790,16 @@
 anager;%0A
-    
 %0A    pub
@@ -1693,17 +1693,19 @@
       if
-(
+ (!
 services
@@ -1726,208 +1726,8 @@
     
-        throw new ConfigurationException(%22None of the %22+TmDataLinkInitialiser.KEY_tmDataLinks+%22, %22+TcUplinkerAdapter.KEY_tcDataLinks+%22 or %22+PpProviderAdapter.KEY_ppDataLinks+%22 configured%22);%0A        %7D%0A
     
@@ -1777,16 +1777,26 @@
 vices);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1835,24 +1835,66 @@
 doStart() %7B%0A
+        if (serviceManager != null) %7B%0A    
         serv
@@ -1910,32 +1910,36 @@
 r.startAsync();%0A
+    
         serviceM
@@ -1957,24 +1957,34 @@
 tHealthy();%0A
+        %7D%0A
         noti
@@ -2043,24 +2043,66 @@
  doStop() %7B%0A
+        if (serviceManager != null) %7B%0A    
         serv
@@ -2117,32 +2117,36 @@
 er.stopAsync();%0A
+    
         serviceM
@@ -2164,24 +2164,34 @@
 tStopped();%0A
+        %7D%0A
         noti

Allow starting Yamcs without any configured links