69a0d0c0fd2da82362661efb5cb46bf092728272
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / nxmatches / NxMatchInfoHelper.java
1 /*
2  * Copyright © 2017 Red Hat, Inc. and others.
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.nxmatches;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
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;
26
27 /**
28  * Helper for Nicira extension matches (this is designed to be absorbed into MatchInfo once we've cleaned up
29  * downstream users).
30  */
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;
35
36     protected NxMatchInfoHelper(Class<? extends ExtensionKey> keyClass) {
37         this.keyClass = keyClass;
38         builderClass = SuperTypeUtil.getTypeParameter(getClass(), 1);
39     }
40
41     @Override
42     public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
43         populateBuilder((B) mapMatchBuilder.computeIfAbsent(builderClass, key -> {
44             try {
45                 return builderClass.newInstance();
46             } catch (InstantiationException | IllegalAccessException e) {
47                 throw new IllegalStateException("Unable to create an instance of " + builderClass, e);
48             }
49         }));
50     }
51
52     @Override
53     public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
54         B builder = (B) mapMatchBuilder.remove(builderClass);
55
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);
65         }
66     }
67
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();
74         }
75         if (extensions == null) {
76             extensions = new ArrayList<>();
77         }
78         extensions.add(new ExtensionListBuilder().setExtensionKey(extentionKey)
79                 .setExtension(
80                         new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class, nxAugMatch).build())
81                 .build());
82         return new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(extensions).build();
83     }
84
85     protected abstract void applyValue(NxAugMatchNodesNodeTableFlowBuilder 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         NxMatchInfoHelper<?, ?> that = (NxMatchInfoHelper<?, ?>) 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
113     @Override
114     public abstract String toString();
115 }