6cf49d923c3fbe58570392636292474bd7dcdb5d
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / IPV6InternetDefaultRouteProgrammer.java
1 /*
2  * Copyright (c) 2017 6WIND, Inc. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.neutronvpn;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16
17 import org.opendaylight.genius.mdsalutil.ActionInfo;
18 import org.opendaylight.genius.mdsalutil.FlowEntity;
19 import org.opendaylight.genius.mdsalutil.InstructionInfo;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
25 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldMeta;
26 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
27 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
28 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
29 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Singleton
34 public class IPV6InternetDefaultRouteProgrammer {
35
36     private static final Logger LOG = LoggerFactory.getLogger(IPV6InternetDefaultRouteProgrammer.class);
37     private final IMdsalApiManager mdsalManager;
38
39     @Inject
40     public IPV6InternetDefaultRouteProgrammer(final IMdsalApiManager mdsalManager) {
41         this.mdsalManager = mdsalManager;
42     }
43
44     private FlowEntity buildIPv6FallbacktoExternalVpn(BigInteger dpId, String routerId, long internetBgpVpnId,
45                                                       long vpnId, boolean add) {
46         List<MatchInfo> matches = new ArrayList<>();
47         matches.add(MatchEthernetType.IPV6);
48
49         //add match for router vpnId
50         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
51
52         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
53         ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
54         if (add) {
55             ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(
56                     MetaDataUtil.getVpnIdMetadata(internetBgpVpnId));
57             listActionInfo.add(actionSetFieldMeta);
58             listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
59             instructionInfo.add(new InstructionApplyActions(listActionInfo));
60         }
61         String defaultIPv6 = "0:0:0:0:0:0:0:0";
62         /* For each router it needs to have unique flow-id. Hence router-id is being used to generate the
63          * flow-id for each DPN instead of internet vpnId.
64          */
65         String flowRef = getIPv6FlowRefL3(dpId, NwConstants.L3_FIB_TABLE, defaultIPv6, routerId);
66
67         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
68                 NwConstants.TABLE_MISS_PRIORITY, flowRef/* "L3 ipv6 internet default route",*/, 0, 0,
69             NwConstants.COOKIE_VM_FIB_TABLE, matches, instructionInfo);
70
71         return flowEntity;
72     }
73
74     /**
75      * This method installs in the FIB table the default route for IPv6.
76      *
77      * @param dpnId of the compute node
78      * @param internetBgpVpnId internetVpn id as long
79      * @param vpnId id of router associated to internet bgpvpn as long
80      */
81     public void installDefaultRoute(BigInteger dpnId, String routerId, long internetBgpVpnId, long vpnId) {
82         FlowEntity flowEntity = buildIPv6FallbacktoExternalVpn(dpnId, routerId, internetBgpVpnId, vpnId, true);
83         LOG.trace("installDefaultRoute: flowEntity: {} ", flowEntity);
84         mdsalManager.installFlow(flowEntity);
85     }
86
87     public void removeDefaultRoute(BigInteger dpnId, String routerId, long internetBgpVpnId, long vpnId) {
88         FlowEntity flowEntity = buildIPv6FallbacktoExternalVpn(dpnId, routerId, internetBgpVpnId, vpnId, false);
89         LOG.trace("removeDefaultRoute: flowEntity: {} ", flowEntity);
90         mdsalManager.removeFlow(flowEntity);
91     }
92
93     public String getIPv6FlowRefL3(BigInteger dpnId, short tableId, String destPrefix, String routerId) {
94         return "L3." + dpnId.toString() + NwConstants.FLOWID_SEPARATOR + tableId
95                 + NwConstants.FLOWID_SEPARATOR + destPrefix + NwConstants.FLOWID_SEPARATOR + routerId;
96     }
97 }