Merge "L2 Gw create changes related to ITM Tunnels creation in neutronvpn module"
[vpnservice.git] / interfacemgr / interfacemgr-api / src / main / java / org / opendaylight / vpnservice / interfacemgr / globals / InterfaceServiceUtil.java
1 /*
2  * Copyright (c) 2015 - 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 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.ServiceTypeFlowBased;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.StypeOpenflow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.StypeOpenflowBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServicesKey;
41
42 import com.google.common.base.Optional;
43
44 public class InterfaceServiceUtil {
45
46     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
47             BigInteger cookie, List<Instruction> instructions) {
48         List<BoundServices>  boundService = new ArrayList<BoundServices>();
49         boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
50         return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName)).build();
51     }
52
53     public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
54                                                  BigInteger cookie, List<Instruction> instructions) {
55         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority).setInstruction(instructions);
56         return new BoundServicesBuilder().setKey(new BoundServicesKey(servicePriority))
57                 .setServiceName(serviceName).setServicePriority(servicePriority)
58                 .setServiceType(ServiceTypeFlowBased.class).addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
59     }
60
61     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
62             BigInteger cookie) {
63         List<BoundServices>  boundService = new ArrayList<BoundServices>();
64         boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
65         return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName)).build();
66     }
67
68     public static List<MatchInfo> getMatchInfoForVlanLPort(BigInteger dpId, long portNo, long vlanId, boolean isVlanTransparent) {
69         List<MatchInfo> matches = new ArrayList<MatchInfo>();
70         matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNo)}));
71         if (vlanId != 0 && !isVlanTransparent) {
72             matches.add(new MatchInfo(MatchFieldType.vlan_vid, new long[] { vlanId }));
73         }
74         return matches;
75     }
76
77     public static short getVlanId(String interfaceName, DataBroker broker) {
78         InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
79                 .child(Interface.class, new InterfaceKey(interfaceName)).build();
80         Optional<Interface> ifInstance = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, id, broker);
81         if (ifInstance.isPresent()) {
82             IfL2vlan vlanIface =ifInstance.get().getAugmentation(IfL2vlan.class);
83             short vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
84             return vlanId;
85         }
86         return -1;
87     }
88
89     public static Set<Object> getStatRequestKeys(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId, long groupId) {
90         Set<Object> statRequestKeys = new HashSet<Object>();
91         statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
92         statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
93         return statRequestKeys;
94     }
95
96     public static GroupInfoKey getGroupStatisticsKey(BigInteger dpId, long groupId) {
97         return new GroupInfoKey(dpId, groupId);
98     }
99
100     public static FlowInfoKey getFlowStatisticsKey(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId) {
101         return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
102     }
103
104     public static List<MatchInfo> getLPortDispatcherMatches(short serviceIndex, int interfaceTag) {
105         List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
106         mkMatches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
107                  MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, serviceIndex),
108                  MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));
109         return mkMatches;
110     }
111
112 }