apply checkstyle check during build for neutron-ovsdb
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / EndpointHelper.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.groupbasedpolicy.neutron.ovsdb.util;
9
10 import static org.opendaylight.groupbasedpolicy.util.DataStoreHelper.readFromDs;
11 import static org.opendaylight.groupbasedpolicy.util.IidFactory.endpointIid;
12
13 import com.google.common.base.Optional;
14
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
19 import org.opendaylight.groupbasedpolicy.util.IidFactory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27
28
29 public class EndpointHelper {
30
31     /**
32      * Look up the {@link Endpoint} from the Endpoint Registry.
33      *
34      * @param epKey The {@link EndpointKey} to look up
35      * @param transaction The {@link ReadOnlyTransaction}
36      * @return The corresponding {@link Endpoint}, null if not found
37      */
38     public static Endpoint lookupEndpoint(EndpointKey epKey, ReadOnlyTransaction transaction) {
39
40         Optional<Endpoint> optionalEp =
41             readFromDs(LogicalDatastoreType.OPERATIONAL, endpointIid(epKey.getL2Context(), epKey.getMacAddress()),
42                 transaction);
43         if (optionalEp.isPresent()) {
44             return optionalEp.get();
45         }
46         return null;
47     }
48
49     /**
50      * Updates an {@link Endpoint} location based on OVSDB Termination point notification.
51      * Note this updates the datastore directly. It does not use the Endpoint RPC, as this has
52      * unfortunate side-effects on EndpointL3 augmentations.
53      *
54      * @param endpoint               the network endpoint
55      * @param nodeIdString           the string representation of the inventory NodeId
56      * @param nodeConnectorIdString  the string representation of the inventory NodeConnectorId
57      * @param rwTx                   a reference to ReadWriteTransaction object
58      */
59     public static void updateEndpointWithLocation(Endpoint endpoint, String nodeIdString, String nodeConnectorIdString,
60             ReadWriteTransaction rwTx) {
61         NodeId invNodeId = new NodeId(nodeIdString);
62         NodeConnectorId ncId = new NodeConnectorId(nodeConnectorIdString);
63         OfOverlayContext newOfOverlayCtx =
64                 new OfOverlayContextBuilder().setNodeId(invNodeId).setNodeConnectorId(ncId).build();
65         rwTx.merge(LogicalDatastoreType.OPERATIONAL,
66                 IidFactory.endpointIid(endpoint.getKey()).augmentation(OfOverlayContext.class), newOfOverlayCtx);
67         DataStoreHelper.submitToDs(rwTx);
68     }
69
70     public static void updateEndpointRemoveLocation(Endpoint endpoint, ReadWriteTransaction rwTx) {
71         EndpointBuilder epBuilder = new EndpointBuilder(endpoint);
72         Endpoint newEp = epBuilder.build();
73         epBuilder.removeAugmentation(OfOverlayContext.class);
74         rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointIid(newEp.getL2Context(), newEp.getMacAddress()),
75             newEp);
76         DataStoreHelper.submitToDs(rwTx);
77     }
78
79 }