2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.interfacemanager.globals;
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;
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.StypeOpenflow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.Uint16;
47 import org.opendaylight.yangtools.yang.common.Uint64;
48 import org.opendaylight.yangtools.yang.common.Uint8;
50 public final class InterfaceServiceUtil {
52 private InterfaceServiceUtil() {
56 * Build a service information structure.
58 * @deprecated Use {@link #buildServiceInfo(String, Uint8)}.
61 public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, Uint8 servicePriority,
62 Uint64 cookie, List<Instruction> instructions) {
63 return buildServiceInfo(serviceName, servicePriority);
67 * Build a service information structure.
69 * @deprecated Use {@link #buildServiceInfo(String, Uint8)}.
72 public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, Uint8 servicePriority,
74 return buildServiceInfo(serviceName, servicePriority);
77 public static ServicesInfo buildServiceInfo(String serviceName, Uint8 servicePriority) {
78 List<BoundServices> boundService = new ArrayList<>();
79 boundService.add(new BoundServicesBuilder().setServicePriority(servicePriority)
80 .setServiceName(serviceName).build());
81 return new ServicesInfoBuilder().setBoundServices(boundService)
82 .withKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
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(StypeOpenflow.class, augBuilder.build()).build();
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));
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.
109 * @param matches - matches list
110 * @param match - metadata or other match
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);
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();
145 public static Set<Object> getStatRequestKeys(Uint64 dpId, short tableId, List<MatchInfo> matches, String flowId,
147 Set<Object> statRequestKeys = new HashSet<>();
148 statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
149 statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
150 return statRequestKeys;
153 public static GroupInfoKey getGroupStatisticsKey(Uint64 dpId, long groupId) {
154 return new GroupInfoKey(dpId, groupId);
157 public static FlowInfoKey getFlowStatisticsKey(Uint64 dpId, short tableId, List<MatchInfo> matches,
159 return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
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()));