+
+ private long getIpAddress(byte[] rawIpAddress) {
+ return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
+ + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
+ }
+
+ private void makeConnectedRoute(BigInteger dpId, long vpnId, VrfEntry vrfEntry, String rd,
+ long groupId, int addOrRemove) {
+ LOG.trace("makeConnectedRoute: vrfEntry {}",vrfEntry);
+ String values[] = vrfEntry.getDestPrefix().split("/");
+ String ipAddress = values[0];
+ int prefixLength = (values.length == 1) ? 32 : Integer.parseInt(values[1]);
+ LOG.debug("Adding route to DPN. ip {} masklen {}", ipAddress, prefixLength);
+ InetAddress destPrefix = null;
+ try {
+ destPrefix = InetAddress.getByName(ipAddress);
+ } catch (UnknownHostException e) {
+ LOG.error("UnknowHostException in addRoute. Failed to add Route for ipPrefix {}", vrfEntry.getDestPrefix());
+ return;
+ }
+
+ List<MatchInfo> matches = new ArrayList<MatchInfo>();
+
+ matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
+ BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
+
+ matches.add(new MatchInfo(MatchFieldType.eth_type,
+ new long[] { 0x0800L }));
+
+ if(prefixLength != 0) {
+ matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
+ getIpAddress(destPrefix.getAddress()), prefixLength }));
+ }
+
+ List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
+ List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+
+ if(addOrRemove == NwConstants.ADD_FLOW) {
+ actionsInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
+ actionsInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
+ actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId)}));
+ instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+ }
+
+ String flowRef = getFlowRef(dpId, L3_FIB_TABLE, rd, destPrefix);
+
+ FlowEntity flowEntity;
+
+ int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
+ flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_FIB_TABLE, flowRef,
+ priority, flowRef, 0, 0,
+ COOKIE_VM_FIB_TABLE, matches, instructions);
+
+ if (addOrRemove == NwConstants.ADD_FLOW) {
+ mdsalManager.installFlow(flowEntity);
+ } else {
+ mdsalManager.removeFlow(flowEntity);
+ }
+ }
+
+ private void makeLFibTableEntry(BigInteger dpId, long label, long groupId,
+ String nextHop, int addOrRemove) {
+ List<MatchInfo> matches = new ArrayList<MatchInfo>();
+ matches.add(new MatchInfo(MatchFieldType.eth_type,
+ new long[] { 0x8847L }));
+ matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(label)}));
+
+ List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
+ List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+ actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
+ instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+
+ // Install the flow entry in L3_LFIB_TABLE
+ String flowRef = getFlowRef(dpId, L3_LFIB_TABLE, label, nextHop);
+
+ FlowEntity flowEntity;
+ flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_LFIB_TABLE, flowRef,
+ DEFAULT_FIB_FLOW_PRIORITY, flowRef, 0, 0,
+ COOKIE_VM_LFIB_TABLE, matches, instructions);
+
+ if (addOrRemove == NwConstants.ADD_FLOW) {
+ mdsalManager.installFlow(flowEntity);
+ } else {
+ mdsalManager.removeFlow(flowEntity);
+ }
+ LOG.debug("LFIB Entry for dpID {} : label : {} group {} modified successfully {}",dpId, label, groupId );
+ }
+
+ private void deleteLocalAdjacency(final BigInteger dpId, final long vpnId, final VrfEntry vrfEntry) {
+ LOG.trace("deleteLocalAdjacency called with dpid {}, vpnId{}, VrfEntry {}",dpId, vpnId, vrfEntry);;
+ try {
+ Future<RpcResult<Void>> result =
+ l3nexthopService.removeLocalNextHop(new RemoveLocalNextHopInputBuilder().setDpnId(dpId)
+ .setIpPrefix(vrfEntry.getDestPrefix())
+ .setNexthopIp(vrfEntry.getNextHopAddress())
+ .setVpnId(vpnId)
+ .build());
+ RpcResult<Void> rpcResult = result.get();
+ if (rpcResult.isSuccessful()) {
+ LOG.debug("Local Next hop for {} on dpn {} successfully deleted", vrfEntry.getDestPrefix(), dpId);
+ } else {
+ LOG.error("Local Next hop for {} on dpn {} not deleted", vrfEntry.getDestPrefix(), dpId);
+ }
+ } catch (NullPointerException | InterruptedException | ExecutionException e) {
+ LOG.trace("", e);
+ }
+ }
+
+ public void populateFibOnNewDpn(BigInteger dpnId, long vpnId, String rd) {
+ LOG.trace("New dpn {} for vpn {} : populateFibOnNewDpn", dpnId, rd);
+ InstanceIdentifier<VrfTables> id = buildVrfId(rd);
+ Optional<VrfTables> vrfTable = read(LogicalDatastoreType.OPERATIONAL, id);
+ if(vrfTable.isPresent()) {
+ for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+ addRouteInternal(dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+ }
+ }
+ }
+
+ public void cleanUpDpnForVpn(BigInteger dpnId, long vpnId, String rd) {
+ LOG.trace("Remove dpn {} for vpn {} : cleanUpDpnForVpn", dpnId, rd);
+ InstanceIdentifier<VrfTables> id = buildVrfId(rd);
+ Optional<VrfTables> vrfTable = read(LogicalDatastoreType.OPERATIONAL, id);
+ if(vrfTable.isPresent()) {
+ for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+ deleteRoute(dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+ }
+ }
+ }
+
+ public static InstanceIdentifier<VrfTables> buildVrfId(String rd) {
+ InstanceIdentifierBuilder<VrfTables> idBuilder =
+ InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
+ InstanceIdentifier<VrfTables> id = idBuilder.build();
+ return id;
+ }
+
+ private String getFlowRef(BigInteger dpnId, short tableId, long label, String nextHop) {
+ return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
+ .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
+ .append(label).append(NwConstants.FLOWID_SEPARATOR)
+ .append(nextHop).toString();
+ }
+
+ private String getFlowRef(BigInteger dpnId, short tableId, String rd, InetAddress destPrefix) {
+ return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
+ .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
+ .append(rd).append(NwConstants.FLOWID_SEPARATOR)
+ .append(destPrefix.getHostAddress()).toString();
+ }
+
+ protected GetEgressPointerOutput resolveAdjacency(final BigInteger dpId, final long vpnId,
+ final VrfEntry vrfEntry) {
+ GetEgressPointerOutput adjacency = null;
+ LOG.trace("resolveAdjacency called with dpid {}, vpnId{}, VrfEntry {}",dpId, vpnId, vrfEntry);;
+ try {
+ Future<RpcResult<GetEgressPointerOutput>> result =
+ l3nexthopService.getEgressPointer(new GetEgressPointerInputBuilder().setDpnId(dpId)
+ .setIpPrefix(vrfEntry.getDestPrefix())
+ .setNexthopIp(vrfEntry.getNextHopAddress())
+ .setVpnId(vpnId)
+ .build());
+ RpcResult<GetEgressPointerOutput> rpcResult = result.get();
+ if (rpcResult.isSuccessful()) {
+ adjacency = rpcResult.getResult();
+ } else {
+ LOG.error("Next hop information not available");
+ }
+ } catch (NullPointerException | InterruptedException | ExecutionException e) {
+ LOG.trace("", e);
+ }
+ return adjacency;
+ }
+
+ protected Long getVpnId(String rd) {
+ Long vpnId = null;
+ InstanceIdentifier<VpnInstances> id = InstanceIdentifier.create(VpnInstances.class);
+ Optional<VpnInstances> vpnInstances = read(LogicalDatastoreType.OPERATIONAL, id);
+ if(vpnInstances.isPresent()) {
+ List<VpnInstance> vpns = vpnInstances.get().getVpnInstance();
+ for(VpnInstance vpn : vpns) {
+ if(vpn.getIpv4Family().getRouteDistinguisher().equals(rd)) {
+ VpnInstance1 vpnInstanceId = vpn.getAugmentation(VpnInstance1.class);
+ if (vpnInstanceId != null) {
+ vpnId = vpnInstanceId.getVpnId();
+ break;
+ }
+ }
+ }
+ }
+ return vpnId;
+ }
+
+ public void processNodeAdd(BigInteger dpnId) {
+ LOG.debug("Received notification to install TableMiss entries for dpn {} ", dpnId);
+ makeTableMissFlow(dpnId, NwConstants.ADD_FLOW);
+ }
+
+ private void makeTableMissFlow(BigInteger dpnId, int addOrRemove) {
+ final BigInteger COOKIE_TABLE_MISS = new BigInteger("1030000", 16);
+ // Instruction to punt to controller
+ List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
+ List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+ actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
+ instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+ List<MatchInfo> matches = new ArrayList<MatchInfo>();
+ FlowEntity flowEntityLfib = MDSALUtil.buildFlowEntity(dpnId, L3_LFIB_TABLE,
+ getFlowRef(dpnId, L3_LFIB_TABLE, NwConstants.TABLE_MISS_FLOW),
+ NwConstants.TABLE_MISS_PRIORITY, "Table Miss", 0, 0, COOKIE_TABLE_MISS, matches, instructions);
+
+ FlowEntity flowEntityFib = MDSALUtil.buildFlowEntity(dpnId,L3_FIB_TABLE, getFlowRef(dpnId, L3_FIB_TABLE, NwConstants.TABLE_MISS_FLOW),
+ NwConstants.TABLE_MISS_PRIORITY, "FIB Table Miss Flow", 0, 0, COOKIE_VM_FIB_TABLE,
+ matches, instructions);
+
+ if (addOrRemove == NwConstants.ADD_FLOW) {
+ LOG.debug("Invoking MDSAL to install Table Miss Entries");
+ mdsalManager.installFlow(flowEntityLfib);
+ mdsalManager.installFlow(flowEntityFib);
+ } else {
+ mdsalManager.removeFlow(flowEntityLfib);
+ mdsalManager.removeFlow(flowEntityFib);
+
+ }
+ }
+
+ private String getFlowRef(BigInteger dpnId, short tableId, int tableMiss) {
+ return new StringBuffer().append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
+ .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tableMiss)
+ .append(FLOWID_PREFIX).toString();
+ }