All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.projectfloodlight.openflow.protocol.OFOxmList Maven / Gradle / Ivy

Go to download

OpenFlowJ API supporting OpenFlow versions 1.0 through 1.5.1, generated by LoxiGen

The newest version!
package org.projectfloodlight.openflow.protocol;

import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;

import org.projectfloodlight.openflow.exceptions.OFParseError;
import org.projectfloodlight.openflow.protocol.match.MatchField;
import org.projectfloodlight.openflow.protocol.match.MatchFields;
import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
import org.projectfloodlight.openflow.types.OFValueType;
import org.projectfloodlight.openflow.types.PrimitiveSinkable;
import org.projectfloodlight.openflow.util.ChannelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.common.hash.PrimitiveSink;

import io.netty.buffer.ByteBuf;

public class OFOxmList implements Iterable>, Writeable, PrimitiveSinkable {
    private static final Logger logger = LoggerFactory.getLogger(OFOxmList.class);

    private final Map> oxmMap;

    public final static OFOxmList EMPTY = new OFOxmList(ImmutableMap.>of());

    private OFOxmList(Map> oxmMap) {
        this.oxmMap = oxmMap;
    }

    @SuppressWarnings("unchecked")
    public > OFOxm get(MatchField matchField) {
        return (OFOxm) oxmMap.get(matchField.id);
    }

    public static class Builder {
        private final Map> oxmMap;

        public Builder() {
            oxmMap = new EnumMap<>(MatchFields.class);
        }

        public Builder(EnumMap> oxmMap) {
            this.oxmMap = oxmMap;
        }

        public > void set(OFOxm oxm) {
            oxmMap.put(oxm.getMatchField().id, oxm);
        }

        public > void unset(MatchField matchField) {
            oxmMap.remove(matchField.id);
        }

        public OFOxmList build() {
            return OFOxmList.ofList(oxmMap.values());
        }
    }

    @Override
    public Iterator> iterator() {
        return oxmMap.values().iterator();
    }

    public static OFOxmList ofList(Iterable> oxmList) {
        Map> map = new EnumMap<>(
                MatchFields.class);
        for (OFOxm o : oxmList) {
            OFOxm canonical = o.getCanonical();

            if(logger.isDebugEnabled() && !Objects.equal(o, canonical)) {
                logger.debug("OFOxmList: normalized non-canonical OXM {} to {}", o, canonical);
            }

            if(canonical != null)
                map.put(canonical.getMatchField().id, canonical);

        }
        return new OFOxmList(map);
    }

    public static OFOxmList of(OFOxm... oxms) {
        Map> map = new EnumMap<>(
                MatchFields.class);
        for (OFOxm o : oxms) {
            OFOxm canonical = o.getCanonical();

            if(logger.isDebugEnabled() && !Objects.equal(o, canonical)) {
                logger.debug("OFOxmList: normalized non-canonical OXM {} to {}", o, canonical);
            }

            if(canonical != null)
                map.put(canonical.getMatchField().id, canonical);
        }
        return new OFOxmList(map);
    }

    public static OFOxmList readFrom(ByteBuf bb, int length,
            OFMessageReader> reader) throws OFParseError {
        return ofList(ChannelUtils.readList(bb, length, reader));
    }

    @Override
    public void writeTo(ByteBuf bb) {
        for (OFOxm o : this) {
            o.writeTo(bb);
        }
    }

    public OFOxmList.Builder createBuilder() {
        return new OFOxmList.Builder(new EnumMap<>(oxmMap));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((oxmMap == null) ? 0 : oxmMap.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        OFOxmList other = (OFOxmList) obj;
        if (oxmMap == null) {
            if (other.oxmMap != null)
                return false;
        } else if (!oxmMap.equals(other.oxmMap))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "OFOxmList" + oxmMap;
    }

    @Override
    public void putTo(PrimitiveSink sink) {
        for (OFOxm o : this) {
            o.putTo(sink);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy