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