summaryrefslogtreecommitdiffstats
path: root/srv6/srv6-impl/src/main/java/io/fd/hc2vpp/srv6/read/steering/request/L3SteeringRequest.java
blob: 1043746e9eb4729569c9c55a171c1785df1a3fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (c) 2018 Bell Canada, Pantheon Technologies and/or its affiliates.
 *
 * 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 io.fd.hc2vpp.srv6.read.steering.request;

import com.google.common.base.Preconditions;
import io.fd.hc2vpp.srv6.read.ReadRequest;
import io.fd.honeycomb.translate.read.ReadContext;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.vpp.jvpp.core.dto.SrSteeringPolDetails;
import io.fd.vpp.jvpp.core.future.FutureJVppCore;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.oc.srte.policy.rev170918.include.prefix.StateBuilder;
import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.oc.srte.policy.rev170918.prefixes.properties.Prefixes;
import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.oc.srte.policy.rev170918.prefixes.properties.prefixes.Prefix;
import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.oc.srte.policy.rev170918.prefixes.properties.prefixes.PrefixBuilder;
import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.oc.srte.policy.rev170918.prefixes.properties.prefixes.PrefixKey;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

/**
 * Request for steering of L3 traffic
 */

public class L3SteeringRequest extends SteeringRequest
        implements ReadRequest<Prefix, PrefixKey, PrefixBuilder> {

    private static final int L3_TRAFFIC_TYPE_IPV6 = 6;
    private static final int L3_TRAFFIC_TYPE_IPV4 = 4;

    public L3SteeringRequest(final FutureJVppCore api) {
        super(api);
    }

    @Override
    public void checkValid() {
        super.checkValid();
    }

    @Override
    @Nonnull
    public List<PrefixKey> readAllKeys(@Nonnull InstanceIdentifier<Prefix> identifier, @Nonnull ReadContext ctx)
            throws ReadFailedException {
        return dumpManager.getDump(identifier, ctx.getModificationCache()).or(STATIC_EMPTY_REPLY)
                .srSteeringPolDetails.stream()
                .filter(
                        srSteeringPolDetails -> ((int) srSteeringPolDetails.trafficType) !=
                                L2SteeringRequest.L2_TRAFFIC_TYPE)
                .map(this::parseL3SteeringKey)
                .collect(Collectors.toList());
    }

    public List<IpPrefix> readAllIpPrefixes(@Nonnull InstanceIdentifier<Prefixes> identifier, @Nonnull ReadContext ctx)
            throws ReadFailedException {
        return dumpManager.getDump(identifier, ctx.getModificationCache()).or(STATIC_EMPTY_REPLY)
                .srSteeringPolDetails.stream()
                .filter(
                        srSteeringPolDetails -> ((int) srSteeringPolDetails.trafficType) !=
                                L2SteeringRequest.L2_TRAFFIC_TYPE)
                .map(this::parseIpPrefix)
                .collect(Collectors.toList());
    }

    @Override
    public void readSpecific(@Nonnull InstanceIdentifier<Prefix> identifier, @Nonnull ReadContext ctx,
                             @Nonnull PrefixBuilder builder) throws ReadFailedException {
        checkValid();
        PrefixKey key = identifier.firstKeyOf(Prefix.class);
        dumpManager.getDump(identifier, ctx.getModificationCache()).or(STATIC_EMPTY_REPLY)
                .srSteeringPolDetails.stream()
                .filter(
                        srSteeringPolDetails -> ((int) srSteeringPolDetails.trafficType) !=
                                L2SteeringRequest.L2_TRAFFIC_TYPE)
                .filter(srSteeringPolDetails -> parseL3SteeringKey(srSteeringPolDetails).equals(key))
                .findFirst()
                .ifPresent(srSteeringPolDetails1 -> parseL3Steering(srSteeringPolDetails1, builder));
    }

    private void parseL3Steering(SrSteeringPolDetails srSteeringPolDetails, final PrefixBuilder builder) {
        PrefixKey key = parseL3SteeringKey(srSteeringPolDetails);
        builder.setKey(key).setIpPrefix(key.getIpPrefix())
                .setState(new StateBuilder().setIpPrefix(key.getIpPrefix()).build());
    }

    private PrefixKey parseL3SteeringKey(SrSteeringPolDetails policyDetails) {
        return new PrefixKey(parseIpPrefix(policyDetails));
    }

    private IpPrefix parseIpPrefix(final SrSteeringPolDetails policyDetails) {
        boolean isIpv6 = isIpv6L3TrafficType(policyDetails.trafficType);
        IpPrefix ipPrefix;
        if (isIpv6) {
            Ipv6Prefix ipv6Prefix = toIpv6Prefix(policyDetails.prefixAddr, policyDetails.maskWidth);
            ipPrefix = new IpPrefix(ipv6Prefix);
        } else {
            Ipv4Prefix ipv4Prefix = toIpv4Prefix(policyDetails.prefixAddr, policyDetails.maskWidth);
            ipPrefix = new IpPrefix(ipv4Prefix);
        }
        return ipPrefix;
    }

    private boolean isIpv6L3TrafficType(final byte trafficType) {
        Preconditions.checkArgument(trafficType == L3_TRAFFIC_TYPE_IPV4 || trafficType == L3_TRAFFIC_TYPE_IPV6,
                "Unsupported traffic type for L3 steering");

        return trafficType == L3_TRAFFIC_TYPE_IPV6;
    }
}