Merge "Remove He-design inventory manager"
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / NshNpConvertor.java
1 /*
2  * Copyright (c) 2015 Intel, Inc. 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.openflowplugin.extension.vendor.nicira.convertor.match;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
13 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
14 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.np.grouping.NshNpValuesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.NshNpCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.NshNpCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketInBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemovedBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStats;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStatsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStatsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshNpGrouping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshNpKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsh.np.grouping.NxmNxNshNp;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nsh.np.grouping.NxmNxNshNpBuilder;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39
40 public class NshNpConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
41     @Override
42     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
43         NshNpCaseValue nshNpCaseValue = ((NshNpCaseValue) input.getMatchEntryValue());
44
45         return resolveAugmentation(new NxmNxNshNpBuilder().setValue(nshNpCaseValue.getNshNpValues().getValue()).build(), path,
46                 NxmNxNshNpKey.class);
47     }
48
49     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxNshNp value,
50                                                                                            MatchPath path, Class<? extends ExtensionKey> key) {
51         switch (path) {
52             case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH:
53                 return new ExtensionAugment<>(NxAugMatchNotifUpdateFlowStats.class,
54                         new NxAugMatchNotifUpdateFlowStatsBuilder().setNxmNxNshNp(value).build(), key);
55             case RPCFLOWSSTATISTICS_FLOWANDSTATISTICSMAPLIST_MATCH:
56                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
57                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxNshNp(value).build(), key);
58             case PACKETRECEIVED_MATCH:
59                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
60                         .setNxmNxNshNp(value).build(), key);
61             case SWITCHFLOWREMOVED_MATCH:
62                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
63                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxNshNp(value).build(), key);
64             case PACKETINMESSAGE_MATCH:
65                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
66                         new NxAugMatchPacketInMessageBuilder().setNxmNxNshNp(value).build(), key);
67             default:
68                 throw new CodecPreconditionException(path);
69         }
70     }
71
72     @Override
73     public MatchEntry convert(Extension extension) {
74         Optional<NxmNxNshNpGrouping> matchGrouping = MatchUtil.nshNpResolver.getExtension(extension);
75         if (!matchGrouping.isPresent()) {
76             throw new CodecPreconditionException(extension);
77         }
78         Short value = matchGrouping.get().getNxmNxNshNp().getValue();
79         NshNpCaseValueBuilder nshNpCaseValueBuilder = new NshNpCaseValueBuilder();
80         nshNpCaseValueBuilder.setNshNpValues(new NshNpValuesBuilder()
81                 .setValue(value).build());
82
83
84         return MatchUtil.createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshNp.class,
85                 Nxm1Class.class,
86                 nshNpCaseValueBuilder.build()).build();
87     }
88
89 }