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