2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.impl;
11 import java.util.HashMap;
13 import java.util.Random;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.nic.utils.MdsalUtils;
18 import org.opendaylight.vpnservice.utils.IidFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpnintent.rev150105.MplsLabels;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpnintent.rev150105.labels.Label;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpnintent.rev150105.labels.LabelBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpnintent.rev150105.labels.LabelKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpnintent.rev150105.vpn.intent.Endpoint;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class MplsLabelManagerService {
30 private static final Logger LOG = LoggerFactory.getLogger(MplsLabelManagerService.class);
31 private static final Integer MAX_MPLS_LABEL = 524288;
32 private static InstanceIdentifier<Label> LABEL_IID = null;
33 public static final InstanceIdentifier<MplsLabels> MPLS_LABELS_IID = IidFactory.getMplsLabelsIid();
34 private final DataBroker dataBroker;
35 private final Random random = new Random();
36 private final MdsalUtils mdsal;
38 public MplsLabelManagerService(DataBroker dataBroker) {
39 this.dataBroker = dataBroker;
40 this.mdsal = new MdsalUtils(this.dataBroker);
44 * Generate a unique Mpls Label
45 * Mpls label is of length 20 bits maximum
46 * @return :next unique Mpls label value
48 public Long getUniqueLabel(Endpoint endpoint) {
49 Long nextUniqueLabel = (long) random.nextInt(MAX_MPLS_LABEL);
50 while(checkIsLabelUsed(nextUniqueLabel)) {
51 nextUniqueLabel = (long) random.nextInt(MAX_MPLS_LABEL);
53 updateToLabelStore(nextUniqueLabel, endpoint, true);
54 return nextUniqueLabel;
58 * Delete label from datastore
59 * @param endpoint :endpoint whose label needs to be deleted
61 public void deleteLabel(Endpoint endpoint) {
62 Map<Long, String> labelMap = getAllLabels();
63 for (Map.Entry<Long, String> labelEntry : labelMap.entrySet()) {
64 if(labelEntry.getValue().equalsIgnoreCase(endpoint.getSiteName())) {
65 updateToLabelStore(labelEntry.getKey(), endpoint, false);
71 * Update the model for Labels with used Mpls label values
72 * @param label :mpls label allocated to an endpoint
73 * @param endpoint :endpoint to which mpls label is allocated to
74 * @param add :true if add label to datastore, false to delete label from datastore
76 private void updateToLabelStore(Long label, Endpoint endpoint, boolean add) {
77 LABEL_IID = IidFactory.getLabelIid(label);
78 Label mplsLabel = new LabelBuilder().
79 setKey(new LabelKey(label)).
81 setSiteName(endpoint.getSiteName()).
82 setIpPrefix(endpoint.getIpPrefix()).
83 setSwitchPortId(endpoint.getSwitchPortId()).build();
86 mdsal.put(LogicalDatastoreType.OPERATIONAL, LABEL_IID, mplsLabel);
87 LOG.info("Add mpls label to operational datastore: {} for endpoint: {}", label, endpoint.getSiteName());
89 mdsal.delete(LogicalDatastoreType.OPERATIONAL, LABEL_IID);
90 LOG.info("Delete mpls label from operational datastore: {} for endpoint: {}", label, endpoint.getSiteName());
95 * Check if label is already allocated to any endpoint
96 * @param nextUniqueLabel :value of mpls label
97 * @return :true is label is already used else false
99 private boolean checkIsLabelUsed(Long nextUniqueLabel) {
100 Map<Long, String> labelMap = getAllLabels();
101 if(labelMap.containsKey(nextUniqueLabel)) {
108 * Get a map of all the labels allocated to the endpoints
109 * @return :hashmap of labels as key, site names as value
111 private Map<Long, String> getAllLabels() {
112 Map<Long, String> labelMap = new HashMap<>();
113 MplsLabels mplsLabels = mdsal.read(LogicalDatastoreType.OPERATIONAL, MPLS_LABELS_IID);
114 if(mplsLabels != null) {
115 for (Label label : mplsLabels.getLabel()) {
116 labelMap.put(label.getLabelId(), label.getSiteName());