2 * Copyright (c) 2018 Xoriant Corporation 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.unimgr.mef.legato;
11 import com.google.common.base.Optional;
13 import java.util.Collections;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener;
20 import org.opendaylight.unimgr.mef.legato.util.LegatoConstants;
21 import org.opendaylight.unimgr.mef.legato.util.LegatoUtils;
22 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.MefGlobal;
23 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.L2cpEecProfiles;
24 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.L2cpEecProfilesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.l2cp.eec.profiles.Profile;
26 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.l2cp.eec.profiles.ProfileKey;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
34 * @author Arif.Hussain@Xoriant.Com
37 public class LegatoL2cpEecController extends UnimgrDataTreeChangeListener<Profile> {
39 private static final Logger LOG = LoggerFactory.getLogger(LegatoL2cpEecController.class);
41 private static final InstanceIdentifier<Profile> PROFILE_ID = InstanceIdentifier
42 .builder(MefGlobal.class).child(L2cpEecProfiles.class).child(Profile.class).build();
44 private static final InstanceIdentifier<L2cpEecProfiles> L2CP_EEC_PROFILES_ID_OPERATIONAL = InstanceIdentifier
45 .builder(MefGlobal.class).child(L2cpEecProfiles.class).build();
47 private ListenerRegistration<LegatoL2cpEecController> dataTreeChangeListenerRegistration;
49 public LegatoL2cpEecController(DataBroker dataBroker) {
54 public void registerListener() {
55 LOG.info("Initializing LegatoL2cpEecController:init() ");
57 dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
58 new DataTreeIdentifier<Profile>(LogicalDatastoreType.CONFIGURATION, PROFILE_ID), this);
62 public void add(DataTreeModification<Profile> newDataObject) {
63 LOG.info("ClassName :: LegatoL2cpEecController, Method:: add(), Message:: Node Added "
64 + newDataObject.getRootNode().getIdentifier());
65 addToOperationalDB(newDataObject.getRootNode().getDataAfter());
68 public void addToOperationalDB(Profile profileObj) {
70 LOG.info(" inside addNode()");
73 assert profileObj != null;
75 L2cpEecProfiles l2cpEecProfiles = new L2cpEecProfilesBuilder()
76 .setProfile(Collections.singletonList(profileObj)).build();
77 LegatoUtils.addToOperationalDB(l2cpEecProfiles, L2CP_EEC_PROFILES_ID_OPERATIONAL,
79 } catch (Exception e) {
80 LOG.error("Error in addNode(). Err: ", e);
82 LOG.info(" ********** END addNode() ****************** ");
87 public void update(DataTreeModification<Profile> modifiedDataObject) {
88 if (modifiedDataObject.getRootNode() != null && modifiedDataObject.getRootPath() != null) {
89 LOG.info("ClassName :: LegatoL2cpEecController, Method:: update(), Message:: Node modified "
90 + modifiedDataObject.getRootNode().getIdentifier());
93 assert modifiedDataObject.getRootNode().getDataAfter() != null;
94 updateFromOperationalDB(modifiedDataObject.getRootNode().getDataAfter());
95 } catch (Exception ex) {
96 LOG.error("error: ", ex);
101 @SuppressWarnings("unchecked")
102 public void updateFromOperationalDB(Profile profile) {
103 assert profile != null;
104 Optional<Profile> optionalProfile =
105 (Optional<Profile>) LegatoUtils.readProfile(LegatoConstants.L2CP_EEC_PROFILES,
106 dataBroker, LogicalDatastoreType.CONFIGURATION,
107 InstanceIdentifier.create(MefGlobal.class).child(L2cpEecProfiles.class)
108 .child(Profile.class, new ProfileKey(profile.getId())));
110 if (optionalProfile.isPresent()) {
112 LegatoUtils.deleteFromOperationalDB(InstanceIdentifier.create(MefGlobal.class).child(L2cpEecProfiles.class)
113 .child(Profile.class, new ProfileKey(profile.getId())), dataBroker);
115 addToOperationalDB(optionalProfile.get());
121 public void remove(DataTreeModification<Profile> removedDataObject) {
122 if (removedDataObject.getRootNode() != null && removedDataObject.getRootPath() != null) {
124 "ClassName :: LegatoL2cpEecController, Method:: remove(), Message:: Node removed "
125 + removedDataObject.getRootNode().getIdentifier());
128 assert removedDataObject.getRootNode().getDataBefore() != null;
129 deleteFromOperationalDB(removedDataObject.getRootNode().getDataBefore());
130 } catch (Exception ex) {
131 LOG.error("error: ", ex);
137 public void deleteFromOperationalDB(Profile profile) {
139 assert profile != null;
141 .deleteFromOperationalDB(
142 InstanceIdentifier.create(MefGlobal.class).child(L2cpEecProfiles.class)
143 .child(Profile.class, new ProfileKey(profile.getId())),
145 } catch (Exception ex) {
146 LOG.error("error: ", ex);
151 public void close() throws Exception {
152 if (dataTreeChangeListenerRegistration != null) {
153 dataTreeChangeListenerRegistration.close();