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