Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / sfc / translator / src / main / java / org / opendaylight / netvirt / sfc / translator / NeutronMdsalHelper.java
1 /*
2  * Copyright (c) 2016, 2017 Brocade Communications 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
9 package org.opendaylight.netvirt.sfc.translator;
10
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.SfcFlowClassifiers;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.sfc.flow.classifiers.SfcFlowClassifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.sfc.flow.classifiers.SfcFlowClassifierKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.PortPairGroups;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.PortPairs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pair.groups.PortPairGroup;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pair.groups.PortPairGroupKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPair;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPairKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Utility functions to read Neutron models (e.g network, subnet, port, sfc flow classifier
35  * port pair, port group, port chain) from md-sal data store.
36  */
37 public class NeutronMdsalHelper {
38     private static final Logger LOG = LoggerFactory.getLogger(NeutronMdsalHelper.class);
39     private static final InstanceIdentifier<SfcFlowClassifiers> FC_IID =
40             InstanceIdentifier.create(Neutron.class).child(SfcFlowClassifiers.class);
41     private static final InstanceIdentifier<Ports> PORTS_IID =
42             InstanceIdentifier.create(Neutron.class).child(Ports.class);
43     private static final InstanceIdentifier<PortPairs> PORT_PAIRS_IID =
44             InstanceIdentifier.create(Neutron.class).child(PortPairs.class);
45     private static final InstanceIdentifier<PortPairGroups> PORT_PAIR_GROUPS_IID =
46             InstanceIdentifier.create(Neutron.class).child(PortPairGroups.class);
47
48     private final DataBroker dataBroker;
49
50     public NeutronMdsalHelper(DataBroker dataBroker) {
51         this.dataBroker = dataBroker;
52     }
53
54     @Nullable
55     public Port getNeutronPort(Uuid portId) {
56         return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
57                 LogicalDatastoreType.CONFIGURATION, getNeutronPortPath(portId)).orNull();
58     }
59
60     @Nullable
61     public PortPair getNeutronPortPair(Uuid portPairId) {
62         return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
63                 LogicalDatastoreType.CONFIGURATION, getNeutronPortPairPath(portPairId)).orNull();
64     }
65
66     @Nullable
67     public PortPairGroup getNeutronPortPairGroup(Uuid portPairGroupId) {
68         return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
69                 LogicalDatastoreType.CONFIGURATION, getNeutronPortPairGroupPath(portPairGroupId)).orNull();
70     }
71
72     @Nullable
73     public SfcFlowClassifier getNeutronFlowClassifier(Uuid flowClassifierId) {
74         return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
75                 LogicalDatastoreType.CONFIGURATION, getNeutronSfcFlowClassifierPath(flowClassifierId)).orNull();
76     }
77
78     private InstanceIdentifier<Port> getNeutronPortPath(Uuid portId) {
79         return PORTS_IID.builder().child(Port.class, new PortKey(portId)).build();
80     }
81
82     private InstanceIdentifier<PortPair> getNeutronPortPairPath(Uuid portPairId) {
83         return PORT_PAIRS_IID.builder().child(PortPair.class, new PortPairKey(portPairId)).build();
84     }
85
86     private InstanceIdentifier<PortPairGroup> getNeutronPortPairGroupPath(Uuid portPairGroupId) {
87         return PORT_PAIR_GROUPS_IID.builder().child(PortPairGroup.class, new PortPairGroupKey(portPairGroupId)).build();
88     }
89
90     private InstanceIdentifier<SfcFlowClassifier> getNeutronSfcFlowClassifierPath(Uuid portId) {
91         return FC_IID.builder().child(SfcFlowClassifier.class, new SfcFlowClassifierKey(portId)).build();
92     }
93 }