2a0c8ff7da69cc6dab77dc599980c83e604a21bf
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / ericmatches / EricMatchInfoHelper.java
1 /*
2  * Copyright (c) 2019 Alten Calsoft Labs India 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.mdsalutil.ericmatches;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13 import org.opendaylight.genius.mdsalutil.MatchInfo;
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.eric.match.rev180730.EricAugMatchNodesNodeTableFlow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.eric.match.rev180730.EricAugMatchNodesNodeTableFlowBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
24 import org.opendaylight.yangtools.concepts.Builder;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26
27
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;
32
33     protected EricMatchInfoHelper(Class<? extends ExtensionKey> keyClass) {
34         this.keyClass = keyClass;
35         builderClass = SuperTypeUtil.getTypeParameter(getClass(), 1);
36     }
37
38     @Override
39     public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
40         populateBuilder((B) mapMatchBuilder.computeIfAbsent(builderClass, key -> {
41             try {
42                 return builderClass.newInstance();
43             } catch (InstantiationException | IllegalAccessException e) {
44                 throw new IllegalStateException("Unable to create an instance of " + builderClass, e);
45             }
46         }));
47     }
48
49     @Override
50     public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
51         B builder = (B) mapMatchBuilder.remove(builderClass);
52
53         if (builder != null) {
54
55             EricAugMatchNodesNodeTableFlowBuilder ericAugMatchBuilder = new EricAugMatchNodesNodeTableFlowBuilder();
56             applyValue(ericAugMatchBuilder, builder.build());
57             EricAugMatchNodesNodeTableFlow ericAugMatch = ericAugMatchBuilder.build();
58             GeneralAugMatchNodesNodeTableFlow existingAugmentations = matchBuilder
59                     .augmentation(GeneralAugMatchNodesNodeTableFlow.class);
60             GeneralAugMatchNodesNodeTableFlow genAugMatch = generalAugMatchBuilder(existingAugmentations,
61                     ericAugMatch, keyClass);
62             matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, genAugMatch);
63         }
64     }
65
66     private GeneralAugMatchNodesNodeTableFlow generalAugMatchBuilder(
67             GeneralAugMatchNodesNodeTableFlow existingAugmentations, EricAugMatchNodesNodeTableFlow ericAugMatch,
68             Class<? extends ExtensionKey> extentionKey) {
69         List<ExtensionList> extensions = null;
70         if (existingAugmentations != null) {
71             extensions = existingAugmentations.getExtensionList();
72         }
73         if (extensions == null) {
74             extensions = new ArrayList<>();
75         }
76         extensions.add(new ExtensionListBuilder().setExtensionKey(extentionKey)
77                 .setExtension(
78                         new ExtensionBuilder().addAugmentation(EricAugMatchNodesNodeTableFlow.class, ericAugMatch)
79                                 .build())
80                 .build());
81         return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
82     }
83
84     protected abstract void applyValue(EricAugMatchNodesNodeTableFlowBuilder matchBuilder, T value);
85
86     protected abstract void populateBuilder(B builder);
87
88     @Override
89     public boolean equals(Object other) {
90         if (this == other) {
91             return true;
92         }
93         if (other == null || getClass() != other.getClass()) {
94             return false;
95         }
96
97         EricMatchInfoHelper<?, ?> that = (EricMatchInfoHelper<?, ?>) other;
98
99         if (builderClass != null ? !builderClass.equals(that.builderClass) : that.builderClass != null) {
100             return false;
101         }
102         return keyClass != null ? keyClass.equals(that.keyClass) : that.keyClass == null;
103     }
104
105     @Override
106     public int hashCode() {
107         int result = builderClass != null ? builderClass.hashCode() : 0;
108         result = 31 * result + (keyClass != null ? keyClass.hashCode() : 0);
109         return result;
110     }
111 }