CONTROLLER-1809: De-deplicate extraneous ManagedService updates
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / ComponentProcessor.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  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.controller.blueprint.ext;
9
10 import com.google.common.base.Strings;
11 import java.util.ArrayList;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
18 import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
19 import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
20 import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
21 import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata;
22 import org.apache.aries.util.AriesFrameworkUtil;
23 import org.opendaylight.controller.blueprint.BlueprintContainerRestartService;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.Constants;
26 import org.osgi.framework.ServiceRegistration;
27 import org.osgi.service.blueprint.reflect.BeanProperty;
28 import org.osgi.service.blueprint.reflect.ComponentMetadata;
29 import org.osgi.service.blueprint.reflect.ValueMetadata;
30 import org.osgi.service.cm.ManagedService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The singleton component processor that is invoked by the blueprint container to perform operations on
36  * various component definitions prior to component creation.
37  *
38  * @author Thomas Pantelis
39  */
40 public class ComponentProcessor implements ComponentDefinitionRegistryProcessor {
41     static final String DEFAULT_TYPE_FILTER = "(|(type=default)(!(type=*)))";
42
43     private static final Logger LOG = LoggerFactory.getLogger(ComponentProcessor.class);
44     private static final String CM_PERSISTENT_ID_PROPERTY = "persistentId";
45
46     private final List<ServiceRegistration<?>> managedServiceRegs = new ArrayList<>();
47     private Bundle bundle;
48     private BlueprintContainerRestartService blueprintContainerRestartService;
49     private boolean restartDependentsOnUpdates;
50     private boolean useDefaultForReferenceTypes;
51
52     public void setBundle(final Bundle bundle) {
53         this.bundle = bundle;
54     }
55
56     public void setBlueprintContainerRestartService(final BlueprintContainerRestartService restartService) {
57         this.blueprintContainerRestartService = restartService;
58     }
59
60     public void setRestartDependentsOnUpdates(final boolean restartDependentsOnUpdates) {
61         this.restartDependentsOnUpdates = restartDependentsOnUpdates;
62     }
63
64     public void setUseDefaultForReferenceTypes(final boolean useDefaultForReferenceTypes) {
65         this.useDefaultForReferenceTypes = useDefaultForReferenceTypes;
66     }
67
68     public void destroy() {
69         for (ServiceRegistration<?> reg: managedServiceRegs) {
70             AriesFrameworkUtil.safeUnregisterService(reg);
71         }
72     }
73
74     @Override
75     public void process(final ComponentDefinitionRegistry registry) {
76         LOG.debug("{}: In process",  logName());
77
78         for (String name : registry.getComponentDefinitionNames()) {
79             ComponentMetadata component = registry.getComponentDefinition(name);
80             if (component instanceof MutableBeanMetadata) {
81                 processMutableBeanMetadata((MutableBeanMetadata) component);
82             } else if (component instanceof MutableServiceReferenceMetadata) {
83                 processServiceReferenceMetadata((MutableServiceReferenceMetadata)component);
84             }
85         }
86     }
87
88     private void processServiceReferenceMetadata(final MutableServiceReferenceMetadata serviceRef) {
89         if (!useDefaultForReferenceTypes) {
90             return;
91         }
92
93         String filter = serviceRef.getFilter();
94         String extFilter = serviceRef.getExtendedFilter() == null ? null :
95             serviceRef.getExtendedFilter().getStringValue();
96
97         LOG.debug("{}: processServiceReferenceMetadata for {}, filter: {}, ext filter: {}", logName(),
98                 serviceRef.getId(), filter, extFilter);
99
100         if (Strings.isNullOrEmpty(filter) && Strings.isNullOrEmpty(extFilter)) {
101             serviceRef.setFilter(DEFAULT_TYPE_FILTER);
102
103             LOG.debug("{}: processServiceReferenceMetadata for {} set filter to {}", logName(),
104                     serviceRef.getId(), serviceRef.getFilter());
105         }
106     }
107
108     private void processMutableBeanMetadata(final MutableBeanMetadata bean) {
109         if (restartDependentsOnUpdates && bean.getRuntimeClass() != null
110                 && AbstractPropertyPlaceholder.class.isAssignableFrom(bean.getRuntimeClass())) {
111             LOG.debug("{}: Found PropertyPlaceholder bean: {}, runtime {}", logName(), bean.getId(),
112                     bean.getRuntimeClass());
113
114             for (BeanProperty prop : bean.getProperties()) {
115                 if (CM_PERSISTENT_ID_PROPERTY.equals(prop.getName())) {
116                     if (prop.getValue() instanceof ValueMetadata) {
117                         ValueMetadata persistentId = (ValueMetadata)prop.getValue();
118
119                         LOG.debug("{}: Found {} property, value : {}", logName(),
120                                 CM_PERSISTENT_ID_PROPERTY, persistentId.getStringValue());
121
122                         registerManagedService(persistentId.getStringValue());
123                     } else {
124                         LOG.debug("{}: {} property metadata {} is not instanceof ValueMetadata",
125                                 logName(), CM_PERSISTENT_ID_PROPERTY, prop.getValue());
126                     }
127
128                     break;
129                 }
130             }
131         }
132     }
133
134     private void registerManagedService(final String persistentId) {
135         // Register a ManagedService so we get updates from the ConfigAdmin when the cfg file corresponding
136         // to the persistentId changes.
137         final ManagedService managedService = new ManagedService() {
138             private final AtomicBoolean initialUpdate = new AtomicBoolean(true);
139             private volatile Dictionary<String, ?> previousProperties;
140
141             @Override
142             public void updated(final Dictionary<String, ?> properties) {
143                 LOG.debug("{}: ManagedService updated for persistentId {}, properties: {}, initialUpdate: {}",
144                         logName(), persistentId, properties, initialUpdate);
145
146                 // The first update occurs when the service is registered so ignore it as we want subsequent
147                 // updates when it changes. The ConfigAdmin will send an update even if the cfg file doesn't
148                 // yet exist.
149                 if (!initialUpdate.compareAndSet(true, false) && !Objects.equals(previousProperties, properties)) {
150                     blueprintContainerRestartService.restartContainerAndDependents(bundle);
151                 }
152
153                 previousProperties = properties;
154             }
155         };
156
157         Dictionary<String, Object> props = new Hashtable<>();
158         props.put(Constants.SERVICE_PID, persistentId);
159         props.put(Constants.BUNDLE_SYMBOLICNAME, bundle.getSymbolicName());
160         props.put(Constants.BUNDLE_VERSION, bundle.getHeaders().get(Constants.BUNDLE_VERSION));
161         managedServiceRegs.add(bundle.getBundleContext().registerService(ManagedService.class.getName(),
162                 managedService, props));
163     }
164
165     private String logName() {
166         return bundle.getSymbolicName();
167     }
168 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.