Bug 2756 - Action model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / TypeKeyMakerFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import org.opendaylight.openflowjava.protocol.api.keys.ActionSerializerKey;
12 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterActionSerializerKey;
13 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterInstructionSerializerKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.InstructionSerializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdInstruction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.ExperimenterIdCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.ActionChoice;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ExperimenterClass;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
24
25 /**
26  * Creates KeyMakers
27  * @author michal.polkorab
28  */
29 public abstract class TypeKeyMakerFactory {
30
31     private TypeKeyMakerFactory() {
32         //not called
33     }
34     
35     /**
36      * @param version openflow wire version that shall be used
37      *  in lookup key
38      * @return lookup key
39      */
40     public static TypeKeyMaker<MatchEntry> createMatchEntriesKeyMaker(short version) {
41         return new AbstractTypeKeyMaker<MatchEntry>(version) {
42             @Override
43             public MatchEntrySerializerKey<?, ?> make(MatchEntry entry) {
44                 MatchEntrySerializerKey<?, ?> key;
45                 key = new MatchEntrySerializerKey<>(getVersion(), entry.getOxmClass(),
46                         entry.getOxmMatchField());
47                 if (entry.getOxmClass().equals(ExperimenterClass.class)) {
48                     ExperimenterIdCase entryValue = (ExperimenterIdCase) entry.getMatchEntryValue();
49                     key.setExperimenterId(entryValue.getExperimenter().getExperimenter().getValue());
50                     return key;
51                 }
52                 key.setExperimenterId(null);
53                 return key;
54             }
55         };
56     }
57
58     /**
59      * @param version openflow wire version that shall be used
60      *  in lookup key
61      * @return lookup key
62      */
63     public static TypeKeyMaker<Action> createActionKeyMaker(short version) {
64         return new AbstractTypeKeyMaker<Action>(version) {
65             @Override
66             public MessageTypeKey<?> make(Action entry) {
67                 if (entry.getActionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow
68                         .augments.rev150225.action.container.action.choice.ExperimenterIdCase) {
69                     org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.action
70                     .container.action.choice.ExperimenterIdCase expIdCase = (org.opendaylight.yang.gen.v1.urn
71                     .opendaylight.openflow.augments.rev150225.action.container.action.choice
72                     .ExperimenterIdCase) entry.getActionChoice();
73                     return new ExperimenterActionSerializerKey(getVersion(),
74                             expIdCase.getExperimenter().getExperimenter().getValue(),
75                             expIdCase.getExperimenter().getSubType());
76                 }
77                 return new ActionSerializerKey<>(getVersion(), (Class<ActionChoice>) entry.getActionChoice().getImplementedInterface(), null);
78             }
79         };
80     }
81
82     /**
83      * @param version openflow wire version that shall be used
84      *  in lookup key
85      * @return lookup key
86      */
87     public static TypeKeyMaker<Instruction> createInstructionKeyMaker(short version) {
88         return new AbstractTypeKeyMaker<Instruction>(version) {
89             @Override
90             public MessageTypeKey<?> make(Instruction entry) {
91                 if (entry.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common
92                         .instruction.rev130731.Experimenter.class)) {
93                     return new ExperimenterInstructionSerializerKey(getVersion(),
94                             entry.getAugmentation(ExperimenterIdInstruction.class)
95                             .getExperimenter().getValue());
96                 }
97                 return new InstructionSerializerKey<>(getVersion(), entry.getType(), null);
98             }
99         };
100     }
101 }