Migrate to simplified addAugmentation() method
[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.Map;
15 import java.util.Optional;
16 import java.util.Set;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
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.MatchInfo;
23 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
24 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
25 import org.opendaylight.genius.mdsalutil.matches.MatchInPort;
26 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
27 import org.opendaylight.genius.mdsalutil.matches.MatchVlanVid;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
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         BoundServices boundService = new BoundServicesBuilder()
78                 .setServicePriority(servicePriority)
79                 .setServiceName(serviceName)
80                 .build();
81         return new ServicesInfoBuilder().setBoundServices(Map.of(boundService.key(), boundService))
82                 .withKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
83     }
84
85     public static BoundServices getBoundServices(String serviceName, Uint8 servicePriority, Uint16 flowPriority,
86             Uint64 cookie, List<Instruction> instructions) {
87         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
88                 .setInstruction(instructions);
89         return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
90                 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
91                 .addAugmentation(augBuilder.build()).build();
92     }
93
94     public static List<MatchInfo> getMatchInfoForVlanLPort(Uint64 dpId, long portNo, long vlanId,
95             boolean isVlanTransparent) {
96         List<MatchInfo> matches = new ArrayList<>();
97         matches.add(new MatchInPort(dpId, portNo));
98         if (vlanId != 0 && !isVlanTransparent) {
99             matches.add(new MatchVlanVid((int) vlanId));
100         }
101         return matches;
102     }
103
104     /**
105      * If matches contains MatchMetadata in its list and match is of type MatchMetadata, then this
106      * function will merge the MatchMetadatas using "or" of the masks and the values, otherwise it will add
107      * the match to the matches list.
108      *
109      * @param matches - matches list
110      * @param match - metadata or other match
111      */
112     public static void mergeMetadataMatchsOrAdd(List<MatchInfoBase> matches, MatchInfoBase match) {
113         Iterator<MatchInfoBase> iter = matches.iterator();
114         while (iter.hasNext()) {
115             MatchInfoBase match2 = iter.next();
116             if (match2 instanceof MatchMetadata) {
117                 if (match instanceof MatchMetadata) {
118                     MatchMetadata metadataMatch = (MatchMetadata) match;
119                     Uint64 value = MetaDataUtil.mergeMetadataValues(((MatchMetadata) match2).getMetadata(),
120                             metadataMatch.getMetadata());
121                     Uint64 mask = MetaDataUtil.mergeMetadataMask(((MatchMetadata) match2).getMask(),
122                             metadataMatch.getMask());
123                     match = new MatchMetadata(value, mask);
124                     iter.remove();
125                 }
126                 break;
127             }
128         }
129         matches.add(match);
130     }
131
132     public static short getVlanId(String interfaceName, DataBroker broker) throws ExecutionException,
133             InterruptedException {
134         InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
135                 .child(Interface.class, new InterfaceKey(interfaceName)).build();
136         Optional<Interface> ifInstance = SingleTransactionDataBroker.syncReadOptional(broker,
137                 LogicalDatastoreType.CONFIGURATION, id);
138         if (ifInstance.isPresent()) {
139             IfL2vlan vlanIface = ifInstance.get().augmentation(IfL2vlan.class);
140             return vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
141         }
142         return -1;
143     }
144
145     public static Set<Object> getStatRequestKeys(Uint64 dpId, short tableId, List<MatchInfo> matches, String flowId,
146             long groupId) {
147         Set<Object> statRequestKeys = new HashSet<>();
148         statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
149         statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
150         return statRequestKeys;
151     }
152
153     public static GroupInfoKey getGroupStatisticsKey(Uint64 dpId, long groupId) {
154         return new GroupInfoKey(dpId, groupId);
155     }
156
157     public static FlowInfoKey getFlowStatisticsKey(Uint64 dpId, short tableId, List<MatchInfo> matches,
158             String flowId) {
159         return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
160     }
161
162     public static List<MatchInfo> getLPortDispatcherMatches(short serviceIndex, int interfaceTag) {
163         List<MatchInfo> mkMatches = new ArrayList<>();
164         mkMatches.add(new MatchMetadata(
165                  MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, serviceIndex),
166                  MetaDataUtil.getMetaDataMaskForLPortDispatcher()));
167         return mkMatches;
168     }
169 }