org.yamcs.YConfiguration - java examples

Here are the examples of the java api org.yamcs.YConfiguration taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

73 Examples 7

19 View Complete Implementation : YarchTestCase.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@BeforeClreplaced
public static void setUpYarch() throws Exception {
    // reset the prefix if maven runs multiple tests
    YConfiguration.setupTest(null);
    // in the same java
    YConfiguration config = YConfiguration.getConfiguration("yamcs");
    if (config.containsKey("littleEndian")) {
        littleEndian = config.getBoolean("littleEndian");
    } else {
        littleEndian = false;
    }
// org.yamcs.LoggingUtils.enableLogging();
}

19 View Complete Implementation : VcDownlinkManagedParameters.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Stores configuration related to Virtual Channels
 * @author nm
 */
public clreplaced VcDownlinkManagedParameters {

    protected int vcId;

    // if set to true, the encapsulation packets sent to the preprocessor will be without the encapsulation header(CCSDS 133.1-B-2)
    boolean stripEncapsulationHeader;

    // if service = M_PDU
    int maxPacketLength;

    String packetPreprocessorClreplacedName;

    YConfiguration packetPreprocessorArgs;

    final YConfiguration config;

    public VcDownlinkManagedParameters(int vcId) {
        this.vcId = vcId;
        this.config = null;
    }

    public VcDownlinkManagedParameters(YConfiguration config) {
        this.config = config;
        this.vcId = config.getInt("vcId");
    }

    protected void parsePacketConfig() {
        maxPacketLength = config.getInt("maxPacketLength", 65536);
        if (maxPacketLength < 7) {
            throw new ConfigurationException("invalid maxPacketLength: " + maxPacketLength);
        }
        packetPreprocessorClreplacedName = config.getString("packetPreprocessorClreplacedName");
        if (config.containsKey("packetPreprocessorArgs")) {
            packetPreprocessorArgs = config.getConfig("packetPreprocessorArgs");
        }
        stripEncapsulationHeader = config.getBoolean("stripEncapsulationHeader", false);
    }
}

19 View Complete Implementation : ArtemisServer.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    // Divert artemis logging
    System.setProperty("org.jboss.logging.provider", "slf4j");
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
    if (config.containsKey("securityManager")) {
        try {
            securityManager = YObjectLoader.loadObject(config.getMap("securityManager"));
        } catch (IOException e) {
            throw new InitException(e);
        }
    }
}

19 View Complete Implementation : AbstractTcDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
protected void initPostprocessor(String instance, YConfiguration config) {
    String commandPostprocessorClreplacedName = GenericCommandPostprocessor.clreplaced.getName();
    YConfiguration commandPostprocessorArgs = null;
    if (config != null) {
        commandPostprocessorClreplacedName = config.getString("commandPostprocessorClreplacedName", GenericCommandPostprocessor.clreplaced.getName());
        if (config.containsKey("commandPostprocessorArgs")) {
            commandPostprocessorArgs = config.getConfig("commandPostprocessorArgs");
        }
    }
    try {
        if (commandPostprocessorArgs != null) {
            cmdPostProcessor = YObjectLoader.loadObject(commandPostprocessorClreplacedName, instance, commandPostprocessorArgs);
        } else {
            cmdPostProcessor = YObjectLoader.loadObject(commandPostprocessorClreplacedName, instance);
        }
    } catch (ConfigurationException e) {
        log.error("Cannot instantiate the command postprocessor", e);
        throw e;
    } catch (IOException e) {
        log.error("Cannot instantiate the command postprocessor", e);
        throw new ConfigurationException(e);
    }
}

19 View Complete Implementation : ArtemisCmdHistoryDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    locator = AbstractArtemisTranslatorService.getServerLocator(yamcsInstance);
}

19 View Complete Implementation : VcUplinkManagedParameters.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Stores configuration related to Virtual Channels for uplink
 *
 * @author nm
 */
public clreplaced VcUplinkManagedParameters {

    protected int vcId;

    String packetPostprocessorClreplacedName;

    YConfiguration packetPostprocessorArgs;

    protected int priority;

    final YConfiguration config;

    public VcUplinkManagedParameters(int vcId) {
        this.vcId = vcId;
        this.config = null;
    }

    public VcUplinkManagedParameters(YConfiguration config) {
        this.config = config;
        this.vcId = config.getInt("vcId");
        this.priority = config.getInt("priority", 1);
    }

    protected void parsePacketConfig() {
        packetPostprocessorClreplacedName = config.getString("packetPostrocessorClreplacedName");
        if (config.containsKey("packetPreprocessorArgs")) {
            packetPostprocessorArgs = config.getConfig("packetPostprocessorArgs");
        }
    }

    public int getPriority() {
        return priority;
    }

    public int getVirtualChannelId() {
        return vcId;
    }
}

19 View Complete Implementation : RealtimeArchiveFiller.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private void parseConfig(YConfiguration config) {
    flushInterval = config.getInt("flushInterval", 300);
    processorName = config.getString("processorName", processorName);
    maxSegmentSize = config.getInt("maxSegmentSize", ArchiveFillerTask.DEFAULT_MAX_SEGMENT_SIZE);
    threshold = config.getInt("orderingThreshold", 20000);
}

19 View Complete Implementation : TcpTcDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private void configure(String yamcsInstance, YConfiguration config) {
    if (config.containsKey("tcHost")) {
        // this is when the config is specified in tcp.yaml
        host = config.getString("tcHost");
        port = config.getInt("tcPort");
    } else {
        host = config.getString("host");
        port = config.getInt("port");
    }
}

18 View Complete Implementation : YarchDatabaseInstance.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private BucketDatabase loadBucketDatabase(YConfiguration config) {
    String clazz = config.getString("clreplaced");
    Object args = config.get("args");
    try {
        if (args == null) {
            bucketDatabase = YObjectLoader.loadObject(clazz, instanceName);
        } else {
            bucketDatabase = YObjectLoader.loadObject(clazz, instanceName, args);
        }
    } catch (IOException e) {
        throw new ConfigurationException("Failed to load bucket database: " + e.getMessage(), e);
    }
    return bucketDatabase;
}

18 View Complete Implementation : ConnectDialog.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private String[] getLocalDbConfigs() throws ConfigurationException {
    YConfiguration conf = YConfiguration.getConfiguration("mdb");
    return conf.getKeys().toArray(new String[0]);
}

18 View Complete Implementation : ParameterArchiveTest.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Test
public void test1() throws Exception {
    // create a parameter in the map
    int p1id = pidMap.createAndGet("/test/p1", Type.BINARY);
    // close and reopen the archive to check that the parameter is still there
    parchive = new ParameterArchive();
    YConfiguration config = parchive.getSpec().validate(YConfiguration.emptyConfig());
    parchive.init(instance, config);
    pidMap = parchive.getParameterIdDb();
    pgidMap = parchive.getParameterGroupIdDb();
    replacedertNotNull(pidMap);
    replacedertNotNull(pgidMap);
    int p2id = pidMap.createAndGet("/test/p2", Type.SINT32);
    replacedertFalse(p1id == p2id);
    replacedertEquals(p1id, pidMap.createAndGet("/test/p1", Type.BINARY));
}

18 View Complete Implementation : JavaAlgorithmEngine.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public AlgorithmExecutorFactory makeExecutorFactory(AlgorithmManager algorithmManager, String language, YConfiguration config) {
    return factory;
}

18 View Complete Implementation : XtceTmRecorder.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
static String getTimeParreplacedioningSchemaSql() {
    YConfiguration yconfig = YamcsServer.getServer().getConfig();
    String partSchema = "";
    if (yconfig.containsKey("archiveConfig", "timeParreplacedioningSchema")) {
        partSchema = "('" + yconfig.getSubString("archiveConfig", "timeParreplacedioningSchema") + "')";
    }
    return partSchema;
}

18 View Complete Implementation : YarchDatabase.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Handles all tables/streams/indexes for a Yamcs server
 */
public clreplaced YarchDatabase {

    YarchDatabase instance;

    // note that this home variable is currently changed in the
    // org.yamcs.cli.CheckConfig
    // to avoid errors when running check config in parallel with a running
    // yamcs server
    private static String home;

    static YConfiguration config;

    private static Map<String, StorageEngine> storageEngines = new HashMap<>();

    public static final String RDB_ENGINE_NAME = "rocksdb2";

    private static final String DEFAULT_STORAGE_ENGINE = RDB_ENGINE_NAME;

    private static final String defaultStorageEngineName;

    static {
        config = YConfiguration.getConfiguration("yamcs");
        if (config.containsKey("dataDir")) {
            home = config.getString("dataDir");
        }
        List<String> se;
        if (config.containsKey("storageEngines")) {
            se = config.getList("storageEngines");
        } else {
            se = Arrays.asList(RDB_ENGINE_NAME);
        }
        if (config.containsKey("defaultStorageEngine")) {
            defaultStorageEngineName = config.getString("defaultStorageEngine");
            if (!RDB_ENGINE_NAME.equalsIgnoreCase(defaultStorageEngineName)) {
                throw new ConfigurationException("Unknown storage engine: " + defaultStorageEngineName);
            }
        } else {
            defaultStorageEngineName = DEFAULT_STORAGE_ENGINE;
        }
        if (se != null) {
            for (String s : se) {
                if (RDB_ENGINE_NAME.equalsIgnoreCase(s)) {
                    storageEngines.put(RDB_ENGINE_NAME, RdbStorageEngine.getInstance());
                } else {
                    throw new ConfigurationException("Unknown storage engine '" + se + "'");
                }
            }
        }
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        try {
            mbeanServer.registerMBean(new BackupControl(), new ObjectName("org.yamcs:name=Backup"));
        } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | MalformedObjectNameException e) {
            throw new RuntimeException(e);
        }
    }

    static Map<String, YarchDatabaseInstance> databases = new HashMap<>();

    /**
     * @param yamcsInstance
     */
    public static synchronized YarchDatabaseInstance getInstance(String yamcsInstance) {
        YarchDatabaseInstance instance = databases.get(yamcsInstance);
        if (instance == null) {
            try {
                instance = new YarchDatabaseInstance(yamcsInstance);
            } catch (YarchException e) {
                throw new RuntimeException("Cannot create database '" + yamcsInstance + "'", e);
            }
            databases.put(yamcsInstance, instance);
        }
        return instance;
    }

    /**
     * Returns the names of the loaded databases.
     */
    public static Set<String> getDatabases() {
        return databases.keySet();
    }

    static public boolean hasInstance(String dbname) {
        return databases.containsKey(dbname);
    }

    public static boolean instanceExistsOnDisk(String yamcsInstance) {
        File dir = new File(getHome() + "/" + yamcsInstance);
        return dir.exists() && dir.isDirectory();
    }

    /**
     * to be used for testing
     *
     * @param dbName
     *            database name to be removed
     */
    public static void removeInstance(String dbName) {
        YarchDatabaseInstance ydb = databases.remove(dbName);
        if (ydb != null) {
            ydb.close();
        }
    }

    public static void setHome(String home) {
        YarchDatabase.home = home;
    }

    public static String getHome() {
        return home;
    }

    public static String getDataDir() {
        return home;
    }

    public static StorageEngine getDefaultStorageEngine() {
        return storageEngines.get(defaultStorageEngineName);
    }

    public static StorageEngine getStorageEngine(String storageEngineName) {
        return storageEngines.get(storageEngineName);
    }

    public static Set<String> getStorageEngineNames() {
        return storageEngines.keySet();
    }

    public static String getDefaultStorageEngineName() {
        return defaultStorageEngineName;
    }
}

18 View Complete Implementation : ArrayAndAggregatesTest.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Before
public void openDb() throws Exception {
    String dbroot = YarchDatabase.getInstance(instance).getRoot();
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot));
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot + ".rdb"));
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot + ".tbs"));
    RdbStorageEngine rse = RdbStorageEngine.getInstance();
    if (rse.getTablespace(instance) != null) {
        rse.dropTablespace(instance);
    }
    rse.createTablespace(instance);
    parchive = new ParameterArchive();
    YConfiguration config = parchive.getSpec().validate(YConfiguration.emptyConfig());
    parchive.init(instance, config);
    pidDb = parchive.getParameterIdDb();
    ParameterGroupIdDb pgidMap = parchive.getParameterGroupIdDb();
    replacedertNotNull(pidDb);
    replacedertNotNull(pgidMap);
    filler = new MyFiller(parchive);
}

18 View Complete Implementation : DirectoryAuthModule.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(YConfiguration args) throws InitException {
}

18 View Complete Implementation : SecurityStore.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private YConfiguration readConfig() throws ValidationException {
    Spec moduleSpec = new Spec();
    moduleSpec.addOption("clreplaced", OptionType.STRING).withRequired(true);
    moduleSpec.addOption("args", OptionType.ANY);
    Spec guestSpec = new Spec();
    guestSpec.addOption("username", OptionType.STRING).withDefault("guest");
    guestSpec.addOption("displayName", OptionType.STRING);
    guestSpec.addOption("superuser", OptionType.BOOLEAN).withDefault(true);
    guestSpec.addOption("privileges", OptionType.ANY);
    Spec spec = new Spec();
    spec.addOption("enabled", OptionType.BOOLEAN).withDeprecationMessage("Remove this argument. If you want to allow guest access, remove security.yaml");
    spec.addOption("blockUnknownUsers", OptionType.BOOLEAN).withDefault(false);
    spec.addOption("authModules", OptionType.LIST).withElementType(OptionType.MAP).withSpec(moduleSpec);
    spec.addOption("guest", OptionType.MAP).withSpec(guestSpec).withAliases(// Legacy, remove some day
    "unauthenticatedUser").withApplySpecDefaults(true);
    YConfiguration yconf = YConfiguration.emptyConfig();
    if (YConfiguration.isDefined("security")) {
        yconf = YConfiguration.getConfiguration("security");
    }
    yconf = spec.validate(yconf);
    return yconf;
}

