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