2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.interfacemgr.globals;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.mdsalutil.FlowInfoKey;
20 import org.opendaylight.vpnservice.mdsalutil.GroupInfoKey;
21 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
22 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
23 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
24 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServicesBuilder;
38 import com.google.common.base.Optional;
40 public class InterfaceServiceUtil {
42 public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
43 BigInteger cookie, List<Instruction> instructions) {
44 List<BoundServices> boundService = new ArrayList<BoundServices>();
45 boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
46 return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName)).build();
49 public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
51 List<BoundServices> boundService = new ArrayList<BoundServices>();
52 boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
53 return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName)).build();
56 public static List<MatchInfo> getMatchInfoForVlanLPort(BigInteger dpId, long portNo, long vlanId, boolean isVlanTransparent) {
57 List<MatchInfo> matches = new ArrayList<MatchInfo>();
58 matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNo)}));
59 if (vlanId != 0 && !isVlanTransparent) {
60 matches.add(new MatchInfo(MatchFieldType.vlan_vid, new long[] { vlanId }));
65 public static short getVlanId(String interfaceName, DataBroker broker) {
66 InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
67 .child(Interface.class, new InterfaceKey(interfaceName)).build();
68 Optional<Interface> ifInstance = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, id, broker);
69 if (ifInstance.isPresent()) {
70 IfL2vlan vlanIface =ifInstance.get().getAugmentation(IfL2vlan.class);
71 short vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
77 public static Set<Object> getStatRequestKeys(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId, long groupId) {
78 Set<Object> statRequestKeys = new HashSet<Object>();
79 statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
80 statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
81 return statRequestKeys;
84 public static GroupInfoKey getGroupStatisticsKey(BigInteger dpId, long groupId) {
85 return new GroupInfoKey(dpId, groupId);
88 public static FlowInfoKey getFlowStatisticsKey(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId) {
89 return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
92 public static List<MatchInfo> getLPortDispatcherMatches(short serviceIndex, int interfaceTag) {
93 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
94 mkMatches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
95 MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, serviceIndex),
96 MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));