MRI version bump for Aluminium
[genius.git] / interfacemanager / interfacemanager-api / src / main / java / org / opendaylight / genius / interfacemanager / globals / InterfaceServiceUtil.java
1 /*
2  * Copyright (c) 2016, 2017 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 package org.opendaylight.genius.interfacemanager.globals;
9
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
18 import org.opendaylight.genius.mdsalutil.FlowInfoKey;
19 import org.opendaylight.genius.mdsalutil.GroupInfoKey;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
23 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
24 import org.opendaylight.genius.mdsalutil.matches.MatchInPort;
25 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
26 import org.opendaylight.genius.mdsalutil.matches.MatchVlanVid;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.Uint16;
46 import org.opendaylight.yangtools.yang.common.Uint64;
47 import org.opendaylight.yangtools.yang.common.Uint8;
48
49 public final class InterfaceServiceUtil {
50
51     private InterfaceServiceUtil() {
52     }
53
54     /**
55      * Build a service information structure.
56      *
57      * @deprecated Use {@link #buildServiceInfo(String, Uint8)}.
58      */
59     @Deprecated
60     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, Uint8 servicePriority,
61             Uint64 cookie, List<Instruction> instructions) {
62         return buildServiceInfo(serviceName, servicePriority);
63     }
64
65     /**
66      * Build a service information structure.
67      *
68      * @deprecated Use {@link #buildServiceInfo(String, Uint8)}.
69      */
70     @Deprecated
71     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, Uint8 servicePriority,
72             Uint64 cookie) {
73         return buildServiceInfo(serviceName, servicePriority);
74     }
75
76     public static ServicesInfo buildServiceInfo(String serviceName, Uint8 servicePriority) {
77         List<BoundServices> boundService = new ArrayList<>();
78         boundService.add(new BoundServicesBuilder().setServicePriority(servicePriority)
79                 .setServiceName(serviceName).build());
80         return new ServicesInfoBuilder().setBoundServices(boundService)
81                 .withKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
82     }
83
84     public static BoundServices getBoundServices(String serviceName, Uint8 servicePriority, Uint16 flowPriority,
85             Uint64 cookie, List<Instruction> instructions) {
86         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
87                 .setInstruction(instructions);
88         return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
89                 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
90                 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
91     }
92
93     public static List<MatchInfo> getMatchInfoForVlanLPort(Uint64 dpId, long portNo, long vlanId,
94             boolean isVlanTransparent) {
95         List<MatchInfo> matches = new ArrayList<>();
96         matches.add(new MatchInPort(dpId, portNo));
97         if (vlanId != 0 && !isVlanTransparent) {
98             matches.add(new MatchVlanVid((int) vlanId));
99         }
100         return matches;
101     }
102
103     /**
104      * If matches contains MatchMetadata in its list and match is of type MatchMetadata, then this
105      * function will merge the MatchMetadatas using "or" of the masks and the values, otherwise it will add
106      * the match to the matches list.
107      *
108      * @param matches - matches list
109      * @param match - metadata or other match
110      */
111     public static void mergeMetadataMatchsOrAdd(List<MatchInfoBase> matches, MatchInfoBase match) {
112         Iterator<MatchInfoBase> iter = matches.iterator();
113         while (iter.hasNext()) {
114             MatchInfoBase match2 = iter.next();
115             if (match2 instanceof MatchMetadata) {
116                 if (match instanceof MatchMetadata) {
117                     MatchMetadata metadataMatch = (MatchMetadata) match;
118                     Uint64 value = MetaDataUtil.mergeMetadataValues(((MatchMetadata) match2).getMetadata(),
119                             metadataMatch.getMetadata());
120                     Uint64 mask = MetaDataUtil.mergeMetadataMask(((MatchMetadata) match2).getMask(),
121                             metadataMatch.getMask());
122                     match = new MatchMetadata(value, mask);
123                     iter.remove();
124                 }
125                 break;
126             }
127         }
128         matches.add(match);
129     }
130
131     public static short getVlanId(String interfaceName, DataBroker broker) throws ExecutionException,
132             InterruptedException {
133         InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
134                 .child(Interface.class, new InterfaceKey(interfaceName)).build();
135         Optional<Interface> ifInstance = SingleTransactionDataBroker.syncReadOptional(broker,
136                 LogicalDatastoreType.CONFIGURATION, id);
137         if (ifInstance.isPresent()) {
138             IfL2vlan vlanIface = ifInstance.get().augmentation(IfL2vlan.class);
139             return vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
140         }
141         return -1;
142     }
143
144     public static Set<Object> getStatRequestKeys(Uint64 dpId, short tableId, List<MatchInfo> matches, String flowId,
145             long groupId) {
146         Set<Object> statRequestKeys = new HashSet<>();
147         statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
148         statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
149         return statRequestKeys;
150     }
151
152     public static GroupInfoKey getGroupStatisticsKey(Uint64 dpId, long groupId) {
153         return new GroupInfoKey(dpId, groupId);
154     }
155
156     public static FlowInfoKey getFlowStatisticsKey(Uint64 dpId, short tableId, List<MatchInfo> matches,
157             String flowId) {
158         return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
159     }
160
161     public static List<MatchInfo> getLPortDispatcherMatches(short serviceIndex, int interfaceTag) {
162         List<MatchInfo> mkMatches = new ArrayList<>();
163         mkMatches.add(new MatchMetadata(
164                  MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, serviceIndex),
165                  MetaDataUtil.getMetaDataMaskForLPortDispatcher()));
166         return mkMatches;
167     }
168 }