2 * Copyright (c) 2019 Alten Calsoft Labs India 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.mdsalutil.ericmatches;
10 import java.util.HashMap;
12 import org.opendaylight.genius.mdsalutil.MatchInfo;
13 import org.opendaylight.genius.utils.SuperTypeUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.eric.match.rev180730.EricAugMatchNodesNodeTableFlow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.eric.match.rev180730.EricAugMatchNodesNodeTableFlowBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListKey;
24 import org.opendaylight.yangtools.concepts.Builder;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
28 public abstract class EricMatchInfoHelper<T extends DataObject, B extends Builder<T>> extends MatchInfo {
29 private final Class<B> builderClass;
30 // The key class can't be a type parameter, it varies in some subclasses
31 private final Class<? extends ExtensionKey> keyClass;
33 protected EricMatchInfoHelper(Class<? extends ExtensionKey> keyClass) {
34 this.keyClass = keyClass;
35 builderClass = SuperTypeUtil.getTypeParameter(getClass(), 1);
39 public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
40 populateBuilder((B) mapMatchBuilder.computeIfAbsent(builderClass, key -> {
42 return builderClass.getDeclaredConstructor().newInstance();
43 } catch (ReflectiveOperationException e) {
44 throw new IllegalStateException("Unable to create an instance of " + builderClass, e);
50 public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
51 B builder = (B) mapMatchBuilder.remove(builderClass);
53 if (builder != null) {
55 EricAugMatchNodesNodeTableFlowBuilder ericAugMatchBuilder = new EricAugMatchNodesNodeTableFlowBuilder();
56 applyValue(ericAugMatchBuilder, builder.build());
57 EricAugMatchNodesNodeTableFlow ericAugMatch = ericAugMatchBuilder.build();
58 GeneralAugMatchNodesNodeTableFlow existingAugmentations = matchBuilder
59 .augmentation(GeneralAugMatchNodesNodeTableFlow.class);
60 matchBuilder.addAugmentation(generalAugMatchBuilder(existingAugmentations, ericAugMatch, keyClass));
64 private static GeneralAugMatchNodesNodeTableFlow generalAugMatchBuilder(
65 GeneralAugMatchNodesNodeTableFlow existingAugmentations, EricAugMatchNodesNodeTableFlow ericAugMatch,
66 Class<? extends ExtensionKey> extentionKey) {
67 Map<ExtensionListKey, ExtensionList> extensions = null;
68 if (existingAugmentations != null) {
69 extensions = existingAugmentations.getExtensionList();
71 if (extensions == null) {
72 extensions = new HashMap<>();
74 ExtensionList extensionList = new ExtensionListBuilder().setExtensionKey(extentionKey)
75 .setExtension(new ExtensionBuilder().addAugmentation(ericAugMatch).build())
77 extensions.put(extensionList.key(),extensionList);
78 return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
81 protected abstract void applyValue(EricAugMatchNodesNodeTableFlowBuilder matchBuilder, T value);
83 protected abstract void populateBuilder(B builder);
86 public boolean equals(Object other) {
90 if (other == null || getClass() != other.getClass()) {
94 EricMatchInfoHelper<?, ?> that = (EricMatchInfoHelper<?, ?>) other;
96 if (builderClass != null ? !builderClass.equals(that.builderClass) : that.builderClass != null) {
99 return keyClass != null ? keyClass.equals(that.keyClass) : that.keyClass == null;
103 public int hashCode() {
104 int result = builderClass != null ? builderClass.hashCode() : 0;
105 result = 31 * result + (keyClass != null ? keyClass.hashCode() : 0);