18 View Complete Implementation : AbstractTmDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public abstract clreplaced AbstractTmDataLink extends AbstractLink implements TmPacketDataLink, SystemParametersProducer {

    protected volatile long packetCount = 0;

    DataRateMeter packetRateMeter = new DataRateMeter();

    DataRateMeter dataRateMeter = new DataRateMeter();

    String packetPreprocessorClreplacedName;

    YConfiguration packetPreprocessorArgs;

    protected PacketPreprocessor packetPreprocessor;

    final protected Log log;

    protected SystemParametersCollector sysParamCollector;

    private String spLinkStatus, spDataCount, spDataRate, spPacketRate;

    final protected TimeService timeService;

    final static String CFG_PREPRO_CLreplaced = "packetPreprocessorClreplacedName";

    protected TmSink tmSink;

    protected AbstractTmDataLink(String instance, String name, YConfiguration config) {
        super(instance, name, config);
        this.timeService = YamcsServer.getTimeService(instance);
        this.log = new Log(this.getClreplaced(), instance);
        log.setContext(name);
    }

    protected void initPreprocessor(String instance, YConfiguration config) {
        if (config != null) {
            if (config.containsKey(CFG_PREPRO_CLreplaced)) {
                this.packetPreprocessorClreplacedName = config.getString(CFG_PREPRO_CLreplaced);
            } else {
                this.packetPreprocessorClreplacedName = IssPacketPreprocessor.clreplaced.getName();
            }
            if (config.containsKey("packetPreprocessorArgs")) {
                this.packetPreprocessorArgs = config.getConfig("packetPreprocessorArgs");
            }
        } else {
            this.packetPreprocessorClreplacedName = IssPacketPreprocessor.clreplaced.getName();
            this.packetPreprocessorArgs = null;
        }
        try {
            if (packetPreprocessorArgs != null) {
                packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance, packetPreprocessorArgs);
            } else {
                packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance);
            }
        } catch (ConfigurationException e) {
            log.error("Cannot instantiate the packet preprocessor", e);
            throw e;
        } catch (IOException e) {
            log.error("Cannot instantiate the packetInput stream", e);
            throw new ConfigurationException(e);
        }
    }

    protected void setupSysVariables() {
        this.sysParamCollector = SystemParametersCollector.getInstance(yamcsInstance);
        if (sysParamCollector != null) {
            sysParamCollector.registerProducer(this);
            spLinkStatus = sysParamCollector.getNamespace() + "/" + name + "/linkStatus";
            spDataCount = sysParamCollector.getNamespace() + "/" + name + "/dataCount";
            spDataRate = sysParamCollector.getNamespace() + "/" + name + "/dataRate";
            spPacketRate = sysParamCollector.getNamespace() + "/" + name + "/packetRate";
        } else {
            log.info("System variables collector not defined for instance {} ", yamcsInstance);
        }
    }

    @Override
    public Collection<ParameterValue> getSystemParameters() {
        long time = timeService.getMissionTime();
        ParameterValue linkStatus = SystemParametersCollector.getPV(spLinkStatus, time, getLinkStatus().name());
        ParameterValue dataCount = SystemParametersCollector.getPV(spDataCount, time, packetCount);
        ParameterValue dataRate = SystemParametersCollector.getPV(spDataRate, time, dataRateMeter.getFiveSecondsRate());
        ParameterValue packetRate = SystemParametersCollector.getPV(spPacketRate, time, packetRateMeter.getFiveSecondsRate());
        return Arrays.asList(linkStatus, dataCount, dataRate, packetRate);
    }

    @Override
    public long getDataInCount() {
        return packetCount;
    }

    @Override
    public long getDataOutCount() {
        return 0;
    }

    @Override
    public void setTmSink(TmSink tmSink) {
        this.tmSink = tmSink;
    }

    /**
     * called when a new packet is received to update the statistics
     *
     * @param packetSize
     */
    protected void updateStats(int packetSize) {
        packetCount++;
        packetRateMeter.mark(1);
        dataRateMeter.mark(packetSize);
    }

    @Override
    public void resetCounters() {
        packetCount = 0;
    }
}

18 View Complete Implementation : XtceDbFactory.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public static synchronized XtceDb createInstanceByConfig(String configSection, boolean attemptToLoadSerialized) throws ConfigurationException, DatabaseLoadException {
    YConfiguration c = YConfiguration.getConfiguration("mdb");
    if (configSection == null) {
        configSection = c.getFirstEntry();
    }
    List<Object> list = c.getList(configSection);
    return createInstance(list, attemptToLoadSerialized, true);
}

17 View Complete Implementation : IndexHandler.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Handler that always responds with the contents of the index.html file of the webapp. The file is generated
 * dynamically because we do some templating on it.
 */
@Sharable
public clreplaced IndexHandler extends Handler {

    private YConfiguration config;

    private HttpServer httpServer;

    private Path indexFile;

    private String cachedHtml;

    private FileTime cacheTime;

    public IndexHandler(YConfiguration config, HttpServer httpServer, Path webRoot) {
        this.config = config;
        this.httpServer = httpServer;
        indexFile = webRoot.resolve("index.html");
    }

    @Override
    public void handle(ChannelHandlerContext ctx, FullHttpRequest req) {
        if (req.method() != HttpMethod.GET) {
            HttpRequestHandler.sendPlainTextError(ctx, req, METHOD_NOT_ALLOWED);
            return;
        }
        if (!Files.exists(indexFile)) {
            HttpRequestHandler.sendPlainTextError(ctx, req, NOT_FOUND);
            return;
        }
        String html = null;
        try {
            html = getHtml();
        } catch (IOException e) {
            HttpRequestHandler.sendPlainTextError(ctx, req, INTERNAL_SERVER_ERROR);
            return;
        }
        ByteBuf body = ctx.alloc().buffer();
        body.writeCharSequence(html, StandardCharsets.UTF_8);
        HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, body);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, MediaType.HTML);
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, body.readableBytes());
        // Recommend clients to not cache this file. We hash all of our
        // web files, and this reduces likelihood of attempting to load
        // the app from an outdated index.html.
        response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-store, must-revalidate");
        HttpRequestHandler.sendResponse(ctx, req, response, true);
    }

    private synchronized String getHtml() throws IOException {
        FileTime lastModified = Files.getLastModifiedTime(indexFile);
        if (!lastModified.equals(cacheTime)) {
            cachedHtml = processTemplate();
            cacheTime = lastModified;
        }
        return cachedHtml;
    }

    @SuppressWarnings("unchecked")
    private String processTemplate() throws IOException {
        String template = new String(Files.readAllBytes(indexFile), StandardCharsets.UTF_8);
        Map<String, Object> webConfig = new HashMap<>(config.toMap());
        AuthInfo authInfo = AuthHandler.createAuthInfo();
        String authJson = JsonFormat.printer().print(authInfo);
        Map<String, Object> authMap = new Gson().fromJson(authJson, Map.clreplaced);
        webConfig.put("auth", authMap);
        webConfig.put("serverId", YamcsServer.getServer().getServerId());
        Map<String, Object> args = new HashMap<>(4);
        args.put("contextPath", httpServer.getContextPath());
        args.put("config", webConfig);
        args.put("configJson", new Gson().toJson(webConfig));
        return TemplateProcessor.process(template, args);
    }
}

17 View Complete Implementation : AlarmRecorder.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
    try {
        if (ydb.getTable(PARAMETER_ALARM_TABLE_NAME) == null) {
            String cols = StandardTupleDefinitions.PARAMETER_ALARM.getStringDefinition1();
            String query = "create table " + PARAMETER_ALARM_TABLE_NAME + "(" + cols + ", primary key(triggerTime, parameter, seqNum)) table_format=compressed";
            ydb.execute(query);
        }
        setupRecording(yamcsInstance, PARAMETER_ALARM_TABLE_NAME, StandardStreamType.parameterAlarm);
        if (ydb.getTable(EVENT_ALARM_TABLE_NAME) == null) {
            String cols = StandardTupleDefinitions.EVENT_ALARM.getStringDefinition1();
            String query = "create table " + EVENT_ALARM_TABLE_NAME + "(" + cols + ", primary key(triggerTime, eventSource, seqNum)) table_format=compressed";
            ydb.execute(query);
        }
        setupRecording(yamcsInstance, EVENT_ALARM_TABLE_NAME, StandardStreamType.eventAlarm);
    } catch (ParseException | StreamSqlException e) {
        throw new InitException(e);
    }
}

17 View Complete Implementation : ParameterArchive.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    timeService = YamcsServer.getTimeService(yamcsInstance);
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
    tablespace = RdbStorageEngine.getInstance().getTablespace(ydb);
    if (config.containsKey("backFiller")) {
        backFillerConfig = config.getConfig("backFiller");
        log.debug("backFillerConfig: {}", backFillerConfig);
        backFillerEnabled = backFillerConfig.getBoolean("enabled", true);
    }
    if (config.containsKey("realtimeFiller")) {
        realtimeFillerConfig = config.getConfig("realtimeFiller");
        realtimeFillerEnabled = realtimeFillerConfig.getBoolean("enabled", false);
        log.debug("realtimeFillerConfig: {}", realtimeFillerConfig);
    }
    String schema = config.getString("parreplacedioningSchema");
    if (!"none".equalsIgnoreCase(schema)) {
        parreplacedioningSchema = TimeParreplacedionSchema.getInstance(schema);
    }
    if (!config.containsKey("backFiller") && !config.containsKey("realtimeFiller")) {
        backFiller = new BackFiller(this, null);
    }
    try {
        TablespaceRecord.Type trType = TablespaceRecord.Type.PARCHIVE_PINFO;
        List<TablespaceRecord> trl = tablespace.filter(trType, yamcsInstance, trb -> true);
        if (trl.size() > 1) {
            throw new DatabaseCorruptionException("More than one tablespace record of type " + trType.name() + " for instance " + yamcsInstance);
        }
        parameterIdMap = new ParameterIdDb(yamcsInstance, tablespace);
        parameterGroupIdMap = new ParameterGroupIdDb(yamcsInstance, tablespace);
        TablespaceRecord tr;
        if (trl.isEmpty()) {
            // new database
            initializeDb();
        } else {
            // existing database
            tr = trl.get(0);
            parreplacedionTbsIndex = tr.getTbsIndex();
            if (tr.hasParreplacedioningSchema()) {
                parreplacedioningSchema = TimeParreplacedionSchema.getInstance(tr.getParreplacedioningSchema());
            }
            readParreplacedions();
        }
        if (parreplacedioningSchema == null) {
            parreplacedions.insert(new Parreplacedion());
        }
    } catch (RocksDBException | IOException e) {
        throw new InitException(e);
    }
}

17 View Complete Implementation : AbstractLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Abstract link implementation as a {@link Service} handling the basic enable/disable getConfig operations
 *
 * @author nm
 */
public abstract clreplaced AbstractLink extends AbstractService implements Link {

    protected final String yamcsInstance;

    protected final String name;

    protected final Log log;

    protected final EventProducer eventProducer;

    protected final YConfiguration config;

    protected final AtomicBoolean disabled = new AtomicBoolean(false);

    /**
     * singleton for netty worker group. In the future we may have an option to create different worker groups for
     * different links but for now we stick to one.
     */
    static NioEventLoopGroup nelg = new NioEventLoopGroup();

    public AbstractLink(String instance, String name, YConfiguration config) throws ConfigurationException {
        this.yamcsInstance = instance;
        this.name = name;
        this.config = config;
        log = new Log(getClreplaced(), instance);
        log.setContext(name);
        eventProducer = EventProducerFactory.getEventProducer(yamcsInstance, getClreplaced().getSimpleName(), 10000);
    }

    @Override
    public YConfiguration getConfig() {
        return config;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Status getLinkStatus() {
        if (isDisabled()) {
            return Status.DISABLED;
        }
        if (state() == State.FAILED) {
            return Status.FAILED;
        }
        return connectionStatus();
    }

    @Override
    public String getDetailedStatus() {
        return "";
    }

    protected static NioEventLoopGroup getEventLoop() {
        return nelg;
    }

    /**
     * Sets the disabled to false such that getNextPacket does not ignore the received datagrams
     */
    @Override
    public void enable() {
        boolean b = disabled.getAndSet(false);
        if (b) {
            try {
                doEnable();
            } catch (Exception e) {
                disabled.set(true);
                log.warn("Failed to enable link", e);
            }
        }
    }

    @Override
    public void disable() {
        boolean b = disabled.getAndSet(true);
        if (!b) {
            try {
                doDisable();
            } catch (Exception e) {
                disabled.set(false);
                log.warn("Failed to disable link", e);
            }
        }
    }

    @Override
    public boolean isDisabled() {
        return disabled.get();
    }

    protected abstract void doDisable() throws Exception;

    protected abstract void doEnable() throws Exception;

    /**
     * In case the link should be connected (i.e. is running and enabled) this method is called to return the actuall connection status
     */
    protected abstract Status connectionStatus();
}

17 View Complete Implementation : AbstractPacketPreprocessor.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public static ErrorDetectionWordCalculator getErrorDetectionWordCalculator(YConfiguration config) {
    if ((config == null) || !config.containsKey(CONFIG_KEY_ERROR_DETECTION)) {
        return null;
    }
    YConfiguration c = config.getConfig(CONFIG_KEY_ERROR_DETECTION);
    String type = c.getString("type");
    if ("16-SUM".equalsIgnoreCase(type)) {
        return new Running16replacedecksumCalculator();
    } else if ("CRC-16-CCIIT".equalsIgnoreCase(type)) {
        return new CrcCciitCalculator(c);
    } else {
        throw new ConfigurationException("Unknown errorDetectionWord type '" + type + "': supported types are 16-SUM and CRC-16-CCIIT");
    }
}

17 View Complete Implementation : AbstractTmDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
protected void initPreprocessor(String instance, YConfiguration config) {
    if (config != null) {
        if (config.containsKey(CFG_PREPRO_CLreplaced)) {
            this.packetPreprocessorClreplacedName = config.getString(CFG_PREPRO_CLreplaced);
        } else {
            this.packetPreprocessorClreplacedName = IssPacketPreprocessor.clreplaced.getName();
        }
        if (config.containsKey("packetPreprocessorArgs")) {
            this.packetPreprocessorArgs = config.getConfig("packetPreprocessorArgs");
        }
    } else {
        this.packetPreprocessorClreplacedName = IssPacketPreprocessor.clreplaced.getName();
        this.packetPreprocessorArgs = null;
    }
    try {
        if (packetPreprocessorArgs != null) {
            packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance, packetPreprocessorArgs);
        } else {
            packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance);
        }
    } catch (ConfigurationException e) {
        log.error("Cannot instantiate the packet preprocessor", e);
        throw e;
    } catch (IOException e) {
        log.error("Cannot instantiate the packetInput stream", e);
        throw new ConfigurationException(e);
    }
}

