2 * Copyright © 2020 Nokia, Inc. 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
8 package org.opendaylight.transportpce.servicehandler.listeners;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
18 import org.opendaylight.transportpce.common.OperationResult;
19 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkmodel.rev201116.TopologyUpdateResult;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkmodel.rev201116.TopologyUpdateResultBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkmodel.rev201116.TransportpceNetworkmodelListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkmodel.rev201116.topology.update.result.TopologyChanges;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkmodel.rev201116.topology.update.result.TopologyChangesKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirectionBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirectionBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZKey;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToA;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToABuilder;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToAKey;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.Resource;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.ResourceBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.TerminationPoint;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.service.path.PathDescription;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.service.path.PathDescriptionBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class NetworkModelListenerImpl implements TransportpceNetworkmodelListener {
49 private static final Logger LOG = LoggerFactory.getLogger(NetworkModelListenerImpl.class);
50 private final NotificationPublishService notificationPublishService; // to be used for T-API notification
51 private ServiceDataStoreOperations serviceDataStoreOperations;
52 private TopologyUpdateResult topologyUpdateResult;
54 public NetworkModelListenerImpl(NotificationPublishService notificationPublishService,
55 ServiceDataStoreOperations serviceDataStoreOperations) {
56 this.notificationPublishService = notificationPublishService;
57 this.serviceDataStoreOperations = serviceDataStoreOperations;
61 public void onTopologyUpdateResult(TopologyUpdateResult notification) {
62 LOG.debug("Topology update notification: {}", notification);
63 if (compareTopologyUpdateResult(notification)) {
64 LOG.warn("TopologyUpdateResult already wired !");
67 topologyUpdateResult = new TopologyUpdateResultBuilder()
68 .setTopologyChanges(new HashMap<>(notification.getTopologyChanges()))
70 // Update service datastore and service path description
71 updateServicePaths(notification);
75 * Process topology update result.
76 * @param notification the result notification.
78 protected void updateServicePaths(TopologyUpdateResult notification) {
80 Map<TopologyChangesKey, TopologyChanges> topologyChanges = notification.getTopologyChanges();
81 Optional<ServicePathList> servicePathListOptional = this.serviceDataStoreOperations.getServicePaths();
82 if (servicePathListOptional.isEmpty()) {
83 LOG.warn("Enable to retrieve service path list");
86 ServicePathList servicePathList = servicePathListOptional.get();
87 for (ServicePaths servicePaths : servicePathList.getServicePaths().values()) {
88 String serviceName = servicePaths.getServicePathName();
89 PathDescription pathDescription = servicePaths.getPathDescription();
90 // update path descriptions in the datastore
91 Map<AToZKey, AToZ> updatedAtoZ = changePathElementStateAZ(topologyChanges, pathDescription);
92 Map<ZToAKey, ZToA> updatedZtoA = changePathElementStateZA(topologyChanges, pathDescription);
93 OperationResult operationResult = this.serviceDataStoreOperations
94 .modifyServicePath(buildNewPathDescription(pathDescription, updatedAtoZ, updatedZtoA), serviceName);
95 if (!operationResult.isSuccess()) {
96 LOG.warn("Service Path not updated in datastore!");
99 // update service in the datastore. Only path description with all elements in service can have a service
100 // in service. Therefore we check if all the states of the path description resources are inService
101 Optional<Services> serviceOptional = this.serviceDataStoreOperations.getService(serviceName);
102 if (serviceOptional.isEmpty()) {
103 LOG.error("Couldn't retrieve service");
106 Services services = serviceOptional.get();
107 OperationResult operationResult1 = null;
109 switch (services.getOperationalState()) {
111 if (allElementsinPathinService(updatedAtoZ, updatedZtoA)) {
112 LOG.debug("Service {} state does not need to be modified", serviceName);
115 newState = State.OutOfService;
118 if (!allElementsinPathinService(updatedAtoZ, updatedZtoA)) {
119 LOG.debug("Service {} state does not need to be modified", serviceName);
122 newState = State.InService;
125 LOG.warn("Service {} state not managed", serviceName);
130 LOG.debug("Service={} needs to be updated to {}", serviceName, newState);
131 //if (operationResult1 != null && operationResult1.isSuccess()) {
132 //null check probably no more needed
133 if (this.serviceDataStoreOperations
134 .modifyService(serviceName, newState, services.getAdministrativeState())
136 LOG.info("Service state of {} correctly updated to {} in datastore", serviceName, newState);
139 LOG.error("Service state of {} cannot be updated to {} in datastore", serviceName, newState);
144 protected Map<ZToAKey, ZToA> changePathElementStateZA(Map<TopologyChangesKey, TopologyChanges> topologyChanges,
145 PathDescription pathDescription) {
146 Map<ZToAKey, ZToA> newztoaMap = new HashMap<>(pathDescription.getZToADirection().getZToA());
147 List<ZToA> tpResources = pathDescription.getZToADirection().getZToA().values().stream()
148 .filter(ele -> ele.getResource().getResource() instanceof TerminationPoint)
149 .collect(Collectors.toList());
150 for (ZToA ztoA : tpResources) {
151 String ztoAid = ztoA.getId();
152 State ztoAState = ztoA.getResource().getState();
153 TerminationPoint tp = (TerminationPoint) ztoA.getResource().getResource();
154 if (topologyChanges.containsKey(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId()))
155 && !topologyChanges.get(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId())).getState()
156 .equals(ztoAState)) {
157 LOG.debug("updating ztoa tp {}", ztoA);
158 State updatedState = topologyChanges.get(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId()))
160 Resource updatedResource = new ResourceBuilder()
162 .setState(updatedState)
164 ZToA updatedZToA = new ZToABuilder(ztoA)
166 .setResource(updatedResource)
168 newztoaMap.put(updatedZToA.key(), updatedZToA);
174 protected Map<AToZKey, AToZ> changePathElementStateAZ(Map<TopologyChangesKey,
175 TopologyChanges> topologyChanges, PathDescription pathDescription) {
177 Map<AToZKey, AToZ> newatozMap = new HashMap<>(pathDescription.getAToZDirection().getAToZ());
178 List<AToZ> tpResources = pathDescription.getAToZDirection().getAToZ().values().stream()
179 .filter(ele -> ele.getResource().getResource() instanceof TerminationPoint)
180 .collect(Collectors.toList());
181 for (AToZ atoZ : tpResources) {
182 String atoZid = atoZ.getId();
183 State atoZState = atoZ.getResource().getState();
184 TerminationPoint tp = (TerminationPoint) atoZ.getResource().getResource();
185 if (topologyChanges.containsKey(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId()))
186 && !topologyChanges.get(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId())).getState()
187 .equals(atoZState)) {
188 LOG.debug("updating atoz tp {}", atoZ);
189 State updatedState = topologyChanges.get(new TopologyChangesKey(tp.getTpNodeId(), tp.getTpId()))
191 Resource updatedResource = new ResourceBuilder()
193 .setState(updatedState)
195 AToZ updatedAToZ = new AToZBuilder(atoZ)
197 .setResource(updatedResource)
199 newatozMap.put(updatedAToZ.key(), updatedAToZ);
205 private PathDescription buildNewPathDescription(PathDescription pathDescription, Map<AToZKey, AToZ> updatedAtoZ,
206 Map<ZToAKey, ZToA> updatedZtoA) {
207 AToZDirection atozDir = new AToZDirectionBuilder(pathDescription.getAToZDirection())
208 .setAToZ(updatedAtoZ)
210 ZToADirection ztoaDir = new ZToADirectionBuilder(pathDescription.getZToADirection())
211 .setZToA(updatedZtoA)
213 return new PathDescriptionBuilder()
214 .setAToZDirection(atozDir)
215 .setZToADirection(ztoaDir)
219 protected boolean allElementsinPathinService(Map<AToZKey, AToZ> updatedAtoZ, Map<ZToAKey, ZToA> updatedZtoA) {
220 boolean allEleminService = true;
221 Iterator<AToZ> i1 = updatedAtoZ.values().iterator();
222 Iterator<ZToA> i2 = updatedZtoA.values().iterator();
223 // TODO: both directions have same length?
224 while (i1.hasNext() && i2.hasNext()) {
225 if (State.OutOfService.getIntValue() == i1.next().getResource().getState().getIntValue()
226 || State.OutOfService.getIntValue() == i2.next().getResource().getState().getIntValue()) {
227 allEleminService = false;
232 return allEleminService;
235 private boolean compareTopologyUpdateResult(TopologyUpdateResult notification) {
236 return topologyUpdateResult != null
237 && topologyUpdateResult.getTopologyChanges().equals(notification.getTopologyChanges());
240 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
241 this.serviceDataStoreOperations = serviceData;