Bug 3208: Add actions SetNsch[1-4] and matches Nsch[1-4]
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / Nshc1Convertor.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match;
9
10 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
11 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
12 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
13 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
14 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nshc._1.grouping.Nshc1ValuesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.Nshc1CaseValue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.Nshc1CaseValueBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc1Grouping;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketInBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemovedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStats;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStatsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxNshc1Key;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._1.grouping.NxmNxNshc1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.nshc._1.grouping.NxmNxNshc1Builder;
32 import org.opendaylight.yangtools.yang.binding.Augmentation;
33
34 import com.google.common.base.Optional;
35
36 public class Nshc1Convertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
37     @Override
38     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
39         Nshc1CaseValue nsc1CaseValue = ((Nshc1CaseValue) input.getMatchEntryValue());
40
41         return resolveAugmentation(new NxmNxNshc1Builder().setValue(nsc1CaseValue.getNshc1Values().getNshc()).build(), path,
42                 NxmNxNshc1Key.class);
43     }
44
45     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxNshc1 value,
46                                                                                            MatchPath path, Class<? extends ExtensionKey> key) {
47         switch (path) {
48             case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH:
49                 return new ExtensionAugment<>(NxAugMatchNotifUpdateFlowStats.class,
50                         new NxAugMatchNotifUpdateFlowStatsBuilder().setNxmNxNshc1(value).build(), key);
51             case PACKETRECEIVED_MATCH:
52                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
53                         .setNxmNxNshc1(value).build(), key);
54             case SWITCHFLOWREMOVED_MATCH:
55                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
56                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxNshc1(value).build(), key);
57             default:
58                 throw new CodecPreconditionException(path);
59         }
60     }
61
62     @Override
63     public MatchEntry convert(Extension extension) {
64         Optional<NxmNxNshc1Grouping> matchGrouping = MatchUtil.nsc1Resolver.getExtension(extension);
65         if (!matchGrouping.isPresent()) {
66             throw new CodecPreconditionException(extension);
67         }
68         Long value = matchGrouping.get().getNxmNxNshc1().getValue();
69         Nshc1CaseValueBuilder nsc1CaseValueBuilder = new Nshc1CaseValueBuilder();
70         nsc1CaseValueBuilder.setNshc1Values(new Nshc1ValuesBuilder()
71                 .setNshc(value).build());
72
73
74         return MatchUtil.createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshc1.class,
75                 Nxm1Class.class,
76                 nsc1CaseValueBuilder.build()).build();
77     }
78
79 }