17 View Complete Implementation : DataLinkInitialiser.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String instanceName, YConfiguration config) throws InitException {
    super.init(instanceName, config);
    ydb = YarchDatabase.getInstance(instanceName);
    YamcsServerInstance instance = YamcsServer.getServer().getInstance(instanceName);
    YConfiguration instanceConfig = instance.getConfig();
    if (instanceConfig.containsKey("dataLinks")) {
        try {
            List<YConfiguration> linkConfigs = instanceConfig.getConfigList("dataLinks");
            for (YConfiguration linkConfig : linkConfigs) {
                createDataLink(linkConfig);
            }
        } catch (IOException e) {
            throw new InitException(e);
        }
    }
}

17 View Complete Implementation : DataLinkInitialiser.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private void createDataLink(YConfiguration linkConfig) throws IOException {
    String clreplacedName = linkConfig.getString("clreplaced");
    YConfiguration args = null;
    args = linkConfig.getConfig("args");
    String name = linkConfig.getString("name");
    if (linksByName.containsKey(name)) {
        throw new ConfigurationException("Instance " + yamcsInstance + ": there is already a link named '" + name + "'");
    }
    // this is maintained for compatibility with Dreplaced which defines no stream name because the config is specified
    // in a separate file
    String linkLevelStreamName = null;
    if (linkConfig.containsKey("stream")) {
        log.warn("DEPRECATION ALERT: Define 'stream' under 'args'.");
        linkLevelStreamName = linkConfig.getString("stream");
    }
    Link link;
    if (args != null) {
        link = YObjectLoader.loadObject(clreplacedName, yamcsInstance, name, args);
    } else {
        link = YObjectLoader.loadObject(clreplacedName, yamcsInstance, name);
    }
    boolean enabledAtStartup = linkConfig.getBoolean("enabledAtStartup", true);
    if (!enabledAtStartup) {
        link.disable();
    }
    configureDataLink(link, args, linkLevelStreamName);
}

17 View Complete Implementation : UdpParameterDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Receives PP data via UDP.
 *
 * The UDP packets are protobuf encoded ParameterData. We don't use any checksum, replacedume it's done by UDP.
 *
 * @author nm
 */
public clreplaced UdpParameterDataLink extends AbstractExecutionThreadService implements ParameterDataLink {

    private volatile int validDatagramCount = 0;

    private volatile int invalidDatagramCount = 0;

    private volatile boolean disabled = false;

    private int sequenceCount = 0;

    private TimeService timeService;

    private DatagramSocket udpSocket;

    private int port = 31002;

    private String defaultRecordingGroup;

    ParameterSink parameterSink;

    private Log log;

    int MAX_LENGTH = 10 * 1024;

    DatagramPacket datagram = new DatagramPacket(new byte[MAX_LENGTH], MAX_LENGTH);

    YConfiguration config;

    final String name;

    /**
     * Creates a new UDP data link
     *
     * @param instance
     * @param name
     * @param config
     * @throws ConfigurationException
     *             if port is not defined in the config
     */
    public UdpParameterDataLink(String instance, String name, YConfiguration config) throws ConfigurationException {
        this.config = config;
        this.name = name;
        log = new Log(getClreplaced(), instance);
        log.setContext(name);
        timeService = YamcsServer.getTimeService(instance);
        port = config.getInt("port");
        defaultRecordingGroup = config.getString("recordingGroup", "DEFAULT");
    }

    @Override
    public void startUp() throws IOException {
        udpSocket = new DatagramSocket(port);
    }

    @Override
    public void run() {
        while (isRunning()) {
            ParameterData pdata = getNextData();
            if (pdata == null) {
                continue;
            }
            if (pdata.hasGenerationTime()) {
                log.error("Generation time must be specified for each parameter separately");
                continue;
            }
            long now = timeService.getMissionTime();
            String recgroup = pdata.hasGroup() ? pdata.getGroup() : defaultRecordingGroup;
            int sequenceNumber = pdata.hreplacedeqNum() ? pdata.getSeqNum() : sequenceCount++;
            // Regroup by gentime, just in case multiple parameters are submitted with different times.
            Map<Long, List<ParameterValue>> valuesByTime = new LinkedHashMap<>();
            for (ParameterValue pval : pdata.getParameterList()) {
                long gentime = now;
                if (pval.hasGenerationTimeUTC()) {
                    gentime = TimeEncoding.parse(pval.getGenerationTimeUTC());
                    pval = ParameterValue.newBuilder(pval).clearGenerationTimeUTC().setGenerationTime(gentime).build();
                } else if (!pval.hasGenerationTime()) {
                    pval = ParameterValue.newBuilder(pval).setGenerationTime(now).build();
                }
                List<ParameterValue> pvals = valuesByTime.computeIfAbsent(gentime, x -> new ArrayList<>());
                pvals.add(pval);
            }
            for (Entry<Long, List<ParameterValue>> group : valuesByTime.entrySet()) {
                parameterSink.updateParams(group.getKey(), recgroup, sequenceNumber, group.getValue());
            }
        }
    }

    /**
     * Called to retrieve the next packet. It blocks in reading on the UDP socket.
     *
     * @return anything that looks as a valid packet, just the size is taken into account to decide if it's valid or not
     */
    public ParameterData getNextData() {
        while (isRunning() && disabled) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return null;
            }
        }
        try {
            udpSocket.receive(datagram);
            ParameterData.Builder pdb = ParameterData.newBuilder().mergeFrom(datagram.getData(), datagram.getOffset(), datagram.getLength());
            validDatagramCount++;
            return pdb.build();
        } catch (IOException e) {
            log.warn("Exception when receiving parameter data: {}'", e.getMessage());
            invalidDatagramCount++;
        }
        return null;
    }

    @Override
    public Status getLinkStatus() {
        return disabled ? Status.DISABLED : Status.OK;
    }

    /**
     * Returns statistics with the number of datagrams received and the number of invalid datagrams
     */
    @Override
    public String getDetailedStatus() {
        if (disabled) {
            return "DISABLED";
        } else {
            return String.format("OK (%s)\nValid datagrams received: %d\nInvalid datagrams received: %d", port, validDatagramCount, invalidDatagramCount);
        }
    }

    /**
     * Sets the disabled to true such that getNextPacket ignores the received datagrams
     */
    @Override
    public void disable() {
        disabled = true;
    }

    /**
     * Sets the disabled to false such that getNextPacket does not ignore the received datagrams
     */
    @Override
    public void enable() {
        disabled = false;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public long getDataInCount() {
        return validDatagramCount;
    }

    @Override
    public long getDataOutCount() {
        return 0;
    }

    @Override
    public void resetCounters() {
        validDatagramCount = 0;
    }

    @Override
    public void setParameterSink(ParameterSink parameterSink) {
        this.parameterSink = parameterSink;
    }

    @Override
    public YConfiguration getConfig() {
        return config;
    }

    @Override
    public String getName() {
        return name;
    }
}

