2 * Copyright © 2017 Red Hat, Inc. and others.
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.mdsalutil.nxmatches;
10 import java.util.ArrayList;
11 import java.util.List;
13 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
14 import org.opendaylight.genius.utils.SuperTypeUtil;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
24 import org.opendaylight.yangtools.concepts.Builder;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
28 * Helper for Nicira extension matches (this is designed to be absorbed into MatchInfo once we've cleaned up
31 public abstract class NxMatchInfoHelper<T extends DataObject, B extends Builder<T>> implements NxMatchInfo {
32 private final Class<B> builderClass;
33 // The key class can't be a type parameter, it varies in some subclasses
34 private final Class<? extends ExtensionKey> keyClass;
36 protected NxMatchInfoHelper(Class<? extends ExtensionKey> keyClass) {
37 this.keyClass = keyClass;
38 builderClass = SuperTypeUtil.getTypeParameter(getClass(), 1);
42 public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
43 populateBuilder((B) mapMatchBuilder.computeIfAbsent(builderClass, key -> {
45 return builderClass.newInstance();
46 } catch (InstantiationException | IllegalAccessException e) {
47 throw new IllegalStateException("Unable to create an instance of " + builderClass, e);
53 public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
54 B builder = (B) mapMatchBuilder.remove(builderClass);
56 if (builder != null) {
57 NxAugMatchNodesNodeTableFlowBuilder nxAugMatchBuilder = new NxAugMatchNodesNodeTableFlowBuilder();
58 applyValue(nxAugMatchBuilder, builder.build());
59 NxAugMatchNodesNodeTableFlow nxAugMatch = nxAugMatchBuilder.build();
60 GeneralAugMatchNodesNodeTableFlow existingAugmentations = matchBuilder
61 .augmentation(GeneralAugMatchNodesNodeTableFlow.class);
62 GeneralAugMatchNodesNodeTableFlow genAugMatch = generalAugMatchBuilder(existingAugmentations,
63 nxAugMatch, keyClass);
64 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
68 private GeneralAugMatchNodesNodeTableFlow generalAugMatchBuilder(
69 GeneralAugMatchNodesNodeTableFlow existingAugmentations, NxAugMatchNodesNodeTableFlow nxAugMatch,
70 Class<? extends ExtensionKey> extentionKey) {
71 List<ExtensionList> extensions = null;
72 if (existingAugmentations != null) {
73 extensions = existingAugmentations.getExtensionList();
75 if (extensions == null) {
76 extensions = new ArrayList<>();
78 extensions.add(new ExtensionListBuilder().setExtensionKey(extentionKey)
80 new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch).build())
82 return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
85 protected abstract void applyValue(NxAugMatchNodesNodeTableFlowBuilder matchBuilder, T value);
87 protected abstract void populateBuilder(B builder);
90 public boolean equals(Object other) {
94 if (other == null || getClass() != other.getClass()) {
98 NxMatchInfoHelper<?, ?> that = (NxMatchInfoHelper<?, ?>) other;
100 if (builderClass != null ? !builderClass.equals(that.builderClass) : that.builderClass != null) {
103 return keyClass != null ? keyClass.equals(that.keyClass) : that.keyClass == null;
107 public int hashCode() {
108 int result = builderClass != null ? builderClass.hashCode() : 0;
109 result = 31 * result + (keyClass != null ? keyClass.hashCode() : 0);
114 public abstract String toString();