c685a2e09ab71348f041915948470a5f1df1f1a3
[bgpcep.git] / pcep / server / server-provider / src / main / java / org / opendaylight / bgpcep / pcep / server / provider / ManagedTeNode.java
1 /*
2  * Copyright (c) 2021 Orange. 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.bgpcep.pcep.server.provider;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.mdsal.binding.api.TransactionChain;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.PathType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.pcc.configured.lsp.ConfiguredLsp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.pcc.configured.lsp.ConfiguredLspKey;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class ManagedTeNode {
26
27     private enum NodeState {
28         Disabled,
29         Enabled,
30         Sync
31     }
32
33     private final NodeId id;
34     private NodeState state;
35     private ConcurrentMap<ConfiguredLspKey, ManagedTePath> mngPaths =
36             new ConcurrentHashMap<ConfiguredLspKey, ManagedTePath>();
37     private final TransactionChain chain;
38     private static final Logger LOG = LoggerFactory.getLogger(ManagedTeNode.class);
39
40     public ManagedTeNode(final NodeId id, final TransactionChain chain) {
41         this.id = id;
42         this.chain = chain;
43         this.state = NodeState.Enabled;
44     }
45
46     public ManagedTeNode(final NodeId id, TransactionChain chain, final NodeState state) {
47         this.id = id;
48         this.chain = chain;
49         this.state = state;
50     }
51
52     public NodeState getState() {
53         return state;
54     }
55
56     public NodeId getId() {
57         return id;
58     }
59
60     public ConcurrentMap<ConfiguredLspKey, ManagedTePath> getTePaths() {
61         return mngPaths;
62     }
63
64     public WriteTransaction getTransaction() {
65         return chain.newWriteOnlyTransaction();
66     }
67
68     public boolean isSync() {
69         return state == NodeState.Sync;
70     }
71
72     public void sync() {
73         state = NodeState.Sync;
74     }
75
76     /**
77      * Disable this Managed TE Node and removed associated Managed Node from the Data Store. TE Path which are not
78      * delegated are removed from the path list other ones are mark as disabled.
79      */
80     public void disable() {
81         /* Remove associated TE Paths that are not managed by the PCE i.e. TE Path which are not delegated */
82         for (ManagedTePath mngPath: mngPaths.values()) {
83             final ConfiguredLsp lsp = mngPath.getLsp();
84             if (mngPath.getType() == PathType.Stateless || mngPath.getType() == PathType.Pcc) {
85                 mngPaths.remove(lsp.key());
86             } else {
87                 mngPath.disabled();
88             }
89         }
90
91         state = NodeState.Disabled;
92         LOG.debug("Managed TE Node {} has been disabled. Keep configuration {}", id, this);
93     }
94
95     public void addManagedTePath(final ManagedTePath mngPath) {
96         mngPaths.put(mngPath.getLsp().key(), mngPath);
97     }
98
99     public ManagedTePath removeManagedTePath(final ConfiguredLspKey key) {
100         checkArgument(key != null, "Provided Configured LSP Key is a null object");
101
102         /* Get corresponding Managed TE Path */
103         final ManagedTePath mngPath = mngPaths.remove(key);
104         if (mngPath == null) {
105             return null;
106         }
107
108         /* Remove corresponding Managed Path from the Data Store */
109         mngPath.removeFromDataStore();
110
111         return mngPath;
112     }
113
114     public ManagedTePath getManagedTePath(final ConfiguredLspKey key) {
115         checkArgument(key != null, "Provided Configured LSP Key is a null object");
116
117         return mngPaths.get(key);
118     }
119
120     @Override
121     public String toString() {
122         final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper("ManagedTeNode");
123         CodeHelpers.appendValue(helper, "NodeId", id);
124         CodeHelpers.appendValue(helper, "NodeState", state);
125         CodeHelpers.appendValue(helper, "ManagedTePaths", mngPaths);
126         return helper.toString();
127     }
128 }