17 View Complete Implementation : ColumnSerializerFactory.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public clreplaced ColumnSerializerFactory {

    static YConfiguration config;

    static int maxBinaryLength = 1048576;

    static Logger log = LoggerFactory.getLogger(ColumnSerializer.clreplaced.getName());

    static final BooleanColumnSerializer BOOLEAN_CS = new BooleanColumnSerializer();

    static final ByteColumnSerializer BYTE_CS = new ByteColumnSerializer();

    static final ShortColumnSerializer SHORT_CS = new ShortColumnSerializer();

    static final IntegerColumnSerializer INT_CS = new IntegerColumnSerializer();

    static final LongColumnSerializer LONG_CS = new LongColumnSerializer();

    static final DoubleColumnSerializer DOUBLE_CS = new DoubleColumnSerializer();

    static final StringColumnSerializer STRING_CS = new StringColumnSerializer();

    static final BinaryColumnSerializer BINARY_CS = new BinaryColumnSerializer();

    static final ParameterValueColumnSerializer PARAMETER_VALUE_CS = new ParameterValueColumnSerializer();

    static Map<String, ProtobufColumnSerializer> protoSerialziers = new HashMap<>();

    static {
        config = YConfiguration.getConfiguration("yamcs");
        if (config.containsKey("maxBinaryLength")) {
            maxBinaryLength = config.getInt("maxBinaryLength");
        }
    }

    public static ColumnSerializer<?> getColumnSerializer(TableDefinition tblDef, ColumnDefinition cd) {
        DataType type = cd.getType();
        if (type.val == _type.ENUM) {
            return new EnumColumnSerializer(tblDef, cd);
        } else if (type.val == _type.PROTOBUF) {
            return getProtobufSerializer(cd);
        } else {
            return getBasicColumnSerializer(cd.getType());
        }
    }

    /**
     * returns a column serializer for basic types
     *
     * @param type
     * @return
     */
    @SuppressWarnings("incomplete-switch")
    public static ColumnSerializer<?> getBasicColumnSerializer(DataType type) {
        switch(type.val) {
            case BOOLEAN:
                return BOOLEAN_CS;
            case BYTE:
                return BYTE_CS;
            case SHORT:
                return SHORT_CS;
            case INT:
                return INT_CS;
            case DOUBLE:
                return DOUBLE_CS;
            case TIMESTAMP:
            case // intentional fall through
            LONG:
                return LONG_CS;
            case STRING:
                return STRING_CS;
            case BINARY:
                return BINARY_CS;
            case PARAMETER_VALUE:
                return PARAMETER_VALUE_CS;
            case LIST:
            case TUPLE:
                // TODO
                throw new UnsupportedOperationException("List and Tuple not implemented");
        }
        throw new IllegalArgumentException("' " + type + " is not a basic type");
    }

    static public ColumnSerializer<?> getProtobufSerializer(ColumnDefinition cd) {
        String clreplacedName = ((ProtobufDataType) cd.getType()).getClreplacedName();
        synchronized (protoSerialziers) {
            ProtobufColumnSerializer pcs = protoSerialziers.get(clreplacedName);
            if (pcs != null) {
                return pcs;
            }
            Clreplaced<?> c;
            try {
                c = Clreplaced.forName(clreplacedName);
                Method newBuilderMethod = c.getMethod("newBuilder");
                pcs = new ProtobufColumnSerializer(newBuilderMethod);
                protoSerialziers.put(clreplacedName, pcs);
                return pcs;
            } catch (ClreplacedNotFoundException e) {
                throw new IllegalArgumentException("Cannot find clreplaced '" + clreplacedName + "' required to deserialize column '" + cd.getName() + "'", e);
            } catch (NoSuchMethodException e) {
                throw new IllegalArgumentException("Clreplaced '" + clreplacedName + "' required to deserialize column '" + cd.getName() + "' does not have a method newBuilder", e);
            }
        }
    }

    static abstract clreplaced AbstractColumnSerializer<T> implements ColumnSerializer<T> {

        int size;

        public AbstractColumnSerializer(int size) {
            this.size = size;
        }

        @Override
        public T fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            DataInputStream dos = new DataInputStream(new ByteArrayInputStream(b));
            return deserialize(dos, cd);
        }

        @Override
        public byte[] toByteArray(T v) {
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream(size)) {
                DataOutputStream dos = new DataOutputStream(baos);
                serialize(dos, v);
                return baos.toByteArray();
            } catch (IOException e) {
                throw new UncheckedIOException("cannot serialize in memory?", e);
            }
        }
    }

    static clreplaced BooleanColumnSerializer implements ColumnSerializer<Boolean> {

        @Override
        public Boolean deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readBoolean();
        }

        @Override
        public void serialize(DataOutputStream stream, Boolean v) throws IOException {
            stream.writeBoolean((Boolean) v);
        }

        @Override
        public byte[] toByteArray(Boolean v) {
            boolean b = (Boolean) v;
            return new byte[] { (byte) (b ? 1 : 0) };
        }

        @Override
        public Boolean fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            return b[0] == 1;
        }
    }

    static clreplaced ByteColumnSerializer implements ColumnSerializer<Byte> {

        @Override
        public Byte deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readByte();
        }

        @Override
        public void serialize(DataOutputStream stream, Byte v) throws IOException {
            stream.writeByte((Byte) v);
        }

        @Override
        public byte[] toByteArray(Byte v) {
            return new byte[] { v };
        }

        @Override
        public Byte fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            return b[0];
        }
    }

    static clreplaced ShortColumnSerializer implements ColumnSerializer<Short> {

        @Override
        public Short deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readShort();
        }

        @Override
        public void serialize(DataOutputStream stream, Short v) throws IOException {
            stream.writeShort((Short) v);
        }

        @Override
        public byte[] toByteArray(Short v) {
            short s = v;
            return new byte[] { (byte) ((s >> 8) & 0xFF), (byte) (s & 0xFF) };
        }

        @Override
        public Short fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            return (short) (((b[0] & 0xFF) << 8) + (b[1] & 0xFF));
        }
    }

    static clreplaced IntegerColumnSerializer implements ColumnSerializer<Integer> {

        @Override
        public Integer deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readInt();
        }

        @Override
        public void serialize(DataOutputStream stream, Integer v) throws IOException {
            stream.writeInt((Integer) v);
        }

        @Override
        public byte[] toByteArray(Integer v) {
            int x = v;
            return new byte[] { (byte) ((x >> 24) & 0xFF), (byte) ((x >> 16) & 0xFF), (byte) ((x >> 8) & 0xFF), (byte) (x & 0xFF) };
        }

        @Override
        public Integer fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            return (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3];
        }
    }

    static clreplaced DoubleColumnSerializer extends AbstractColumnSerializer<Double> {

        public DoubleColumnSerializer() {
            super(8);
        }

        @Override
        public Double deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readDouble();
        }

        @Override
        public void serialize(DataOutputStream stream, Double v) throws IOException {
            stream.writeDouble(v);
        }
    }

    static clreplaced LongColumnSerializer extends AbstractColumnSerializer<Long> {

        public LongColumnSerializer() {
            super(8);
        }

        @Override
        public Long deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readLong();
        }

        @Override
        public void serialize(DataOutputStream stream, Long v) throws IOException {
            stream.writeLong(v);
        }
    }

    static clreplaced StringColumnSerializer extends AbstractColumnSerializer<String> {

        public StringColumnSerializer() {
            super(32);
        }

        @Override
        public String deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            return stream.readUTF();
        }

        @Override
        public void serialize(DataOutputStream stream, String v) throws IOException {
            stream.writeUTF(v);
        }
    }

    static clreplaced BinaryColumnSerializer implements ColumnSerializer<byte[]> {

        @Override
        public byte[] deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            int length = stream.readInt();
            if (length > maxBinaryLength) {
                log.warn("binary length greater than maxBinaryLenght (is the endianess wrong?): ?>?", length, maxBinaryLength);
                return null;
            }
            byte[] bp = new byte[length];
            stream.readFully(bp);
            return bp;
        }

        @Override
        public void serialize(DataOutputStream stream, byte[] v) throws IOException {
            stream.writeInt(v.length);
            stream.write(v);
        }

        @Override
        public byte[] toByteArray(byte[] v) {
            byte[] r = new byte[4 + v.length];
            ByteArrayUtils.encodeInt(v.length, r, 0);
            System.arraycopy(v, 0, r, 4, v.length);
            return r;
        }

        @Override
        public byte[] fromByteArray(byte[] b, ColumnDefinition cd) throws IOException {
            byte[] r = new byte[b.length - 4];
            System.arraycopy(b, 4, r, 0, r.length);
            return r;
        }
    }

    static clreplaced ProtobufColumnSerializer extends AbstractColumnSerializer<MessageLite> {

        // for columns of type PROTOBUF
        private final Method newBuilderMethod;

        public ProtobufColumnSerializer(Method newBuilderMethod) {
            super(32);
            this.newBuilderMethod = newBuilderMethod;
        }

        @Override
        public MessageLite deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            int length = stream.readInt();
            if (length > maxBinaryLength) {
                log.warn("binary length greater than maxBinaryLenght (is the endianess wrong?): ?>?", length, maxBinaryLength);
                throw new IOException("binary length greater than maxBinaryLength");
            }
            byte[] bp = new byte[length];
            stream.readFully(bp);
            return readProtobufMessage(bp);
        }

        @Override
        public void serialize(DataOutputStream stream, MessageLite v) throws IOException {
            byte[] b = v.toByteArray();
            stream.writeInt(b.length);
            stream.write(b);
        }

        private MessageLite readProtobufMessage(byte[] bp) throws InvalidProtocolBufferException {
            try {
                Builder b = (Builder) newBuilderMethod.invoke(null);
                b.mergeFrom(bp);
                return b.build();
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new IllegalStateException(e);
            }
        }
    }

    static clreplaced EnumColumnSerializer extends AbstractColumnSerializer<String> {

        private final TableDefinition tblDef;

        // for columns of type ENUM
        private volatile BiMap<String, Short> enumValues;

        private final String columnName;

        public EnumColumnSerializer(TableDefinition tblDef, ColumnDefinition cd) {
            super(2);
            this.tblDef = tblDef;
            this.columnName = cd.getName();
        }

        @Override
        public String deserialize(DataInputStream stream, ColumnDefinition cd) throws IOException {
            short x = stream.readShort();
            return enumValues.inverse().get(x);
        }

        @Override
        public void serialize(DataOutputStream stream, String v) throws IOException {
            Short v1;
            if ((enumValues == null) || (v1 = enumValues.get(v)) == null) {
                tblDef.addEnumValue(this, v);
                serialize(stream, v);
                return;
            }
            stream.writeShort(v1);
        }

        void setEnumValues(BiMap<String, Short> enumValues) {
            this.enumValues = enumValues;
        }

        public String getColumnName() {
            return columnName;
        }
    }
}

17 View Complete Implementation : YarchTestCase.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Before
public void setUp() throws Exception {
    YConfiguration config = YConfiguration.getConfiguration("yamcs");
    Path dir = Paths.get(config.getString("dataDir"));
    instance = "yarchtest_" + this.getClreplaced().getSimpleName();
    context = new ExecutionContext(instance);
    if (YarchDatabase.hasInstance(instance)) {
        YarchDatabase.removeInstance(instance);
        RdbStorageEngine rse = RdbStorageEngine.getInstance();
        if (rse.getTablespace(instance) != null) {
            rse.dropTablespace(instance);
        }
    }
    Path ytdir = dir.resolve(instance);
    Path rdbdir = dir.resolve(instance + ".rdb");
    FileUtils.deleteRecursivelyIfExists(ytdir);
    FileUtils.deleteRecursivelyIfExists(rdbdir);
    if (!ytdir.toFile().mkdirs()) {
        throw new IOException("Cannot create directory " + ytdir);
    }
    ydb = YarchDatabase.getInstance(instance);
}

17 View Complete Implementation : TseCommander.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public static void main(String[] args) {
    TseCommanderArgs runtimeOptions = new TseCommanderArgs();
    new JCommander(runtimeOptions).parse(args);
    configureLogging();
    TimeEncoding.setUp();
    YConfiguration.setResolver(new FileBasedConfigurationResolver(runtimeOptions.configDirectory));
    YConfiguration yconf = YConfiguration.getConfiguration("tse");
    List<Service> services = createServices(yconf, runtimeOptions);
    ServiceManager serviceManager = new ServiceManager(services);
    serviceManager.addListener(new ServiceManager.Listener() {

        @Override
        public void failure(Service service) {
            // Stop entire process as soon as one service fails.
            System.exit(1);
        }
    });
    // Allow services to shutdown gracefully
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                serviceManager.stopAsync().awaitStopped(10, TimeUnit.SECONDS);
            } catch (TimeoutException e) {
            // ignore
            }
        }
    });
    serviceManager.startAsync();
}

17 View Complete Implementation : TsePlugin.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Starts the TseCommander. These can be configured in tse.yaml
 */
private void addTseCommander(YamcsServer yamcs) throws PluginException {
    YConfiguration yconf = YConfiguration.getConfiguration("tse");
    try {
        yamcs.addGlobalService("TSE Commander", TseCommander.clreplaced, yconf);
    } catch (ValidationException e) {
        throw new PluginException("Invalid configuration: " + e.getMessage());
    } catch (IOException e) {
        throw new PluginException("Could not start TSE Commander", e);
    }
}

16 View Complete Implementation : PacketViewer.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
protected void readConfig(String instance, YConfiguration config) {
    String packetPreprocessorClreplacedName = config.getString(CFG_PREPRO_CLreplaced, IssPacketPreprocessor.clreplaced.getName());
    try {
        if (config.containsKey("packetPreprocessorArgs")) {
            YConfiguration packetPreprocessorArgs = config.getConfig("packetPreprocessorArgs");
            packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance, packetPreprocessorArgs);
        } else {
            packetPreprocessor = YObjectLoader.loadObject(packetPreprocessorClreplacedName, instance);
        }
    } catch (ConfigurationException e) {
        log.error("Cannot instantiate the packet preprocessor", e);
        throw e;
    } catch (IOException e) {
        log.error("Cannot instantiate the packet preprocessor", e);
        throw new ConfigurationException(e);
    }
    if (config.containsKey("packetInputStreamClreplacedName")) {
        this.packetInputStreamClreplacedName = config.getString("packetInputStreamClreplacedName");
    } else {
        this.packetInputStreamClreplacedName = CcsdsPacketInputStream.clreplaced.getName();
    }
    this.packetInputStreamArgs = config.get("packetInputStreamArgs");
}

16 View Complete Implementation : AbstractArtemisTranslatorService.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
public static ServerLocator getServerLocator(String instance) {
    // for compatibility with old yamcs
    String artemisUrl = "vm:///";
    YamcsServer yamcs = YamcsServer.getServer();
    YamcsServerInstance yamcsInstance = yamcs.getInstance(instance);
    YConfiguration config = yamcsInstance.getConfig();
    if (config.containsKey(ARTEMIS_URL_KEY)) {
        artemisUrl = config.getString(ARTEMIS_URL_KEY);
    } else {
        config = yamcs.getConfig();
        if (config.containsKey(ARTEMIS_URL_KEY)) {
            artemisUrl = config.getString(ARTEMIS_URL_KEY);
        }
    }
    try {
        return ActiveMQClient.createServerLocator(artemisUrl);
    } catch (Exception e) {
        throw new ConfigurationException("Cannot create Artemis connection", e);
    }
}

16 View Complete Implementation : ArtemisParameterDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * receives data from ActiveMQ Artemis and publishes it into a yamcs stream
 *
 * @author nm
 */
public clreplaced ArtemisParameterDataLink extends AbstractService implements ParameterDataLink, MessageHandler {

    protected AtomicLong totalPpCount = new AtomicLong(0);

    protected volatile boolean disabled = false;

    protected Log log;

    private ParameterSink ppListener;

    final XtceDb ppdb;

    final String artemisAddress;

    ClientSession artemisSession;

    ServerLocator locator;

    ClientSessionFactory factory;

    YConfiguration config;

    final String linkName;

    ClientConsumer client;

    public ArtemisParameterDataLink(String instance, String name, String artemisAddress) throws ConfigurationException {
        log = new Log(getClreplaced(), instance);
        log.setContext(name);
        ppdb = XtceDbFactory.getInstance(instance);
        this.artemisAddress = artemisAddress;
        this.linkName = name;
        locator = AbstractArtemisTranslatorService.getServerLocator(instance);
    }

    public ArtemisParameterDataLink(String instance, String name, YConfiguration config) {
        this(instance, name, config.getString("address"));
        this.config = config;
    }

    @Override
    public void setParameterSink(ParameterSink ppListener) {
        this.ppListener = ppListener;
    }

    @Override
    public Status getLinkStatus() {
        if (disabled) {
            return Status.DISABLED;
        } else {
            return Status.OK;
        }
    }

    @Override
    public void disable() {
        disabled = true;
    }

    @Override
    public void enable() {
        disabled = false;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public String getDetailedStatus() {
        if (disabled) {
            return "DISABLED";
        } else {
            return "OK";
        }
    }

    @Override
    public long getDataInCount() {
        return totalPpCount.get();
    }

    @Override
    public long getDataOutCount() {
        return 0;
    }

    @Override
    public void resetCounters() {
        totalPpCount.set(0);
    }

    @Override
    public void onMessage(ClientMessage msg) {
        try {
            msg.acknowledge();
            if (disabled) {
                return;
            }
            ParameterData pd = (ParameterData) Protocol.decode(msg, ParameterData.newBuilder());
            long genTime;
            if (pd.hasGenerationTime()) {
                genTime = pd.getGenerationTime();
            } else {
                Long l = msg.getLongProperty(StandardTupleDefinitions.PARAMETER_COL_GENTIME);
                if (l != null) {
                    genTime = l;
                } else {
                    log.warn("Cannot find generation time either in the body or in the header of the message");
                    return;
                }
            }
            String ppGroup;
            if (pd.hasGroup()) {
                ppGroup = pd.getGroup();
            } else {
                ppGroup = msg.getStringProperty(StandardTupleDefinitions.PARAMETER_COL_GROUP);
                if (ppGroup == null) {
                    log.warn("Cannot find PP group either in the body or in the header of the message");
                    return;
                }
            }
            totalPpCount.addAndGet(pd.getParameterCount());
            ppListener.updateParams(genTime, ppGroup, pd.getSeqNum(), pd.getParameterList());
        } catch (Exception e) {
            log.warn("{} for message: {}", e.getMessage(), msg);
        }
    }

    @Override
    protected void doStart() {
        try {
            factory = locator.createSessionFactory();
            artemisSession = factory.createSession();
            String queue = artemisAddress + "-ArtemisPpProvider";
            log.debug("Starting artemis parameter data link connected to {}.{}", artemisAddress, queue);
            artemisSession.createTemporaryQueue(artemisAddress, queue);
            client = artemisSession.createConsumer(queue, AbstractArtemisTranslatorService.UNIQUEID_HDR_NAME + "<>" + AbstractArtemisTranslatorService.UNIQUEID);
            client.setMessageHandler(this);
            artemisSession.start();
            notifyStarted();
        } catch (Exception e) {
            log.error("Failed connect to artemis");
            notifyFailed(e);
        }
    }

    @Override
    protected void doStop() {
        try {
            artemisSession.close();
            notifyStopped();
        } catch (ActiveMQException e) {
            log.error("Got exception when quiting:", e);
            notifyFailed(e);
        }
    }

    @Override
    public String getName() {
        return linkName;
    }

    @Override
    public YConfiguration getConfig() {
        return config;
    }
}

16 View Complete Implementation : ParameterRecorder.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
    try {
        String cols = PARAMETER.getStringDefinition1();
        if (ydb.getTable(TABLE_NAME) == null) {
            String query = "create table " + TABLE_NAME + "(" + cols + ", primary key(gentime, seqNum)) histogram(" + PARAMETER_COL_GROUP + ") parreplacedion by time_and_value(gentime" + XtceTmRecorder.getTimeParreplacedioningSchemaSql() + ",group) table_format=compressed";
            ydb.execute(query);
        }
        StreamConfig sc = StreamConfig.getInstance(yamcsInstance);
        if (!config.containsKey("streams")) {
            List<StreamConfigEntry> sceList = sc.getEntries(StandardStreamType.param);
            for (StreamConfigEntry sce : sceList) {
                streams.add(sce.getName());
                ydb.execute("insert_append into " + TABLE_NAME + " select * from " + sce.getName());
            }
        } else if (config.containsKey("streams")) {
            List<String> streamNames = config.getList("streams");
            for (String sn : streamNames) {
                StreamConfigEntry sce = sc.getEntry(StandardStreamType.param, sn);
                if (sce == null) {
                    throw new ConfigurationException("No stream config found for '" + sn + "'");
                }
                streams.add(sce.getName());
                ydb.execute("insert_append into " + TABLE_NAME + " select * from " + sce.getName());
            }
        }
    } catch (ParseException | StreamSqlException e) {
        throw new InitException("Exception when creating parameter input stream", e);
    }
}

