/*
 * Copyright 2013-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cloudfoundry.doppler;

import org.cloudfoundry.Nullable;
import org.immutables.value.Value;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Wraps an Event and adds metadata
 */
@Value.Immutable
abstract class _Envelope {

    public static Envelope from(org.cloudfoundry.dropsonde.events.Envelope dropsonde) {
        Objects.requireNonNull(dropsonde, "dropsonde");

        Envelope.Builder envelope = Envelope.builder()
            .deployment(dropsonde.deployment)
            .eventType(EventType.from(dropsonde.eventType))
            .index(dropsonde.index)
            .ip(dropsonde.ip)
            .job(dropsonde.job)
            .origin(dropsonde.origin)
            .tags(dropsonde.tags)
            .timestamp(dropsonde.timestamp);

        Optional.ofNullable(dropsonde.containerMetric).ifPresent(d -> envelope.containerMetric(ContainerMetric.from(d)));
        Optional.ofNullable(dropsonde.error).ifPresent(d -> envelope.error(Error.from(d)));
        Optional.ofNullable(dropsonde.httpStart).ifPresent(d -> envelope.httpStart(HttpStart.from(d)));
        Optional.ofNullable(dropsonde.httpStartStop).ifPresent(d -> envelope.httpStartStop(HttpStartStop.from(d)));
        Optional.ofNullable(dropsonde.httpStop).ifPresent(d -> envelope.httpStop(HttpStop.from(d)));
        Optional.ofNullable(dropsonde.logMessage).ifPresent(d -> envelope.logMessage(LogMessage.from(d)));
        Optional.ofNullable(dropsonde.valueMetric).ifPresent(d -> envelope.valueMetric(ValueMetric.from(d)));

        return envelope.build();
    }

    /**
     * The enclosed {@link ContainerMetric}
     */
    @Nullable
    abstract ContainerMetric getContainerMetric();

    /**
     * Deployment name (used to uniquely identify source)
     */
    @Nullable
    abstract String getDeployment();

    /**
     * The enclosed {@link Error}
     */
    @Nullable
    abstract Error getError();

    /**
     * Type of wrapped event. Only the optional field corresponding to the value of eventType should be set.
     */
    abstract EventType getEventType();

    /**
     * The enclosed {@link HttpStart}
     */
    @Nullable
    abstract HttpStart getHttpStart();

    /**
     * The enclosed {@link HttpStartStop}
     */
    @Nullable
    abstract HttpStartStop getHttpStartStop();

    /**
     * The enclosed {@link HttpStop}
     */
    @Nullable
    abstract HttpStop getHttpStop();

    /**
     * Index of job (used to uniquely identify source)
     */
    @Nullable
    abstract String getIndex();

    /**
     * IP address (used to uniquely identify source)
     */
    @Nullable
    abstract String getIp();

    /**
     * Job name (used to uniquely identify source)
     */
    @Nullable
    abstract String getJob();

    /**
     * The enclosed {@link LogMessage}
     */
    @Nullable
    abstract LogMessage getLogMessage();

    /**
     * Unique description of the origin of this event
     */
    abstract String getOrigin();

    /**
     * key/value tags to include additional identifying information
     */
    abstract Map<String, String> getTags();

    /**
     * UNIX timestamp (in nanoseconds) event was wrapped in this Envelope.
     */
    @Nullable
    abstract Long getTimestamp();

    /**
     * The enclosed {@link ValueMetric}
     */
    @Nullable
    abstract ValueMetric getValueMetric();

}

@@ -1521,32 +1521,145 @@
 tric.from(d)));%0A
+        Optional.ofNullable(dropsonde.counterEvent).ifPresent(d -%3E envelope.counterEvent(CounterEvent.from(d)));%0A
         Optional
@@ -2435,24 +2435,141 @@
 rMetric();%0A%0A
+    /**%0A     * The enclosed %7B@link CounterEvent%7D%0A     */%0A    @Nullable%0A    abstract CounterEvent getCounterEvent();%0A%0A
     /**%0A    

Add CounterEvent to Envelope