Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / SourceMapper.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import java.math.BigInteger;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.Dirty;
17 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
18 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
19 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
20 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
46
47 /**
48  * Manage the table that assigns source endpoint group, bridge domain, and 
49  * router domain to registers to be used by other tables.
50  * @author readams
51  */
52 public class SourceMapper extends FlowTable {
53     protected static final Logger LOG =
54             LoggerFactory.getLogger(SourceMapper.class);
55
56     public static final short TABLE_ID = 1;
57
58     public SourceMapper(OfTable.OfTableCtx ctx) {
59         super(ctx);
60     }
61
62     @Override
63     public short getTableId() {
64         return TABLE_ID;
65     }
66
67     @Override
68     public void sync(ReadWriteTransaction t,
69                      InstanceIdentifier<Table> tiid,
70                      Map<String, FlowCtx> flowMap, 
71                      NodeId nodeId, PolicyInfo policyInfo, 
72                      Dirty dirty) throws Exception {
73         dropFlow(t, tiid, flowMap, Integer.valueOf(1), null);
74
75         for (EgKey sepg : ctx.epManager.getGroupsForNode(nodeId)) {
76             IndexedTenant tenant = 
77                     ctx.policyResolver.getTenant(sepg.getTenantId());
78             if (tenant == null) continue;
79
80             EndpointGroup eg = tenant.getEndpointGroup(sepg.getEgId());
81             L3Context l3c = tenant.resolveL3Context(eg.getNetworkDomain());
82             L2BridgeDomain bd = tenant.resolveL2BridgeDomain(eg.getNetworkDomain());
83             L2FloodDomain fd = tenant.resolveL2FloodDomain(eg.getNetworkDomain());
84             int egId = 0, bdId = 0, fdId = 0, l3Id = 0;
85             
86             egId = ctx.policyManager.getContextOrdinal(sepg.getTenantId(), 
87                                                        sepg.getEgId());
88             if (bd != null)
89                 bdId = ctx.policyManager.getContextOrdinal(sepg.getTenantId(), 
90                                                            bd.getId());
91             if (fd != null)
92                 fdId = ctx.policyManager.getContextOrdinal(sepg.getTenantId(), 
93                                                            fd.getId());
94             if (l3c != null)
95                 l3Id = ctx.policyManager.getContextOrdinal(sepg.getTenantId(), 
96                                                            l3c.getId());
97
98             NodeConnectorId tunPort =
99                     ctx.switchManager.getTunnelPort(nodeId);
100             if (tunPort != null) {
101                 FlowId flowid = new FlowId(new StringBuilder()
102                     .append(tunPort.getValue())
103                     .append("|tunnel|")
104                     .append(egId)
105                     .append("|")
106                     .append(bdId)
107                     .append("|")
108                     .append(fdId)
109                     .append("|")
110                     .append(l3Id)
111                     .toString());
112                 if (visit(flowMap, flowid.getValue())) {
113                     MatchBuilder mb = new MatchBuilder()
114                         .setInPort(tunPort);
115                     addNxTunIdMatch(mb, egId);
116                     Action segReg = nxLoadRegAction(NxmNxReg0.class, 
117                                                     BigInteger.valueOf(egId));
118                     // set condition group register to all ones to bypass
119                     // policy enforcement
120                     Action scgReg = nxLoadRegAction(NxmNxReg1.class,
121                                                     BigInteger.valueOf(0xffffff));
122                     Action bdReg = nxLoadRegAction(NxmNxReg4.class, 
123                                                    BigInteger.valueOf(bdId));
124                     Action fdReg = nxLoadRegAction(NxmNxReg5.class, 
125                                                    BigInteger.valueOf(fdId));
126                     Action vrfReg = nxLoadRegAction(NxmNxReg6.class, 
127                                                     BigInteger.valueOf(l3Id));
128                     FlowBuilder flowb = base()
129                         .setId(flowid)
130                         .setPriority(Integer.valueOf(150))
131                         .setMatch(mb.build())
132                         .setInstructions(instructions(applyActionIns(segReg,
133                                                                      scgReg,
134                                                                      bdReg,
135                                                                      fdReg,
136                                                                      vrfReg),
137                                                       gotoTableIns((short)(TABLE_ID + 1))));
138                     writeFlow(t, tiid, flowb.build());
139                 }
140             }
141             
142             for (Endpoint e : ctx.epManager.getEPsForNode(nodeId, sepg)) {
143                 OfOverlayContext ofc = e.getAugmentation(OfOverlayContext.class);
144                 if (ofc != null && ofc.getNodeConnectorId() != null &&
145                         (ofc.getLocationType() == null ||
146                         LocationType.Internal.equals(ofc.getLocationType())) &&
147                         e.getTenant() != null && e.getEndpointGroup() != null) {
148                     syncEP(t, tiid, flowMap, policyInfo, nodeId, e, ofc,
149                            egId, bdId, fdId, l3Id);
150                 } 
151             }
152         }
153     }
154     
155     private void syncEP(ReadWriteTransaction t,
156                         InstanceIdentifier<Table> tiid,
157                         Map<String, FlowCtx> flowMap, 
158                         PolicyInfo policyInfo,
159                         NodeId nodeId, Endpoint e, OfOverlayContext ofc,
160                         int egId, int bdId, int fdId, int l3Id) 
161                                  throws Exception {
162         // Set sEPG, flood domain, bridge domain, and layer 3 context 
163         // for internal endpoints by directly matching each endpoint
164
165         List<ConditionName> conds = ctx.epManager.getCondsForEndpoint(e);
166         ConditionGroup cg = 
167                 policyInfo.getEgCondGroup(new EgKey(e.getTenant(), 
168                                                     e.getEndpointGroup()), 
169                                           conds);
170         int cgId = ctx.policyManager.getCondGroupOrdinal(cg);
171
172         FlowId flowid = new FlowId(new StringBuilder()
173             .append(ofc.getNodeConnectorId().getValue())
174             .append("|")
175             .append(e.getMacAddress().getValue())
176             .append("|")
177             .append(egId)
178             .append("|")
179             .append(bdId)
180             .append("|")
181             .append(fdId)
182             .append("|")
183             .append(l3Id)
184             .append("|")
185             .append(cgId)
186             .toString());
187         if (visit(flowMap, flowid.getValue())) {
188             Action segReg = nxLoadRegAction(NxmNxReg0.class, 
189                                             BigInteger.valueOf(egId));
190             Action scgReg = nxLoadRegAction(NxmNxReg1.class, 
191                                             BigInteger.valueOf(cgId));
192             Action bdReg = nxLoadRegAction(NxmNxReg4.class, 
193                                            BigInteger.valueOf(bdId));
194             Action fdReg = nxLoadRegAction(NxmNxReg5.class, 
195                                            BigInteger.valueOf(fdId));
196             Action vrfReg = nxLoadRegAction(NxmNxReg6.class, 
197                                             BigInteger.valueOf(l3Id));
198             FlowBuilder flowb = base()
199                 .setPriority(Integer.valueOf(100))
200                 .setId(flowid)
201                 .setMatch(new MatchBuilder()
202                     .setEthernetMatch(ethernetMatch(e.getMacAddress(), 
203                                                     null, null))
204                     .setInPort(ofc.getNodeConnectorId())
205                     .build())
206                 .setInstructions(instructions(applyActionIns(segReg,
207                                                              scgReg,
208                                                              bdReg,
209                                                              fdReg,
210                                                              vrfReg),
211                                               gotoTableIns((short)(TABLE_ID + 1))));
212             writeFlow(t, tiid, flowb.build());
213         }
214     }
215 }