16 View Complete Implementation : CfdpService.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
public void init(String yamcsInstance, YConfiguration config) throws InitException {
    super.init(yamcsInstance, config);
    String inStream = config.getString("inStream");
    String outStream = config.getString("outStream");
    mySourceId = config.getLong("sourceId");
    destinationId = config.getLong("destinationId");
    YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
    cfdpIn = ydb.getStream(inStream);
    if (cfdpIn == null) {
        throw new ConfigurationException("cannot find stream " + inStream);
    }
    cfdpOut = ydb.getStream(outStream);
    if (cfdpOut == null) {
        throw new ConfigurationException("cannot find stream " + outStream);
    }
    cfdpIn.addSubscriber(this);
    String bucketName = config.getString("incomingBucket");
    YarchDatabaseInstance globalYdb = YarchDatabase.getInstance(YamcsServer.GLOBAL_INSTANCE);
    try {
        incomingBucket = globalYdb.getBucket(bucketName);
        if (incomingBucket == null) {
            incomingBucket = globalYdb.createBucket(bucketName);
        }
    } catch (IOException e) {
        throw new InitException(e);
    }
    eventProducer = EventProducerFactory.getEventProducer(yamcsInstance, "CfdpService", 10000);
}

16 View Complete Implementation : HttpRequestHandler.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Handles handshakes and messages.
 *
 * We have following different request types
 * <ul>
 * <li>static requests - sent to the fileRequestHandler - do no go higher in the netty pipeline</li>
 * <li>websocket requests - the pipeline is modified to add the websocket handshaker.</li>
 * <li>load data requests - the pipeline is modified by the respective route handler</li>
 * <li>standard API calls (the vast majority) - the HttpObjectAgreggator is added upstream to collect (and limit) all
 * data from the http request in one object.</li>
 * </ul>
 * Because we support multiple http requests on one connection (keep-alive), we have to clean the pipeline when the
 * request type changes
 */
