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.CosProfiles;
24 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.CosProfilesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.cos.profiles.Profile;
26 import org.opendaylight.yang.gen.v1.urn.mef.yang.mef.global.rev171215.mef.global.cos.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
36 public class LegatoCosProfileController extends UnimgrDataTreeChangeListener<Profile> {
38 public LegatoCosProfileController(DataBroker dataBroker) {
43 private static final Logger LOG = LoggerFactory.getLogger(LegatoCosProfileController.class);
44 private static final InstanceIdentifier<Profile> PROFILE_IID = InstanceIdentifier.builder(MefGlobal.class)
45 .child(CosProfiles.class).child(Profile.class).build();
46 private ListenerRegistration<LegatoCosProfileController> dataTreeChangeListenerRegistration;
48 public void registerListener() {
49 LOG.info("Initializing LegatoSlsProfileController:init() ");
50 dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
51 new DataTreeIdentifier<Profile>(LogicalDatastoreType.CONFIGURATION, PROFILE_IID), this);
55 public void close() throws Exception {
56 if (dataTreeChangeListenerRegistration != null) {
57 dataTreeChangeListenerRegistration.close();
62 public void add(DataTreeModification<Profile> newDataObject) {
63 LOG.info(" Node Added " + newDataObject.getRootNode().getIdentifier());
64 addToOperationalDB(newDataObject.getRootNode().getDataAfter());
67 public void addToOperationalDB(Profile profile) {
69 assert profile != null;
70 CosProfiles cosProfiles = new CosProfilesBuilder().setProfile(Collections.singletonList(profile)).build();
71 InstanceIdentifier<CosProfiles> profilesTx = InstanceIdentifier.create(MefGlobal.class)
72 .child(CosProfiles.class);
73 LegatoUtils.addToOperationalDB(cosProfiles, profilesTx, dataBroker);
74 } catch (Exception ex) {
75 LOG.error("error: ", ex);
80 public void remove(DataTreeModification<Profile> removedDataObject) {
81 // TODO Auto-generated method stub
82 if (removedDataObject.getRootNode() != null && removedDataObject.getRootPath() != null) {
83 LOG.info(" Node removed " + removedDataObject.getRootNode().getIdentifier());
85 assert removedDataObject.getRootNode().getDataBefore() != null;
86 deleteFromOperationalDB(removedDataObject.getRootNode().getDataBefore());
87 } catch (Exception ex) {
88 LOG.error("error: ", ex);
94 public void deleteFromOperationalDB(Profile profile) {
95 assert profile != null;
96 LegatoUtils.deleteFromOperationalDB(InstanceIdentifier.create(MefGlobal.class)
97 .child(CosProfiles.class).child(Profile.class, new ProfileKey(profile.getId())),
102 public void update(DataTreeModification<Profile> modifiedDataObject) {
103 // TODO Auto-generated method stub
104 if (modifiedDataObject.getRootNode() != null && modifiedDataObject.getRootPath() != null) {
105 LOG.info(" Node modified " + modifiedDataObject.getRootNode().getIdentifier());
107 assert modifiedDataObject.getRootNode().getDataAfter() != null;
108 updateFromOperationalDB(modifiedDataObject.getRootNode().getDataAfter());
109 } catch (Exception ex) {
110 LOG.error("error: ", ex);
115 @SuppressWarnings("unchecked")
116 public void updateFromOperationalDB(Profile profile) {
117 assert profile != null;
118 InstanceIdentifier<Profile> instanceIdentifier = InstanceIdentifier.create(MefGlobal.class)
119 .child(CosProfiles.class).child(Profile.class, new ProfileKey(profile.getId()));
120 Optional<Profile> optionalProfile =
121 (Optional<Profile>) LegatoUtils.readProfile(LegatoConstants.COS_PROFILES,
122 dataBroker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
123 if (optionalProfile.isPresent()) {
124 LegatoUtils.deleteFromOperationalDB(instanceIdentifier, dataBroker);
125 addToOperationalDB(optionalProfile.get());