ELAN FT Support for BE
[vpnservice.git] / interfacemgr / interfacemgr-api / src / main / java / org / opendaylight / vpnservice / interfacemgr / globals / InterfaceServiceUtil.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.interfacemgr.globals;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
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;
37
38 import com.google.common.base.Optional;
39
40 public class InterfaceServiceUtil {
41
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();
47     }
48
49     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
50             BigInteger cookie) {
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();
54     }
55
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 }));
61         }
62         return matches;
63     }
64
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();
72             return vlanId;
73         }
74         return -1;
75     }
76
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;
82     }
83
84     public static GroupInfoKey getGroupStatisticsKey(BigInteger dpId, long groupId) {
85         return new GroupInfoKey(dpId, groupId);
86     }
87
88     public static FlowInfoKey getFlowStatisticsKey(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId) {
89         return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
90     }
91
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() }));
97         return mkMatches;
98     }
99
100 }