public clreplaced HttpRequestHandler extends ChannelInboundHandlerAdapter {

    public static final String ANY_PATH = "*";

    private static final String API_PATH = "api";

    private static final String AUTH_PATH = "auth";

    private static final String STATIC_PATH = "static";

    private static final String WEBSOCKET_PATH = "_websocket";

    public static final AttributeKey<User> CTX_USER = AttributeKey.valueOf("user");

    private static final Log log = new Log(HttpRequestHandler.clreplaced);

    public static final Object CONTENT_FINISHED_EVENT = new Object();

    private static StaticFileHandler fileRequestHandler = new StaticFileHandler();

    private static final FullHttpResponse BAD_REQUEST = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.EMPTY_BUFFER);

    public static final String HANDLER_NAME_COMPRESSOR = "hndl_compressor";

    public static final String HANDLER_NAME_CHUNKED_WRITER = "hndl_chunked_writer";

    public static final byte[] NEWLINE_BYTES = "\r\n".getBytes();

    static {
        HttpUtil.setContentLength(BAD_REQUEST, 0);
    }

    private static TokenStore tokenStore = new TokenStore();

    private HttpServer httpServer;

    private String contextPath;

    private Router apiRouter;

    private AuthHandler authHandler;

    private HttpAuthorizationChecker authChecker;

    private boolean contentExpected = false;

    YConfiguration wsConfig;

    public HttpRequestHandler(HttpServer httpServer, Router apiRouter) {
        this.httpServer = httpServer;
        this.apiRouter = apiRouter;
        wsConfig = httpServer.getConfig().getConfig("webSocket");
        contextPath = httpServer.getContextPath();
        authHandler = new AuthHandler(tokenStore, contextPath);
        authChecker = new HttpAuthorizationChecker(tokenStore);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof HttpMessage) {
            DecoderResult dr = ((HttpMessage) msg).decoderResult();
            if (!dr.isSuccess()) {
                log.warn("{} Exception while decoding http message: {}", ctx.channel().id().replacedhortText(), dr.cause());
                ctx.writeAndFlush(BAD_REQUEST);
                return;
            }
        }
        if (msg instanceof HttpRequest) {
            contentExpected = false;
            processRequest(ctx, (HttpRequest) msg);
            ReferenceCountUtil.release(msg);
        } else if (msg instanceof HttpContent) {
            if (contentExpected) {
                ctx.fireChannelRead(msg);
                if (msg instanceof LastHttpContent) {
                    ctx.fireUserEventTriggered(CONTENT_FINISHED_EVENT);
                }
            } else if (!(msg instanceof LastHttpContent)) {
                log.warn("{} unexpected http content received: {}", ctx.channel().id().replacedhortText(), msg);
                ReferenceCountUtil.release(msg);
                ctx.close();
            }
        } else {
            log.error("{} unexpected message received: {}", ctx.channel().id().replacedhortText(), msg);
            ReferenceCountUtil.release(msg);
        }
    }

    private void processRequest(ChannelHandlerContext ctx, HttpRequest req) {
        // We have this also on info level coupled with the HTTP response status
        // code, but this is on debug for an earlier reporting while debugging issues
        log.debug("{} {} {}", ctx.channel().id().replacedhortText(), req.method(), req.uri());
        try {
            handleRequest(ctx, req);
        } catch (IOException e) {
            log.warn("Exception while handling http request", e);
            sendPlainTextError(ctx, req, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        } catch (HttpException e) {
            sendPlainTextError(ctx, req, e.getStatus(), e.getMessage());
        }
    }

    private void handleRequest(ChannelHandlerContext ctx, HttpRequest req) throws IOException, HttpException {
        cleanPipeline(ctx.pipeline());
        if (!req.uri().startsWith(contextPath)) {
            sendPlainTextError(ctx, req, NOT_FOUND);
            return;
        }
        String pathString = HttpUtils.getPathWithoutContext(req, contextPath);
        // Note: pathString starts with / so path[0] is always empty
        String[] path = pathString.split("/", 3);
        switch(path[1]) {
            case STATIC_PATH:
                if (path.length == 2) {
                    // do not accept "/static/" (i.e. directory listing) requests
                    sendPlainTextError(ctx, req, FORBIDDEN);
                    return;
                }
                fileRequestHandler.handleStaticFileRequest(ctx, req, path[2]);
                return;
            case AUTH_PATH:
                ctx.pipeline().addLast(HANDLER_NAME_COMPRESSOR, new HttpContentCompressor());
                ctx.pipeline().addLast(new HttpObjectAggregator(65536));
                ctx.pipeline().addLast(authHandler);
                ctx.fireChannelRead(req);
                contentExpected = true;
                return;
            case API_PATH:
                verifyAuthentication(ctx, req);
                contentExpected = apiRouter.scheduleExecution(ctx, req, pathString);
                return;
            case WEBSOCKET_PATH:
                verifyAuthentication(ctx, req);
                if (path.length == 2) {
                    // No instance specified
                    prepareChannelForWebSocketUpgrade(ctx, req, null, null);
                } else {
                    path = path[2].split("/", 2);
                    if (YamcsServer.hasInstance(path[0])) {
                        if (path.length == 1) {
                            prepareChannelForWebSocketUpgrade(ctx, req, path[0], null);
                        } else {
                            prepareChannelForWebSocketUpgrade(ctx, req, path[0], path[1]);
                        }
                    } else {
                        sendPlainTextError(ctx, req, NOT_FOUND);
                    }
                }
                return;
        }
        // Maybe a plugin registered a custom handler
        Handler handler = httpServer.createHandler(path[1]);
        if (handler == null) {
            handler = httpServer.createHandler(ANY_PATH);
        }
        if (handler != null) {
            ctx.pipeline().addLast(HANDLER_NAME_COMPRESSOR, new HttpContentCompressor());
            ctx.pipeline().addLast(new HttpObjectAggregator(65536));
            ctx.pipeline().addLast(handler);
            ctx.fireChannelRead(req);
            contentExpected = true;
            return;
        }
        // Too bad.
        sendPlainTextError(ctx, req, NOT_FOUND);
    }

    private void verifyAuthentication(ChannelHandlerContext ctx, HttpRequest req) throws HttpException {
        User user = authChecker.verifyAuth(ctx, req);
        ctx.channel().attr(CTX_USER).set(user);
    }

    /**
     * Adapts Netty's pipeline for allowing WebSocket upgrade
     *
     * @param ctx
     *            context for this channel handler
     */
    private void prepareChannelForWebSocketUpgrade(ChannelHandlerContext ctx, HttpRequest req, String yamcsInstance, String processor) {
        contentExpected = true;
        ctx.pipeline().addLast(new HttpObjectAggregator(65536));
        int maxFrameLength = wsConfig.getInt("maxFrameLength");
        int maxDropped = wsConfig.getInt("connectionCloseNumDroppedMsg");
        int lo = wsConfig.getConfig("writeBufferWaterMark").getInt("low");
        int hi = wsConfig.getConfig("writeBufferWaterMark").getInt("high");
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(lo, hi);
        // Add websocket-specific handlers to channel pipeline
        String webSocketPath = req.uri();
        String subprotocols = "json, protobuf";
        ctx.pipeline().addLast(new WebSocketServerProtocolHandler(webSocketPath, subprotocols, false, maxFrameLength));
        HttpRequestInfo originalRequestInfo = new HttpRequestInfo(req);
        originalRequestInfo.setYamcsInstance(yamcsInstance);
        originalRequestInfo.setProcessor(processor);
        originalRequestInfo.setUser(ctx.channel().attr(CTX_USER).get());
        ctx.pipeline().addLast(new WebSocketFrameHandler(originalRequestInfo, maxDropped, waterMark));
        // Effectively trigger websocket-handler (will attempt handshake)
        ctx.fireChannelRead(req);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause instanceof NotSslRecordException) {
            log.info("Non TLS connection (HTTP?) attempted on the HTTPS port, closing channel");
        } else {
            log.error("Closing channel: {}", cause.getMessage());
        }
        ctx.close();
    }

    public static <T extends Message> ChannelFuture sendMessageResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponseStatus status, T responseMsg) {
        return sendMessageResponse(ctx, req, status, responseMsg, true);
    }

    public static <T extends Message> ChannelFuture sendMessageResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponseStatus status, T responseMsg, boolean autoCloseOnError) {
        ByteBuf body = ctx.alloc().buffer();
        MediaType contentType = getAcceptType(req);
        try {
            if (contentType == MediaType.PROTOBUF) {
                try (ByteBufOutputStream channelOut = new ByteBufOutputStream(body)) {
                    responseMsg.writeTo(channelOut);
                }
            } else if (contentType == MediaType.PLAIN_TEXT) {
                body.writeCharSequence(responseMsg.toString(), StandardCharsets.UTF_8);
            } else {
                // JSON by default
                contentType = MediaType.JSON;
                String str = JsonFormat.printer().preservingProtoFieldNames().print(responseMsg);
                body.writeCharSequence(str, StandardCharsets.UTF_8);
                // For curl comfort
                body.writeBytes(NEWLINE_BYTES);
            }
        } catch (IOException e) {
            return sendPlainTextError(ctx, req, HttpResponseStatus.INTERNAL_SERVER_ERROR, e.toString());
        }
        HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, body);
        HttpUtils.setContentTypeHeader(response, contentType);
        int txSize = body.readableBytes();
        HttpUtil.setContentLength(response, txSize);
        return sendResponse(ctx, req, response, autoCloseOnError);
    }

    public static ChannelFuture sendPlainTextError(ChannelHandlerContext ctx, HttpRequest req, HttpResponseStatus status) {
        return sendPlainTextError(ctx, req, status, status.toString());
    }

    public static ChannelFuture sendPlainTextError(ChannelHandlerContext ctx, HttpRequest req, HttpResponseStatus status, String msg) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer(msg + "\r\n", CharsetUtil.UTF_8));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
        return sendResponse(ctx, req, response, true);
    }

    public static ChannelFuture sendResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse response, boolean autoCloseOnError) {
        if (response.status() == HttpResponseStatus.OK) {
            log.info("{} {} {} {}", ctx.channel().id().replacedhortText(), req.method(), req.uri(), response.status().code());
            ChannelFuture writeFuture = ctx.writeAndFlush(response);
            if (!HttpUtil.isKeepAlive(req)) {
                writeFuture.addListener(ChannelFutureListener.CLOSE);
            }
            return writeFuture;
        } else {
            if (req != null) {
                log.warn("{} {} {} {}", ctx.channel().id().replacedhortText(), req.method(), req.uri(), response.status().code());
            } else {
                log.warn("{} malformed or illegal request. Sending back {}", ctx.channel().id().replacedhortText(), response.status().code());
            }
            ChannelFuture writeFuture = ctx.writeAndFlush(response);
            if (autoCloseOnError) {
                writeFuture = writeFuture.addListener(ChannelFutureListener.CLOSE);
            }
            return writeFuture;
        }
    }

    private void cleanPipeline(ChannelPipeline pipeline) {
        while (pipeline.last() != this) {
            pipeline.removeLast();
        }
    }

    /**
     * Returns the Accept header if present and not set to ANY or Content-Type header if present or JSON if none of the
     * headers is present or the Accept is present and set to ANY.
     */
    private static MediaType getAcceptType(HttpRequest req) {
        String acceptType = req.headers().get(HttpHeaderNames.ACCEPT);
        if (acceptType != null) {
            MediaType r = MediaType.from(acceptType);
            if (r == MediaType.ANY) {
                return getContentType(req);
            } else {
                return r;
            }
        } else {
            return getContentType(req);
        }
    }

    /**
     * @return The Content-Type header if present or else defaults to JSON.
     */
    public static MediaType getContentType(HttpRequest req) {
        String declaredContentType = req.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (declaredContentType != null) {
            return MediaType.from(declaredContentType);
        }
        return MediaType.JSON;
    }

    /**
     * Sends base HTTP response indicating the use of chunked transfer encoding NM 11-May-2018: We do not put the
     * ChunckedWriteHandler on the pipeline because the input is already chunked.
     */
    public static ChannelFuture startChunkedTransfer(ChannelHandlerContext ctx, HttpRequest req, MediaType contentType, String filename) {
        log.info("{} {} {} 200 starting chunked transfer", ctx.channel().id().replacedhortText(), req.method(), req.uri());
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
        response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
        // Set Content-Disposition header so that supporting clients will treat
        // response as a downloadable file
        if (filename != null) {
            response.headers().set("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        }
        return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
    }

    public static ChannelFuture writeChunk(ChannelHandlerContext ctx, ByteBuf buf) throws IOException {
        Channel ch = ctx.channel();
        if (!ch.isOpen()) {
            throw new ClosedChannelException();
        }
        ChannelFuture writeFuture = ctx.writeAndFlush(new DefaultHttpContent(buf));
        try {
            if (!ch.isWritable()) {
                boolean writeCompleted = writeFuture.await(10, TimeUnit.SECONDS);
                if (!writeCompleted) {
                    throw new IOException("Channel did not become writable in 10 seconds");
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return writeFuture;
    }

    public static clreplaced ChunkedTransferStats {

        public int totalBytes = 0;

        public int chunkCount = 0;

        HttpMethod originalMethod;

        String originalUri;

        public ChunkedTransferStats(HttpMethod method, String uri) {
            originalMethod = method;
            originalUri = uri;
        }
    }
}

16 View Complete Implementation : ParameterArchive.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * The parameter archive stores data in parreplacedions -> intervals -> segments. A parreplacedion covers one year and each
 * parreplacedion has its own RocksDB database
 *
 * An interval covers 2^23 millisec =~ 139 minutes
 *
 * An segment covers at most maxSegmentSize values for one parameter
 *
 * @author nm
 */
public clreplaced ParameterArchive extends AbstractYamcsService {

    public static final boolean STORE_RAW_VALUES = true;

    // 2^23 millisecons =~ 139 minutes per interval
    public static final int NUMBITS_MASK = 23;

    public static final int TIMESTAMP_MASK = (0xFFFFFFFF >>> (32 - NUMBITS_MASK));

    public static final long INTERVAL_MASK = ~TIMESTAMP_MASK;

    private ParameterIdDb parameterIdMap;

    private ParameterGroupIdDb parameterGroupIdMap;

    private Tablespace tablespace;

    TimeParreplacedionSchema parreplacedioningSchema;

    // the tbsIndex used to encode parreplacedion information
    int parreplacedionTbsIndex;

    private ParreplacedionedTimeInterval<Parreplacedion> parreplacedions = new ParreplacedionedTimeInterval<>();

    SegmentEncoderDecoder vsEncoder = new SegmentEncoderDecoder();

    TimeService timeService;

    private BackFiller backFiller;

    private RealtimeArchiveFiller realtimeFiller;

    YConfiguration realtimeFillerConfig;

    YConfiguration backFillerConfig;

    boolean realtimeFillerEnabled;

    boolean backFillerEnabled;

    @Override
    public Spec getSpec() {
        Spec spec = new Spec();
        spec.addOption("backFiller", OptionType.ANY);
        spec.addOption("realtimeFiller", OptionType.ANY);
        spec.addOption("parreplacedioningSchema", OptionType.STRING).withDefault("YYYY").withChoices("YYYY/DOY", "YYYY/MM", "YYYY", "none");
        return spec;
    }

    @Override
    public void init(String yamcsInstance, YConfiguration config) throws InitException {
        super.init(yamcsInstance, config);
        timeService = YamcsServer.getTimeService(yamcsInstance);
        YarchDatabaseInstance ydb = YarchDatabase.getInstance(yamcsInstance);
        tablespace = RdbStorageEngine.getInstance().getTablespace(ydb);
        if (config.containsKey("backFiller")) {
            backFillerConfig = config.getConfig("backFiller");
            log.debug("backFillerConfig: {}", backFillerConfig);
            backFillerEnabled = backFillerConfig.getBoolean("enabled", true);
        }
        if (config.containsKey("realtimeFiller")) {
            realtimeFillerConfig = config.getConfig("realtimeFiller");
            realtimeFillerEnabled = realtimeFillerConfig.getBoolean("enabled", false);
            log.debug("realtimeFillerConfig: {}", realtimeFillerConfig);
        }
        String schema = config.getString("parreplacedioningSchema");
        if (!"none".equalsIgnoreCase(schema)) {
            parreplacedioningSchema = TimeParreplacedionSchema.getInstance(schema);
        }
        if (!config.containsKey("backFiller") && !config.containsKey("realtimeFiller")) {
            backFiller = new BackFiller(this, null);
        }
        try {
            TablespaceRecord.Type trType = TablespaceRecord.Type.PARCHIVE_PINFO;
            List<TablespaceRecord> trl = tablespace.filter(trType, yamcsInstance, trb -> true);
            if (trl.size() > 1) {
                throw new DatabaseCorruptionException("More than one tablespace record of type " + trType.name() + " for instance " + yamcsInstance);
            }
            parameterIdMap = new ParameterIdDb(yamcsInstance, tablespace);
            parameterGroupIdMap = new ParameterGroupIdDb(yamcsInstance, tablespace);
            TablespaceRecord tr;
            if (trl.isEmpty()) {
                // new database
                initializeDb();
            } else {
                // existing database
                tr = trl.get(0);
                parreplacedionTbsIndex = tr.getTbsIndex();
                if (tr.hasParreplacedioningSchema()) {
                    parreplacedioningSchema = TimeParreplacedionSchema.getInstance(tr.getParreplacedioningSchema());
                }
                readParreplacedions();
            }
            if (parreplacedioningSchema == null) {
                parreplacedions.insert(new Parreplacedion());
            }
        } catch (RocksDBException | IOException e) {
            throw new InitException(e);
        }
    }

    private void initializeDb() throws RocksDBException {
        log.debug("initializing db");
        TablespaceRecord.Builder trb = TablespaceRecord.newBuilder().setType(Type.PARCHIVE_PINFO);
        if (parreplacedioningSchema != null) {
            trb.setParreplacedioningSchema(parreplacedioningSchema.getName());
        }
        TablespaceRecord tr = tablespace.createMetadataRecord(yamcsInstance, trb);
        parreplacedionTbsIndex = tr.getTbsIndex();
    }

    public TimeParreplacedionSchema getParreplacedioningSchema() {
        return parreplacedioningSchema;
    }

    private void readParreplacedions() throws IOException, RocksDBException {
        YRDB db = tablespace.getRdb();
        byte[] range = new byte[TBS_INDEX_SIZE];
        ByteArrayUtils.encodeInt(parreplacedionTbsIndex, range, 0);
        try (AscendingRangeIterator it = new AscendingRangeIterator(db.newIterator(), range, false, range, false)) {
            while (it.isValid()) {
                TimeBasedParreplacedion tbp = TimeBasedParreplacedion.parseFrom(it.value());
                Parreplacedion p = new Parreplacedion(tbp.getParreplacedionStart(), tbp.getParreplacedionEnd(), tbp.getParreplacedionDir());
                Parreplacedion p1 = parreplacedions.insert(p, 0);
                if (p1 == null) {
                    throw new DatabaseCorruptionException("Parreplacedion " + p + " overlaps with existing parreplacedions");
                }
                it.next();
            }
        }
    }

    static long decodeParreplacedionId(String prefix, String cfname) {
        try {
            return Long.parseLong(cfname.substring(prefix.length()), 16);
        } catch (NumberFormatException e) {
            throw new ParameterArchiveException("Cannot decode parreplacedion id from column family: " + cfname);
        }
    }

    public ParameterIdDb getParameterIdDb() {
        return parameterIdMap;
    }

    public ParameterGroupIdDb getParameterGroupIdDb() {
        return parameterGroupIdMap;
    }

    public void writeToArchive(PGSegment pgs) throws RocksDBException, IOException {
        pgs.consolidate();
        Parreplacedion p = createAndGetParreplacedion(pgs.getSegmentStart());
        try (WriteBatch writeBatch = new WriteBatch();
            WriteOptions wo = new WriteOptions()) {
            writeToBatch(writeBatch, p, pgs);
            tablespace.getRdb(p.parreplacedionDir, false).getDb().write(wo, writeBatch);
        }
    }

    public void writeToArchive(long segStart, Collection<PGSegment> pgList) throws RocksDBException, IOException {
        Parreplacedion p = createAndGetParreplacedion(segStart);
        try (WriteBatch writeBatch = new WriteBatch();
            WriteOptions wo = new WriteOptions()) {
            for (PGSegment pgs : pgList) {
                pgs.consolidate();
                replacedert (segStart == pgs.getSegmentStart());
                writeToBatch(writeBatch, p, pgs);
            }
            tablespace.getRdb(p.parreplacedionDir, false).getDb().write(wo, writeBatch);
        }
    }

    private void writeToBatch(WriteBatch writeBatch, Parreplacedion p, PGSegment pgs) throws RocksDBException {
        // write the time segment
        SortedTimeSegment timeSegment = pgs.getTimeSegment();
        byte[] timeKey = new SegmentKey(parameterIdMap.timeParameterId, pgs.getParameterGroupId(), pgs.getSegmentStart(), SegmentKey.TYPE_ENG_VALUE).encode();
        byte[] timeValue = vsEncoder.encode(timeSegment);
        writeBatch.put(timeKey, timeValue);
        // and then the consolidated value segments
        List<BaseSegment> consolidated = pgs.getConsolidatedValueSegments();
        List<BaseSegment> consolidatedRawValues = pgs.getConsolidatedRawValueSegments();
        List<ParameterStatusSegment> satusSegments = pgs.getConsolidatedParameterStatusSegments();
        for (int i = 0; i < consolidated.size(); i++) {
            BaseSegment vs = consolidated.get(i);
            int parameterId = pgs.getParameterId(i);
            String pname = parameterIdMap.getParameterFqnById(parameterId);
            if (vs.size() != timeSegment.size()) {
                throw new IllegalArgumentException("Trying to write to archive an engineering value segment whose size (" + vs.size() + ") is different than the time segment (" + timeSegment.size() + ") " + "for parameterId: " + parameterId + "(" + pname + ") and segment: [" + TimeEncoding.toString(timeSegment.getSegmentStart()) + " - " + TimeEncoding.toString(timeSegment.getSegmentEnd()) + "]");
            }
            byte[] engKey = new SegmentKey(parameterId, pgs.getParameterGroupId(), pgs.getSegmentStart(), SegmentKey.TYPE_ENG_VALUE).encode();
            byte[] engValue = vsEncoder.encode(vs);
            writeBatch.put(engKey, engValue);
            if (STORE_RAW_VALUES && consolidatedRawValues != null) {
                BaseSegment rvs = consolidatedRawValues.get(i);
                if (rvs != null) {
                    if (rvs.size() != timeSegment.size()) {
                        throw new IllegalArgumentException("Trying to write to archive an raw value segment whose size (" + rvs.size() + ") is different than the time segment (" + timeSegment.size() + ") " + "for parameterId: " + parameterId + "(" + pname + ") and segment: [" + TimeEncoding.toString(timeSegment.getSegmentStart()) + " - " + TimeEncoding.toString(timeSegment.getSegmentEnd()) + "]");
                    }
                    byte[] rawKey = new SegmentKey(parameterId, pgs.getParameterGroupId(), pgs.getSegmentStart(), SegmentKey.TYPE_RAW_VALUE).encode();
                    byte[] rawValue = vsEncoder.encode(rvs);
                    writeBatch.put(rawKey, rawValue);
                }
            }
            ParameterStatusSegment pss = satusSegments.get(i);
            if (pss.size() != timeSegment.size()) {
                throw new IllegalArgumentException("Trying to write to archive an parameter status segment whose size (" + pss.size() + ") is different than the time segment (" + timeSegment.size() + ") " + "for parameterId: " + parameterId + "(" + pname + ") and segment: [" + TimeEncoding.toString(timeSegment.getSegmentStart()) + " - " + TimeEncoding.toString(timeSegment.getSegmentEnd()) + "]");
            }
            byte[] pssKey = new SegmentKey(parameterId, pgs.getParameterGroupId(), pgs.getSegmentStart(), SegmentKey.TYPE_PARAMETER_STATUS).encode();
            byte[] pssValue = vsEncoder.encode(pss);
            writeBatch.put(pssKey, pssValue);
        }
    }

    /**
     * get parreplacedion for segment, creating it if it doesn't exist
     *
     * @param segStart
     * @throws RocksDBException
     */
    public Parreplacedion createAndGetParreplacedion(long segStart) throws RocksDBException {
        synchronized (parreplacedions) {
            Parreplacedion p = parreplacedions.getFit(segStart);
            if (p == null) {
                TimeParreplacedionInfo pinfo = parreplacedioningSchema.getParreplacedionInfo(segStart);
                p = new Parreplacedion(pinfo.getStart(), pinfo.getEnd(), pinfo.getDir());
                p = parreplacedions.insert(p, 60000L);
                replacedert p != null;
                TimeBasedParreplacedion tbp = TimeBasedParreplacedion.newBuilder().setParreplacedionDir(p.parreplacedionDir).setParreplacedionStart(p.getStart()).setParreplacedionEnd(p.getEnd()).build();
                byte[] key = new byte[TBS_INDEX_SIZE + 8];
                ByteArrayUtils.encodeInt(parreplacedionTbsIndex, key, 0);
                ByteArrayUtils.encodeLong(pinfo.getStart(), key, TBS_INDEX_SIZE);
                tablespace.putData(key, tbp.toByteArray());
            }
            return p;
        }
    }

    public Future<?> reprocess(long start, long stop) {
        log.debug("Scheduling a reprocess for interval [{} - {}]", TimeEncoding.toString(start), TimeEncoding.toString(stop));
        if (backFiller == null) {
            throw new ConfigurationException("backFilling is not enabled");
        }
        return backFiller.scheduleFillingTask(start, stop);
    }

    /**
     * a copy of the parreplacedions from start to stop inclusive
     *
     * @param start
     * @param stop
     * @return a sorted list of parreplacedions
     */
    public List<Parreplacedion> getParreplacedions(long start, long stop, boolean ascending) {
        List<Parreplacedion> r = new ArrayList<>();
        Iterator<Parreplacedion> it;
        if (ascending) {
            it = parreplacedions.overlappingIterator(new TimeInterval(start, stop));
        } else {
            it = parreplacedions.overlappingReverseIterator(new TimeInterval(start, stop));
        }
        while (it.hasNext()) {
            r.add(it.next());
        }
        return r;
    }

    @Override
    protected void doStart() {
        if (backFillerEnabled) {
            backFiller = new BackFiller(this, backFillerConfig);
            backFiller.start();
        }
        if (realtimeFillerEnabled) {
            realtimeFiller = new RealtimeArchiveFiller(this, realtimeFillerConfig);
            realtimeFiller.startAsync();
        }
        notifyStarted();
    }

    @Override
    protected void doStop() {
        log.debug("Stopping ParameterArchive service for instance {}", yamcsInstance);
        if (backFiller != null) {
            backFiller.stop();
        }
        if (realtimeFiller != null) {
            realtimeFiller.stopAsync();
            realtimeFiller.awaitTerminated();
        }
        notifyStopped();
    }

    public void printKeys(PrintStream out) throws DecodingException, RocksDBException, IOException {
        out.println("pid\t pgid\t type\tSegmentStart\tcount\tsize\tstype");
        SegmentEncoderDecoder decoder = new SegmentEncoderDecoder();
        for (Parreplacedion p : parreplacedions) {
            try (RocksIterator it = gereplacederator(p)) {
                it.seekToFirst();
                while (it.isValid()) {
                    SegmentKey key = SegmentKey.decode(it.key());
                    byte[] v = it.value();
                    BaseSegment s;
                    s = decoder.decode(it.value(), key.segmentStart);
                    out.println(key.parameterId + "\t " + key.parameterGroupId + "\t " + key.type + "\t" + TimeEncoding.toString(key.segmentStart) + "\t" + s.size() + "\t" + v.length + "\t" + s.getClreplaced().getSimpleName());
                    it.next();
                }
            }
        }
    }

    /**
     * Delete all parreplacedions that overlap with [start, stop) segment.
     *
     * @param start
     * @param stop
     * @throws RocksDBException
     * @return all the parreplacedions removed
     */
    public List<Parreplacedion> deleteParreplacedions(long start, long stop) throws RocksDBException {
        // List<Parreplacedion> parts = getParreplacedions(start, stop, true);
        throw new UnsupportedOperationException("operation not supported");
    }

    public RocksIterator gereplacederator(Parreplacedion p) throws RocksDBException, IOException {
        return tablespace.getRdb(p.parreplacedionDir, false).newIterator();
    }

    public SortedTimeSegment getTimeSegment(Parreplacedion p, long segmentStart, int parameterGroupId) throws RocksDBException, IOException {
        byte[] timeKey = new SegmentKey(parameterIdMap.timeParameterId, parameterGroupId, segmentStart, SegmentKey.TYPE_ENG_VALUE).encode();
        byte[] tv = tablespace.getRdb(p.parreplacedionDir, false).get(timeKey);
        if (tv == null) {
            return null;
        }
        try {
            return (SortedTimeSegment) vsEncoder.decode(tv, segmentStart);
        } catch (DecodingException e) {
            throw new DatabaseCorruptionException(e);
        }
    }

    Parreplacedion getParreplacedions(long instant) {
        synchronized (parreplacedions) {
            return parreplacedions.getFit(instant);
        }
    }

    /**
     * returns the intervalStart where this instant could fit.
     *
     * @param instant
     * @return
     */
    public static long getIntervalStart(long instant) {
        return instant & INTERVAL_MASK;
    }

    /**
     * returns the end of the interval where the instant fits
     *
     * @param instant
     * @return
     */
    public static long getIntervalEnd(long instant) {
        return instant | TIMESTAMP_MASK;
    }

    /**
     * duration in milliseconds of one segment
     *
     * @return
     */
    public static long getIntervalDuration() {
        return TIMESTAMP_MASK + 1l;
    }

    public Tablespace getTablespace() {
        return tablespace;
    }

    public static clreplaced Parreplacedion extends TimeInterval {

        final String parreplacedionDir;

        Parreplacedion() {
            super();
            this.parreplacedionDir = null;
        }

        Parreplacedion(long start, long end, String dir) {
            super(start, end);
            this.parreplacedionDir = dir;
        }

        @Override
        public String toString() {
            return "parreplacedion: " + parreplacedionDir + "[" + TimeEncoding.toString(getStart()) + " - " + TimeEncoding.toString(getEnd()) + "]";
        }

        public String getParreplacedionDir() {
            return parreplacedionDir;
        }
    }
}

16 View Complete Implementation : SecurityStore.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private AuthModule loadAuthModule(YConfiguration moduleConfig) throws InitException {
    String moduleClreplaced = moduleConfig.getString("clreplaced");
    YConfiguration moduleArgs = YConfiguration.emptyConfig();
    if (moduleConfig.containsKey("args")) {
        moduleArgs = moduleConfig.getConfig("args");
    }
    log.debug("Loading AuthModule " + moduleClreplaced);
    try {
        AuthModule authModule = YObjectLoader.loadObject(moduleClreplaced);
        Spec spec = authModule.getSpec();
        if (log.isDebugEnabled()) {
            Map<String, Object> unsafeArgs = moduleArgs.getRoot();
            Map<String, Object> safeArgs = spec.maskSecrets(unsafeArgs);
            log.debug("Raw args for {}: {}", moduleClreplaced, safeArgs);
        }
        moduleArgs = spec.validate((YConfiguration) moduleArgs);
        if (log.isDebugEnabled()) {
            Map<String, Object> unsafeArgs = moduleArgs.getRoot();
            Map<String, Object> safeArgs = spec.maskSecrets(unsafeArgs);
            log.debug("Initializing {} with resolved args: {}", moduleClreplaced, safeArgs);
        }
        authModule.init(moduleArgs);
        return authModule;
    } catch (ValidationException e) {
        throw new InitException(e);
    } catch (IOException e) {
        throw new InitException("Failed to load AuthModule", e);
    }
}

16 View Complete Implementation : AbstractTcDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Base implementation for a TC data link that initialises a post processor and provides a queueing and rate limiting
 * function.
 *
 * @author nm
 */
public abstract clreplaced AbstractTcDataLink extends AbstractService implements TcDataLink, SystemParametersProducer {

    protected CommandHistoryPublisher commandHistoryPublisher;

    protected volatile boolean disabled = false;

    protected volatile long dataCount;

    protected String sv_linkStatus_id, sp_dataCount_id;

    protected SystemParametersCollector sysParamCollector;

    protected final Log log;

    protected final String yamcsInstance;

    protected final String name;

    TimeService timeService;

    protected CommandPostprocessor cmdPostProcessor;

    final YConfiguration config;

    static final PreparedCommand SIGNAL_QUIT = new PreparedCommand(new byte[0]);

    protected long housekeepingInterval = 10000;

    private AggregatedDataLink parent = null;

    protected boolean failCommandOnDisabled;

    public AbstractTcDataLink(String yamcsInstance, String linkName, YConfiguration config) throws ConfigurationException {
        log = new Log(getClreplaced(), yamcsInstance);
        log.setContext(linkName);
        this.yamcsInstance = yamcsInstance;
        this.name = linkName;
        this.config = config;
        timeService = YamcsServer.getTimeService(yamcsInstance);
        failCommandOnDisabled = config.getBoolean("failCommandOnDisabled", false);
        initPostprocessor(yamcsInstance, config);
    }

    protected long getCurrentTime() {
        if (timeService != null) {
            return timeService.getMissionTime();
        } else {
            return TimeEncoding.getWallclockTime();
        }
    }

    protected void initPostprocessor(String instance, YConfiguration config) {
        String commandPostprocessorClreplacedName = GenericCommandPostprocessor.clreplaced.getName();
        YConfiguration commandPostprocessorArgs = null;
        if (config != null) {
            commandPostprocessorClreplacedName = config.getString("commandPostprocessorClreplacedName", GenericCommandPostprocessor.clreplaced.getName());
            if (config.containsKey("commandPostprocessorArgs")) {
                commandPostprocessorArgs = config.getConfig("commandPostprocessorArgs");
            }
        }
        try {
            if (commandPostprocessorArgs != null) {
                cmdPostProcessor = YObjectLoader.loadObject(commandPostprocessorClreplacedName, instance, commandPostprocessorArgs);
            } else {
                cmdPostProcessor = YObjectLoader.loadObject(commandPostprocessorClreplacedName, instance);
            }
        } catch (ConfigurationException e) {
            log.error("Cannot instantiate the command postprocessor", e);
            throw e;
        } catch (IOException e) {
            log.error("Cannot instantiate the command postprocessor", e);
            throw new ConfigurationException(e);
        }
    }

    @Override
    public void setCommandHistoryPublisher(CommandHistoryPublisher commandHistoryListener) {
        this.commandHistoryPublisher = commandHistoryListener;
        cmdPostProcessor.setCommandHistoryPublisher(commandHistoryListener);
    }

    public String getLinkName() {
        return name;
    }

    @Override
    public Status getLinkStatus() {
        if (disabled) {
            return Status.DISABLED;
        } else {
            return Status.OK;
        }
    }

    @Override
    public void disable() {
        disabled = true;
    }

    @Override
    public void enable() {
        disabled = false;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public long getDataInCount() {
        return 0;
    }

    @Override
    public long getDataOutCount() {
        return dataCount;
    }

    protected void setupSysVariables() {
        this.sysParamCollector = SystemParametersCollector.getInstance(yamcsInstance);
        if (sysParamCollector != null) {
            sysParamCollector.registerProducer(this);
            sv_linkStatus_id = sysParamCollector.getNamespace() + "/" + name + "/linkStatus";
            sp_dataCount_id = sysParamCollector.getNamespace() + "/" + name + "/dataCount";
        } else {
            log.info("System variables collector not defined for instance {} ", yamcsInstance);
        }
    }

    @Override
    public List<ParameterValue> getSystemParameters() {
        long time = getCurrentTime();
        ParameterValue linkStatus = SystemParametersCollector.getPV(sv_linkStatus_id, time, getLinkStatus().name());
        ParameterValue dataCount = SystemParametersCollector.getPV(sp_dataCount_id, time, getDataOutCount());
        return Arrays.asList(linkStatus, dataCount);
    }

    @Override
    public YConfiguration getConfig() {
        return config;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void resetCounters() {
        dataCount = 0;
    }

    @Override
    public AggregatedDataLink getParent() {
        return parent;
    }

    @Override
    public void setParent(AggregatedDataLink parent) {
        this.parent = parent;
    }

    /**
     * Send to command history the failed command
     */
    protected void failedCommand(CommandId commandId, String reason) {
        long currentTime = getCurrentTime();
        commandHistoryPublisher.publishAck(commandId, ACK_SENT_CNAME_PREFIX, currentTime, AckStatus.NOK, reason);
        commandHistoryPublisher.commandFailed(commandId, currentTime, reason);
    }

    public String getYamcsInstance() {
        return yamcsInstance;
    }
}

16 View Complete Implementation : PusPacketPreprocessor.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
void configureTimeDecoder(YConfiguration config) {
    if (config != null) {
        useLocalGenerationTime = config.getBoolean("useLocalGenerationTime", false);
    }
    if (!useLocalGenerationTime && config != null && config.containsKey(Constants.CONFIG_KEY_TIME_ENCODING)) {
        YConfiguration c = config.getConfig(Constants.CONFIG_KEY_TIME_ENCODING);
        String type = c.getString("type", "CUC");
        if ("CUC".equals(type)) {
            int implicitPField = c.getInt("implicitPField", DEFAULT_IMPLICIT_PFIELD);
            timeDecoder = new CucTimeDecoder(implicitPField);
        } else {
            throw new ConfigurationException("Time encoding of type '" + type + " not supported. Supported: CUC=CCSDS unsegmented time");
        }
        pktTimeOffset = c.getInt("pktTimeOffset", DEFAULT_PKT_TIME_OFFSET);
    } else {
        pktTimeOffset = DEFAULT_PKT_TIME_OFFSET;
        timeDecoder = new CucTimeDecoder(DEFAULT_IMPLICIT_PFIELD);
    }
    log.debug("Using time decoder {}", timeDecoder);
}

16 View Complete Implementation : TcpTcDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Override
protected void initPostprocessor(String instance, YConfiguration config) {
    // traditionally this has used by default the ISS post-processor
    Map<String, Object> m = null;
    if (config == null) {
        m = new HashMap<>();
        config = YConfiguration.wrap(m);
    } else if (!config.containsKey("commandPostprocessorClreplacedName")) {
        m = config.getRoot();
    }
    if (m != null) {
        log.warn("Please set the commandPostprocessorClreplacedName for the TcpTcDataLink; in the future versions it will default to GenericCommandPostprocessor");
        m.put("commandPostprocessorClreplacedName", IssCommandPostprocessor.clreplaced.getName());
    }
    super.initPostprocessor(instance, config);
}

16 View Complete Implementation : YObjectLoader.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Loads clreplacedes defined in the yamcs server or client configuration properties
 *
 * @param clreplacedName
 * @param args
 * @return an object of the given clreplaced instantiated with the given parameters
 * @throws ConfigurationException
 * @throws IOException
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
static public <T> T loadObject(String clreplacedName, Object... args) throws ConfigurationException, IOException {
    try {
        Clreplaced ic = Clreplaced.forName(clreplacedName);
        Constructor<T> constructor = null;
        Constructor[] constructors = ic.getConstructors();
        for (Constructor c : constructors) {
            Clreplaced<?>[] params = c.getParameterTypes();
            if (params.length != args.length) {
                continue;
            }
            boolean ok = true;
            for (int i = 0; i < params.length; i++) {
                if ((args[i] != null) && !params[i].isreplacedignableFrom(args[i].getClreplaced())) {
                    if (args[i] instanceof YConfiguration) {
                        YConfiguration yc = (YConfiguration) args[i];
                        boolean isDeprecated = c.getDeclaredAnnotation(Deprecated.clreplaced) != null;
                        if (params[i].isreplacedignableFrom(yc.getRoot().getClreplaced()) && !isDeprecated) {
                            log.warn("Clreplaced {} uses a Map<String, Object> in the constructor. " + "Use YConfiguration instead", clreplacedName);
                            args[i] = yc.getRoot();
                            continue;
                        }
                    }
                    ok = false;
                    break;
                }
            }
            if (ok) {
                constructor = c;
                break;
            }
        }
        if (constructor == null) {
            StringBuilder sb = new StringBuilder();
            sb.append("Cannot find a constructor for clreplaced '" + clreplacedName + "' and arguments (");
            boolean first = true;
            for (Object o : args) {
                if (!first) {
                    sb.append(", ");
                } else {
                    first = false;
                }
                if (o == null) {
                    sb.append("java.lang.Object");
                } else {
                    sb.append(o.getClreplaced().getName());
                }
            }
            sb.append(")");
            throw new ConfigurationException(sb.toString());
        } else {
            checkDeprecated(ic);
            return constructor.newInstance(args);
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getCause();
        if (t instanceof ConfigurationException) {
            throw (ConfigurationException) t;
        } else if (t instanceof IOException) {
            throw (IOException) t;
        } else if (t instanceof ExceptionInInitializerError) {
            throw new ConfigurationException("Cannot instantiate object from clreplaced " + clreplacedName + ": " + t.getCause(), t.getCause());
        } else {
            throw new ConfigurationException("Cannot instantiate object from clreplaced " + clreplacedName + ": " + t, t);
        }
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigurationException("Cannot instantiate object from clreplaced " + clreplacedName + ": " + e, e);
    }
}

16 View Complete Implementation : YarchDatabaseInstance.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * Loads pre-defined buckets. The buckets will be created if they do not exist yet. By using the <code>path</code>
 * argument, it is possible to map a bucket to a random file system location instead of the default bucket storage
 * engine of this Yarch instance.
 */
private void loadBuckets(List<YConfiguration> configs) {
    try {
        for (YConfiguration config : configs) {
            String name = config.getString("name");
            if (config.containsKey("path")) {
                Path path = Paths.get(config.getString("path"));
                addFileSystemBucket(name, path);
            } else {
                Bucket bucket = getBucket(name);
                if (bucket == null) {
                    log.info("Creating bucket {}", name);
                    createBucket(name);
                }
            }
        }
    } catch (IOException e) {
        throw new ConfigurationException("Failed to load buckets: " + e.getMessage(), e);
    }
}

16 View Complete Implementation : ParameterArchiveTest.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
@Before
public void openDb() throws Exception {
    String dbroot = YarchDatabase.getInstance(instance).getRoot();
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot));
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot + ".rdb"));
    FileUtils.deleteRecursivelyIfExists(Paths.get(dbroot + ".tbs"));
    RdbStorageEngine rse = RdbStorageEngine.getInstance();
    if (rse.getTablespace(instance) != null) {
        rse.dropTablespace(instance);
    }
    rse.createTablespace(instance);
    Map<String, Object> conf = new HashMap<>();
    if (parreplacedioningSchema != null) {
        conf.put("parreplacedioningSchema", parreplacedioningSchema);
    }
    parchive = new ParameterArchive();
    YConfiguration config = parchive.getSpec().validate(YConfiguration.wrap(conf));
    parchive.init(instance, config);
    pidMap = parchive.getParameterIdDb();
    pgidMap = parchive.getParameterGroupIdDb();
    replacedertNotNull(pidMap);
    replacedertNotNull(pgidMap);
}

16 View Complete Implementation : AosFrameDecoderTest.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
AosManagedParameters getParams() {
    Map<String, Object> m = new HashMap<>();
    m.put("spacecraftId", 3);
    m.put("frameLength", 128);
    m.put("errorCorrection", "CRC16");
    m.put("insertZoneLength", 0);
    m.put("frameHeaderErrorControlPresent", false);
    List<Map<String, Object>> vclist = new ArrayList<>();
    m.put("virtualChannels", vclist);
    Map<String, Object> vc0 = new HashMap<>();
    vc0.put("vcId", 0);
    vc0.put("ocfPresent", false);
    vc0.put("service", "PACKET");
    vc0.put("packetPreprocessorClreplacedName", "org.yamcs.tctm.GenericPacketPreprocessor");
    Map<String, Object> vc1 = new HashMap<>();
    vc1.put("vcId", 1);
    vc1.put("ocfPresent", false);
    vc1.put("service", "PACKET");
    vc1.put("packetPreprocessorClreplacedName", "org.yamcs.tctm.GenericPacketPreprocessor");
    vclist.add(vc0);
    vclist.add(vc1);
    YConfiguration config = YConfiguration.wrap(m);
    return new AosManagedParameters(config);
}

16 View Complete Implementation : TmFrameDecoderTest.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
TmManagedParameters getParams() {
    Map<String, Object> m = new HashMap<>();
    m.put("spacecraftId", 35);
    m.put("frameLength", 1115);
    m.put("errorCorrection", "CRC16");
    List<Map<String, Object>> vclist = new ArrayList<>();
    m.put("virtualChannels", vclist);
    Map<String, Object> vc0 = new HashMap<>();
    vc0.put("vcId", 0);
    vc0.put("ocfPresent", true);
    vc0.put("service", "PACKET");
    vc0.put("packetPreprocessorClreplacedName", "org.yamcs.tctm.GenericPacketPreprocessor");
    vclist.add(vc0);
    YConfiguration config = YConfiguration.wrap(m);
    return new TmManagedParameters(config);
}

16 View Complete Implementation : TseCommander.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
private static List<Service> createServices(YConfiguration yconf, TseCommanderArgs runtimeOptions) {
    List<Service> services = new ArrayList<>();
    InstrumentController instrumentController = new InstrumentController();
    if (yconf.containsKey("instruments")) {
        for (YConfiguration instrumentConfig : yconf.getConfigList("instruments")) {
            String name = instrumentConfig.getString("name");
            try {
                InstrumentDriver instrument = YObjectLoader.loadObject(instrumentConfig.toMap(), name);
                instrumentController.addInstrument(instrument);
            } catch (IOException e) {
                throw new Error(e);
            }
        }
    }
    services.add(instrumentController);
    TelnetServer telnetServer = new TelnetServer(runtimeOptions.telnetPort, instrumentController);
    services.add(telnetServer);
    if (runtimeOptions.tctmPort != null) {
        services.add(new TcTmServer(runtimeOptions.tctmPort, instrumentController));
    }
    return services;
}

15 View Complete Implementation : ArtemisTmDataLink.java
Copyright GNU Affero General Public License v3.0
Author : yamcs
/**
 * receives data from Artemis ActiveMQ and publishes it into a yamcs stream
 *
 * @author nm
 */
public clreplaced ArtemisTmDataLink extends AbstractService implements TmPacketDataLink, MessageHandler {

    protected AtomicLong packetcount = new AtomicLong();

    protected volatile boolean disabled = false;

    protected Log log;

    private TmSink tmSink;

    final TimeService timeService;

    final String artemisAddress;

    ClientSession artemisSession;

    ServerLocator locator;

    ClientSessionFactory factory;

    ClientConsumer client;

    boolean preserveIncomingReceptionTime = false;

    YConfiguration config;

    final String linkName;

    public ArtemisTmDataLink(String instance, String name, String artemisAddress) throws ConfigurationException {
        this.artemisAddress = artemisAddress;
        this.linkName = name;
        log = new Log(getClreplaced(), instance);
        log.setContext(name);
        timeService = YamcsServer.getTimeService(instance);
        locator = AbstractArtemisTranslatorService.getServerLocator(instance);
    }

    public ArtemisTmDataLink(String instance, String name, YConfiguration config) throws ConfigurationException {
        this(instance, name, config.getString("address"));
        preserveIncomingReceptionTime = config.getBoolean("preserveIncomingReceptionTime", false);
    }

    @Override
    public void setTmSink(TmSink tmProcessor) {
        this.tmSink = tmProcessor;
    }

    @Override
    public Status getLinkStatus() {
        if (disabled) {
            return Status.DISABLED;
        } else {
            return Status.OK;
        }
    }

    @Override
    public void disable() {
        disabled = true;
    }

    @Override
    public void enable() {
        disabled = false;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public String getDetailedStatus() {
        if (disabled) {
            return "DISABLED";
        } else {
            return "OK";
        }
    }

    @Override
    public long getDataInCount() {
        return packetcount.get();
    }

    @Override
    public long getDataOutCount() {
        return 0;
    }

    @Override
    public void resetCounters() {
        packetcount.set(0);
    }

    @Override
    public void onMessage(ClientMessage msg) {
        try {
            msg.acknowledge();
            if (disabled) {
                return;
            }
            TmPacketData tm = (TmPacketData) Protocol.decode(msg, TmPacketData.newBuilder());
            packetcount.incrementAndGet();
            long rectime = preserveIncomingReceptionTime ? TimeEncoding.fromProtobufTimestamp(tm.getReceptionTime()) : timeService.getMissionTime();
            TmPacket pwt = new TmPacket(rectime, TimeEncoding.fromProtobufTimestamp(tm.getGenerationTime()), tm.getSequenceNumber(), tm.getPacket().toByteArray());
            tmSink.processPacket(pwt);
        } catch (Exception e) {
            log.warn("{} for message: {}", e.getMessage(), msg);
        }
    }

    @Override
    protected void doStart() {
        try {
            factory = locator.createSessionFactory();
            artemisSession = factory.createSession(false, true, true, true);
            String queue = artemisAddress + "-ActiveMQTmProvider";
            artemisSession.createTemporaryQueue(artemisAddress, queue);
            log.debug("Starting artemis tm data link connected to {}.{}", artemisAddress, queue);
            client = artemisSession.createConsumer(queue, AbstractArtemisTranslatorService.UNIQUEID_HDR_NAME + "<>" + AbstractArtemisTranslatorService.UNIQUEID);
            client.setMessageHandler(this);
            artemisSession.start();
            notifyStarted();
        } catch (Exception e) {
            log.error("Failed to set connect to artemis");
            notifyFailed(e);
        }
    }

    @Override
    protected void doStop() {
        try {
            artemisSession.close();
            locator.close();
            notifyStopped();
        } catch (ActiveMQException e) {
            log.error("Got exception when quiting:", e);
            notifyFailed(e);
        }
    }

    @Override
    public String getName() {
        return linkName;
    }

    @Override
    public YConfiguration getConfig() {
        return config;
    }
}